OOPhoto - Simple Service Objects In Place

<cfcomponent
	output="false"
	hint="Creates, intializes, and wires together all objects that are needed in the system.">
 
	<!--- Setup data structures and default values. --->
	<cfset VARIABLES.Instance = {} />
 
 
	<cffunction
		name="Init"
		access="public"
		returntype="any"
		output="false"
		hint="Returns an initialized component.">
 
		<!--- Define arguemnts. --->
		<cfargument
			name="Config"
			type="any"
			required="true"
			hint="The application configuration object."
			/>
 
		<!--- Store the configuration object. --->
		<cfset VARIABLES.Instance.Config = ARGUMENTS.Config />
 
		<!--- Create and store the UDF library. --->
		<cfset VARIABLES.Instance.UDF = THIS.CreateCFC( "UDF" ).Init() />
 
		<!--- Create and store the photo gallery service. --->
		<cfset VARIABLES.Instance.PhotoGalleryService = THIS.CreateCFC( "PhotoGalleryService" ) />
 
		<!--- Create and store the photo service. --->
		<cfset VARIABLES.Instance.PhotoService = THIS.CreateCFC( "PhotoService" ) />
 
		<!--- Create and store the comment service. --->
		<cfset VARIABLES.Instance.CommentService = THIS.CreateCFC( "CommentService" ) />
 
		<!--- Inject dependencies and then initialize the object. --->
		<cfset VARIABLES.Instance.PhotoGalleryService.InjectDependency(
			"DSN",
			THIS.Get( "Config" ).DSN
			).Init() />
 
		<!--- Inject dependencies and then initialize the object. --->
		<cfset VARIABLES.Instance.PhotoService.InjectDependency(
			"DSN",
			THIS.Get( "Config" ).DSN
			).Init() />
 
		<!--- Inject dependencies and then initialize the object. --->
		<cfset VARIABLES.Instance.CommentService.InjectDependency(
			"DSN",
			THIS.Get( "Config" ).DSN
			).Init() />
 
		<!--- Return This reference. --->
		<cfreturn THIS />
	</cffunction>
 
 
	<cffunction
		name="CreateCFC"
		access="public"
		returntype="any"
		output="false"
		hint="Returns the given CFC.">
		 
		<!--- Define arguments. --->
		<cfargument
			name="CFC"
			type="string"
			required="true"
			hint="The relative path to the CFC to create."
			/>
 
		<!--- Return non-initialized CFC. --->
		<cfreturn CreateObject( "component", ARGUMENTS.CFC ) />
	</cffunction>
 
 
	<cffunction
		name="Get"
		access="public"
		returntype="any"
		output="false"
		hint="Returns the given object.">
 
		<!--- Define arguments. --->
		<cfargument
			name="Type"
			type="string"
			required="true"
			hint="The type of object that we need to return."
			/>
 
 
		<!--- Check to see if the given object exists in our singleton cache. --->
		<cfif StructKeyExists( VARIABLES.Instance, ARGUMENTS.Type )>
 
			<!--- Return the cached singleton. --->
			<cfreturn VARIABLES.Instance[ ARGUMENTS.Type ] />
 
		<cfelseif (ARGUMENTS.Type EQ "ErrorCollection")>
 
			<!--- Return new instance. --->
			<cfreturn THIS.CreateCFC( ARGUMENTS.Type ).Init() />
 
		<cfelse>
 
			<!---
				If we have gotten this far, then an unknown object has
				been requested. Throw an exception.
			--->
			<cfthrow
				type="OOPhoto.UnknownObjectType"
				message="You have requested an unknown object type."
				detail="The object type you have requested #UCase( ARGUMENTS.Type )# is not valid for this object factory."
				/>
 
		</cfif>
	</cffunction>
 
</cfcomponent>

For Cut-and-Paste