Question

I am using ColdFusion 9.0.1.

Right now, we are including our UDF library in the onRequest() method in our application.cfc. Here's how we include it:

<cfscript>
    // INCLUDE LIBRARY
    include "udf/udf_library.cfm";
</cfscript>

I am wondering if there is a way to put the entire library into the APPLICATION scope so that it's created just once. Would we do this the same way as we put our orders.cfc into the APPLICATION scope, like this:

APPLICATION.AppInfo.objOrders = createObject("component", "globaladmin.orders");

Should the UDF library be converted to a CFC to make this happen?

How would we reference the function in the CFC?

Currently we call the UDF functions with no fuss, like this:

<cfscript>
   createButton("Canada Postal Codes", "ShowSection", "ShippingCanadaPostalCodes");
   wrapCell(Buttons);
   wrapRow(Cells, "TableSubHead"));
</cfscript>

It would be really ugly to have to add "APPLICTION.AppInfo" before each function.

So, would there be any advantage to moving the UDF library to the APPLICATION scope or loading it only once somewhere else?

Was it helpful?

Solution

I think scoping your UDFs is a good idea. As it is you just have them as part of the REQUEST scope, so there's room for name clashes if methods of the same names are declared in .cfm pages in other parts of your application.

If you do add them and object in the APPLICATION scope, in onApplicationSart() for instance, then you'll have to be aware of thread safety issues. Presumably your UDFs are fairly self contained, so your APPLICATION scoped object won't hold any internal state as such, so you should be safe enough in that respect.

If you just want it created once then add your functions to a CFC and create it in your Application.cfc's onApplicationStart() function, assigned to the APPLICATION scope as you've described above.

Should the UDF library be converted to a CFC to make this happen?

YES

How would we reference the function in the CFC?

APPLICATION.AppInfo.yourObj.createButton("Canada Postal Codes", "ShowSection", "ShippingCanadaPostalCodes")
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top