Just
following up on a promise to post a singleton example. This bit of code comes
from a base cms model I have been working on that I will be releasing
as open source software as soon as I am satisfied with it. This Singleton
is called on each request, in this example in an fbx_settings.cfm file in a FB3
app:
<cfset attributes.cmDAOFactorySingleton =
createObject("component","com.rosecanyon.cms.cmDAO.cmDAOFactorySingleton").init()/>
<--- you can call the reset function to re-load
the factory --->
<cfset
attributes.cmDAOFactorySingleton.reset()/>
<--- then you just call the factory that you want,
in this case the MySQL factory --->
<cfset attributes.cmDAOFactory =
attributes.cmDAOFactorySingleton.getcmDAOFactory("mysql",request.readDSN,request.writeDSN)
/>
One of
the things I am likely going to change in this model is the way I pass base
config arguments like the db type and datasources. I am planning on implementing
a config bean to hold the configuration properties.
<!--- cmDAOFactorySingleton.cfc
--->
<cfcomponent
displayname="cmDAOFactorySingleton">
<cffunction name="init" access="public" output="false" returntype="cmDAOFactorySingleton">
<cfreturn this/>
</cffunction>
<cffunction name="reset" access="public" output="false" returntype="void">
<cfset structDelete(application,"cmDAOFactory")/>
</cffunction>
<cffunction name="init" access="public" output="false" returntype="cmDAOFactorySingleton">
<cfreturn this/>
</cffunction>
<cffunction name="reset" access="public" output="false" returntype="void">
<cfset structDelete(application,"cmDAOFactory")/>
</cffunction>
<cffunction name="getcmDAOFactory"
access="public" returntype="cmDAOFactory">
<cfargument name="dbtype" type="string" required="true"/>
<cfargument name="readdsn" type="string" required="true" />
<cfargument name="writedsn" type="string" required="true" />
<cfif NOT structKeyExists(application,"cmDAOFactory")>
<cfset setcmDAOFactory(arguments.dbtype,arguments.readdsn,arguments.writedsn)/>
</cfif>
<cfreturn application.cmDAOFactory />
</cffunction>
<cfargument name="dbtype" type="string" required="true"/>
<cfargument name="readdsn" type="string" required="true" />
<cfargument name="writedsn" type="string" required="true" />
<cfif NOT structKeyExists(application,"cmDAOFactory")>
<cfset setcmDAOFactory(arguments.dbtype,arguments.readdsn,arguments.writedsn)/>
</cfif>
<cfreturn application.cmDAOFactory />
</cffunction>
<cffunction name="setcmDAOFactory"
access="public" returntype="void">
<cfargument name="dbtype" type="string" required="true"/>
<cfargument name="readdsn" type="string" required="true" />
<cfargument name="writedsn" type="string" required="true" />
<cfset application.cmDAOFactory = createObject("component","com.rosecanyon.cms.cmDAO.cmDAOFactory").init(arguments.dbtype,arguments.readdsn,arguments.writedsn)/>
<cfswitch _expression_="#arguments.dbtype#">
<cfcase value="mssql">
<cfset application.cmDAOFactory = createObject("component","com.rosecanyon.cms.cmDAO.cmDAOFactory_mssql").init(arguments.readdsn,arguments.writedsn)/>
</cfcase>
<cfcase value="mysql">
<cfset application.cmDAOFactory = createObject("component","com.rosecanyon.cms.cmDAO.cmDAOFactory_mysql").init(arguments.readdsn,arguments.writedsn)/>
</cfcase>
<cfdefaultcase>
<cfset application.cmDAOFactory = createObject("component","com.rosecanyon.cms.cmDAO.cmDAOFactory").init(arguments.readdsn,arguments.writedsn)/>
</cfdefaultcase>
</cfswitch>
</cffunction>
</cfcomponent>
<cfargument name="dbtype" type="string" required="true"/>
<cfargument name="readdsn" type="string" required="true" />
<cfargument name="writedsn" type="string" required="true" />
<cfset application.cmDAOFactory = createObject("component","com.rosecanyon.cms.cmDAO.cmDAOFactory").init(arguments.dbtype,arguments.readdsn,arguments.writedsn)/>
<cfswitch _expression_="#arguments.dbtype#">
<cfcase value="mssql">
<cfset application.cmDAOFactory = createObject("component","com.rosecanyon.cms.cmDAO.cmDAOFactory_mssql").init(arguments.readdsn,arguments.writedsn)/>
</cfcase>
<cfcase value="mysql">
<cfset application.cmDAOFactory = createObject("component","com.rosecanyon.cms.cmDAO.cmDAOFactory_mysql").init(arguments.readdsn,arguments.writedsn)/>
</cfcase>
<cfdefaultcase>
<cfset application.cmDAOFactory = createObject("component","com.rosecanyon.cms.cmDAO.cmDAOFactory").init(arguments.readdsn,arguments.writedsn)/>
</cfdefaultcase>
</cfswitch>
</cffunction>
</cfcomponent>
---------------------------------------------------------------Original Message-----
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Rob Munn
Sent: Monday, February 27, 2006 10:41 PM
To: [email protected]
Subject: RE: [CFCDev] Lazy Load - any patterns to encapsulate?This looks like the Singleton pattern to me:page = CreateObject("component","prototype.entity.systemsforge.PageSingleton").Init("#ThisPageFilePath#");};Inside the PageSingleton.cfc init() function you put the logic to check whether the page exists in your application.page structure, initialize it if it doesn't exist, and return it. That way the only place you ever reference the application scope is in PageSingleton. I can post a clearer code example tomorrow when my brain is less foggy, but this is the method I learned from others here and it has worked well for me.---------------------------------------------------------------Original Message-----
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Peter Bell
Sent: Monday, February 27, 2006 7:58 PM
To: [email protected]
Subject: [CFCDev] Lazy Load - any patterns to encapsulate?Hi There,So, I have a page class that I lazy load to minimize initial application load time for large sites. However, for performance purposes, I store each page object once it has been created using the application scope (I don't have so many instances of the page class that this would be a problem in terms of memory and each one is fairly small).So, my calling code looked like this:<cfscript>// If the page doesn't exist in application scope, initialize it (lazy load)
if(not isDefined("Application.page.#ThisPageFilePath#"))
{"Application.page.#ThisPageFilePath#" = CreateObject("component","prototype.entity.systemsforge.Page").Init("#ThisPageFilePath#");};</cfscript>That looked plain wrong (seemed to be wrong for anything other than a page something - e.g. a page service to be worrying about whether or not a given page had been constructed or not), so I created an Application.PageService, provided it with a GetbyFilePath() method and put the above code in there.Is this going in the right direction?Best Wishes,
Peter----------------------------------------------------------
You are subscribed to cfcdev. To unsubscribe, send an email to [EMAIL PROTECTED] with the words 'unsubscribe cfcdev' as the subject of the email.
CFCDev is run by CFCZone (www.cfczone.org) and supported by CFXHosting (wwwcfxhosting.com).
An archive of the CFCDev list is available at www.mail-archive.com/[email protected]
You are subscribed to cfcdev. To unsubscribe, send an email to [email protected] with the words 'unsubscribe cfcdev' as the subject of the email.
CFCDev is run by CFCZone (www.cfczone.org) and supported by CFXHosting (wwwcfxhosting.com).
An archive of the CFCDev list is available at www.mail-archive.com/[email protected]
You are subscribed to cfcdev. To unsubscribe, send an email to [email protected] with the words 'unsubscribe cfcdev' as the subject of the email.
CFCDev is run by CFCZone (www.cfczone.org) and supported by CFXHosting (www.cfxhosting.com).
An archive of the CFCDev list is available at www.mail-archive.com/[email protected]
