Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extending ColdFusion with application wide UDFs

I've been exploring different methods of structuring my ColdFusion applications and I'm looking for some opinions on the best way to provide application wide UDFs.

For each of my apps, I generally use a bunch of extra functions that don't really belong in any particular object. Data manipulation stuff mostly. I want these functions to be available throughout my application, both for use in CFM templates and in Application instantiated CFCs.

The way I see it there are various methods of achieving this, but they all have their own limitations:

  1. Instantiate a base Utils CFC in the Application scope. This is the method I've used most often. All the functions are available app wide, but if I instantiate the same CFC from multiple applications then they'll each have their own application scope - meaning that each has to instantiate their own base Utils CFC. There's nothing wrong with this but it feels like I'm not encapsulating the CFC well enough. I'm not keen on referencing the Application scope from within the CFC.

  2. Create an base Utils CFC and make every other CFC extend this. This works fine, and it means the CFC can reference the Utils functions directly from the CFC's THIS scope - However it means the Utils functions are kept in memory for every CFC. It also doesn't sit right conceptually as my other CFCs have no relationship with the Utils CFC.

  3. Inject my base Utils CFC into my other CFCs. Another method I've been playing with is instantiating my base Utils CFC in the Application scope, but then passing that as an object to an argument in my other CFCs. This works for me conceptually, and for encapsulation. In the same way that I'll set up my datasources in my init method, I can do the same with my UDFs. This has the same issue that the UDFs are included in each CFC. When I dump all my CFCs I get each UDF multiple times - however as I'm passing an instantiated object, I'm assuming that it's not taking any extra memory space. If anyone could confirm that, it'd be helpful - I'm just assuming! The only real problem I have with this method is that it seems a bit convoluted.

  4. Have my Application CFC extend my Utils CFC. This is what a lot of frameworks seem to do. I've not used this method, but I'm sure there are pros and cons.

  5. CFInclude my UDFs from separate templates, directly in Application.cfc This is functionally similar to instantiating in the Application scope.

  6. Add my UDFs to the server's Components.cfc It's a great idea in theory - I can maintain one copy of the base Utils and be sure that everything on the server can access them - However, if I want to run apps across multiple servers then they'll all need those functions. Plus any update to the server may overwrite the components. It just feels like hacking the core - which I'm sure we can all atest from bitter experience, is bad.

So - my question is this: What is the best practice for extending CF with UDFs in an elegant and reusable way? Any of the above options or something I've not thought of?

like image 390
Gary Stanton Avatar asked Mar 16 '13 16:03

Gary Stanton


1 Answers

If you are really concerned about structure and keeping things independent, don't even start with singletons or inheritance that extend functionality. Instead extend the base functionality within ColdFusion by appending your non-component library on runtime/request, see ColdFusion Developer's Guide. This doesn't magically solve all problems, but at least that's the proper way to implement general purpose functions.

like image 119
Alex Avatar answered Nov 17 '22 01:11

Alex