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:
- 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.
- 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.
- 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.
- 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.
- CFInclude my UDFs from separate templates, directly in Application.cfc This is functionally similar to instantiating in the Application scope.
- 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.
No comments:
Post a Comment