Search code examples
coldfusioncfmllucee

Including Application's user-defined-functions into modules


My application for simplistic purposes has

  • Application
    • include functions.cfm
    • a cache object application.o
      • cfcomponent modules each with create/update/view a model type
        • each compent includes functions.cfm so that it may use them, resulting in each module containing UDFs that they don't use.
        • an example would be
          • application.o.items = new cfcs.items()
            • includes functions.cfm
          • application.o.categories = new cfcs.categories()
            • includes functions.cfm

The result is that each of the 10 modules is duplicating the entirety of functions.cfm into each module.

I've re-engineered, turning functions into a component (it's named application._, which is copied into variarables._ inside each module and calling the functions like _.myUDF() which should be calling application._ by reference rather than recreating every function in the file for every component.

This should be the faster and lightest way to do this, shouldn't it? Can anyone suggest enhancements to this style, or give reasons why include might be preferrable, beyond the need to recreate the component if I change add functions to the component functions.cfc?


Solution

  • Your file functions.cfm was included in your CFC files, so you can then call functionA() within any of those CFCs.

    If you convert to functions.cfc and inject that object into your other CFCs, then you'll have to reference the functions objects within your other CFC files:

    <cfset functions.functionA()>

    So that's a lot of code to refactor.

    Seems like you should extend your existing CFCs with functions.cfc:

    <cfcomponent extends="cfc.functions">

    This makes your other components child objects of functions.cfc, which therefore inherit the contents of functions.cfc. Now, from within any child CFC, you can call functionA() as you had when you included functions.cfm.

    If that's more in line with what you want and you want ALL of your CFCs to include these functions, you can pull a totally baller move and update your CF server's core component.cfc from which all CFCs are extended. This file should be found in <CF_ROOT>\WEB-INF\cftags. I've done this in the past to add additional "native" functions to ColdBox applicaitons. Just make sure you track those changes in source control.