1

I have some common UDFs and CFCs that I'd like to make available to all my controllers. I'm using Model-Glue 3. I've thought of several ways of doing so:

  1. Create a base controller that has <cfinclude>'s to the UDFs and instantiates the CFCs. All other controller inherit from this controller.
  2. Convert all UDFs to CFCs and use ColdSpring.xml to make the CFCs into beans. Then make it available to the controller using the beans attribute in ModelGlue.xml.
  3. Store the UDFs and CFCs in the helpers folder and access them using the helpers scope. However, this looks like it was intended to be used by a view rather than a controller.
  4. Create a global onRequestStart which will instantiate the CFCs and store them in the event object. Then the controllers will access the CFCs by grabbing them directly out of the event object.

My question is, what is the method used by most people to make a common set of UDFs and CFCs available to all controllers?

Daniel T.
  • 37,212
  • 36
  • 139
  • 206
  • "Best"? That's subjective. Certainly each developer will have their preference. – ale Sep 19 '11 at 13:11

1 Answers1

3

I would use option 2 above.

For those objects that need the helper methods I would use DI to inject a helper object into them. This will be more flexible going forward.

I do not like the idea of a base object with all of the helpers. Here is why:

  1. What if later you want to break up the helpers in multiple CFCs? You can't. Depending on how many help functions you have and how many it could grow into, this could make your objects ugly. What if you someday have 50 helper functions. Do you really want your controllers having 50 extra methods that really have nothing to do with their primary concern.

  2. Separations of concerns. Controllers should worry about being controllers. They should nto be loaded down with additional functions so that they know how to format strings. That should be handled by a StringHelper or something.

The other two options just don't sound great. What is the Helpers scope?

Jason Dean
  • 9,585
  • 27
  • 36
  • Model Glue 3 introduced a helpers folder. Any UDF or CFC that you drop in there can be accessed globally by using `helpers.udfName.functionName`, and the framework handles the instantiation and caching. The official docs has a section on it: http://docs.model-glue.com/wiki/HowTos/HowToUseHelpers#Helpers – Daniel T. Sep 18 '11 at 21:36
  • Well, then that seems like a good way to go, and one that the framework developers intended for you to use. If you are asking then you must have some reservations about using it. What would those be? – Jason Dean Sep 18 '11 at 21:55
  • It seems that by helpers, the dev meant "view helpers". I'm not so sure if using it in a controller is good practice. – Daniel T. Sep 18 '11 at 22:06
  • Well, it says in the docs: "Model-Glue 3 solves this problem by creating support for what it calls "helpers". Helpers are libraries of user defined functions or CFCs of methods. By placing these files in the proper location. Model-Glue 3 will automatically load them and make them accessible via the new helpers scope. ***This scope is available to both controller and views file. ***". So it seems like it should be OK. – Jason Dean Sep 18 '11 at 22:15
  • Oh nice, I missed that line. Thanks! – Daniel T. Sep 18 '11 at 22:34