While building my first app using CFCs yesterday, I found it easier for
me to do a test for the existence of an object and if not found,
instantiate it within Application.cfm like this.
<cfif NOT isDefined("request.dsn")>
<cfset request.dsn = "mydatabase" />
</cfif>
<cfif NOT isDefined("request.inventory")>
<cfset request.inventory = createObject("component",
"cfcs.inventory").init(request.dsn) />
</cfif>
<cfif NOT isDefined("request.classes")>
<cfset request.classes = createObject("component",
"cfcs.classes").init(request.dsn) />
</cfif>
...
/application.cfm
Each instantiation is calling a simple init() method within the class
such as this one within inventory.cfc:
<cfcomponent hint="Inventory Object">
<cfset variables.dsn = "" />
<cfset variables.inventory = "" />
<cffunction name="init" access="public" returnType="inventory"
output="false" hint="I instantiate qry">
<cfargument name="dsn" type="string" required="yes" />
<!--- initiate value(s) --->
<cfset variables.dsn = arguments.dsn />
<cfset variables.inventory = "none" />
<cfreturn this />
</cffunction>
(there are more methods within this cfc but I'm leaving them out to make
this easier to read.)
</cfcomponent>
My thought here is that I will always have access to the instantiated
objects without having to test for their existence. However, since I'm
not completely familiar with how this all works, Am I creating large
amounts of overhead to the server for this convenience? I wouldn't
think so since I'm not calling any major methods yet but maybe there is
more going on?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
Get the answers you are looking for on the ColdFusion Labs
Forum direct from active programmers and developers.
http://www.adobe.com/cfusion/webforums/forum/categories.cfm?forumid-72&catid=648
Archive:
http://www.houseoffusion.com/groups/CF-Talk/message.cfm/messageid:288895
Subscription: http://www.houseoffusion.com/groups/CF-Talk/subscribe.cfm
Unsubscribe:
http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=11502.10531.4