cfprabhu
cfprabhu

Reputation: 5362

How to initiate the multiple cfc with one object?

I have one project folder. Inside the project folder is one Application.cfc, one index.cfm and a folder of cfc's:

/ProjectFolder

In the cfc folder, I have 10 *.cfc files. How can I initiate, or map, the 10 *.cfc files with the one object in ColdFusion?

Upvotes: 1

Views: 120

Answers (1)

Adrian J. Moreno
Adrian J. Moreno

Reputation: 14859

If any of the CFCs can be created once and only once, meaning that they do nothing more than call stored procedures, contain algorithms or other business logic, then you can simply create those CFCs as application scoped variables when the application first starts.

In this example, Application.cfc is in the root folder and the CFC files are in the /cfc/ folder.

<cffunction name="onApplicationStart" returnType="boolean" output="false">
    <cfset application.stObject = {
            foo = new cfc.Foo()
            , bar = new cfc.Bar()
            , etc = new cfc.Etc()
        } />
    <cfreturn true />
</cffunction>

Then, anywhere in your code, you can reference a particular CFC and call a function in it like so:

<cfset qMyData = application.stObject.foo.getMyData() />

If you need to populate a CFC with data and carry it around though a user's session, you would want to create that object using onSessionStart() in Application.cfc. You can also create it at the point that you need to populate it and place it into session instead of carrying around an empty version that never gets used.

Finally, you may need to just create certain of those CFCs as needed for the life of the page request only. Those would be created in the variables scope and populated at some point during the request and would be removed once the request has completed.

Upvotes: 2

Related Questions