3
votes

We have four different environments: dev, q/a, test and prod. I decided to convert our Application.cfm to Application.cfc and put them in our source control. There are plenty application vars that have different values for each environment.

I ended up creating several sql server tables to store these environment variables based on their types. Now, I am in the middle of dynamically setting up these application variables.

My question is that I started adding custom methods inside application.cfc. I am not 100% sure if this is the best place. [For example: getAppLinks(), setAppLinks() ]. Otherwise, I could create a new cfc and call this one from Application.cfc.

All these methods are currently being called once in the onApplicationStart() method.

Does anybody have any comments on implementing custom methods in Application.cfc?

thanks

edited: added a custom method:

 <cffunction name="setUpAppDSNs" access="private" returnType="void" output="false">
    <cfargument name="dsn"        type="string" required="yes">
    <cfargument name="serverName" type="string" required="yes">
    <cfscript>
        var dsnNames   = structNew();           
        var qryAppDSNs = new Query(dataSource = '#arguments.dsn#',  
                                   sql        = ' SELECT dsnID, #arguments.serverName#Server, description
                                                  FROM cfAppDSN ').execute().getResult();  
         for (i = 1; i lte qryAppDSNs.recordCount; i++) {
            dsnNames['#qryAppDSNs.description[i]#'] = qryAppDSNs['#serverName#Server'][i]; 
         }

         StructAppend(application,dsnNames);
    </cfscript>
</cffunction>
2
At my previous job we had quite a few custom methods located in our Application.cfc and we never had a problem. If it's going to be methods that get used through out the application I dont see a problem with it. - Anonymous
I do that for code that's only called within application.cfc. My onApplicationStart calls setupApplication(), which in turn calls a number of setup methods. The nice thing about this is that in onRequest, I can check for URL.reset=="whatever" and call setupApplication() from there, which helps in development - barnyr
Great! I have something similar to force application start within onRequestStart method. - CFNinja
it is possible to call onApplicationStart manually but If you call this method explicitly, ColdFusion does not start the application; it does execute the method code, but does not lock the Application scope while the method executes. - Travis

2 Answers

4
votes

I have a similar issue and solved it by extending the application.cfc with our globalFunctions.cfc

<cfcomponent displayname="Application" 
     output="false" extends="shared.cfc.globalFunctions">

I don't know if this will work for you but it allowed us to use the same functions it multiple different applications without maintaining multiple copies of those functions.

3
votes

Application.cfc is just a CFC. the only "special" things about it are:

  1. CF instantiates an instance of it every request;
  2. coincidental to that, ColdFusion looks for a few event handlers and interceptors in there (onApplicationStart(), etc).

But it's still just a CFC. Given it's called Application.cfc and busies itself with the application lifecycle, it makes sense to put methods in there that relate to the application lifecycle - same as with organising any CFC.

So to answer your question... Application.cfc is exactly the right place for these methods of yours.