Kung Fu Ninja
Kung Fu Ninja

Reputation: 3752

Custom methods inside Application.cfc

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>

Upvotes: 3

Views: 1176

Answers (2)

Adam Cameron
Adam Cameron

Reputation: 29870

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.

Upvotes: 3

Lance
Lance

Reputation: 3213

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.

Upvotes: 4

Related Questions