Skip to content

Instantly share code, notes, and snippets.

@rip747
Created June 1, 2012 17:38
Show Gist options
  • Save rip747/2853902 to your computer and use it in GitHub Desktop.
Save rip747/2853902 to your computer and use it in GitHub Desktop.
implementing a CFWheels service layer
<!--- add the following to the events/onapplicationstart.cfm --->
<cffunction name="initServices" returntype="void" hint="I initialize the services objects for this app">
<cfset var loc = {}>
<cfset application.$_ServiceObjects = {}>
<cfdirectory action="list" directory="#expandPath('services')#" name="loc.services"/>
<cfloop query="loc.services">
<cfset application.$_ServiceObjects[ListFirst(name, '.')] = createObject("component", "services.#ListFirst(name, '.')#").init()>
</cfloop>
</cffunction>
<cfset initServices()>
<!--- add the following to the events/functions.cfm --->
<cffunction name="service" returntype="any" hint="I am the method used to access any service layer object from within any controller">
<cfargument name="service" type="string" required="true" />
<cfset var retval = "" />
<cfif structkeyExists(application.$_ServiceObjects,arguments.service)>
<cfset retval = application.$_ServiceObjects[arguments.service] />
</cfif>
<cfreturn retval />
</cffunction>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment