Skip to content

Instantly share code, notes, and snippets.

@RichardDavies
Created March 30, 2011 20:48
Show Gist options
  • Save RichardDavies/895261 to your computer and use it in GitHub Desktop.
Save RichardDavies/895261 to your computer and use it in GitHub Desktop.
Reactor factory with autowire support (for use with ColdBox)
<cfcomponent displayname="Application global implicit events handler" extends="coldbox.system.EventHandler" output="false" autowire="true">
<!--- Autowire dependencies --->
<cfproperty name="reactor" inject="ocm" scope="instance" /> <!--- Inject Reactor factory from ColdBox cache --->
<cfset Instance = StructNew() />
<cffunction name="onAppInit" returntype="void" output="false">
<cfargument name="event" required="true" />
<cfscript>
// Autowire the custom Reactor Factory
getPlugin("beanFactory").autowire(Instance.reactor, false);
</cfscript>
</cffunction>
</cfcomponent>
<!--- This is a modified version of the Reactor Factory object that autowires objects created by this factory --->
<cfcomponent displayname="Reactor Factory" hint="Reactor factory with autowire support" extends="reactor.reactorFactory" output="false" autowire="true" cache="true" cacheTimeout="0">
<!--- Autowire dependencies --->
<cfproperty name="beanFactory" inject="coldbox:plugin:beanFactory" scope="instance" /> <!--- Inject ColdBox beanFactory plugin --->
<cfset Instance = StructNew() />
<!------------------------------------------- CONSTRUCTOR METHOD ------------------------------------------>
<cffunction name="init" returntype="ReactorFactory" output="false" access="public">
<cfargument name="configuration" hint="I am either a relative or absolute path to the config XML file or an instance of a reactor.config.config component" required="yes" type="any" _type="any" />
<cfargument name="BeanFactory" hint="I am an IOC beanfactory that you can inject to inject objects into your records and gateways" required="false" type="coldspring.beans.BeanFactory">
<cfscript>
Super.init(ArgumentCollection=Arguments);
return this;
</cfscript>
</cffunction>
<!------------------------------------------- PUBLIC METHODS ------------------------------------------>
<cffunction name="createDao" returntype="any" output="false" access="public">
<cfargument name="objectAlias" type="string" />
<cfscript>
var Local = StructNew();
Local.object = Super.createDao(Arguments.objectAlias);
autowire(Local.object);
return Local.object;
</cfscript>
</cffunction>
<cffunction name="createDictionary" returntype="any" output="false" access="public">
<cfargument name="objectAlias" type="string" />
<cfscript>
var Local = StructNew();
Local.object = Super.createDictionary(Arguments.objectAlias);
autowire(Local.object);
return Local.object;
</cfscript>
</cffunction>
<cffunction name="createGateway" returntype="any" output="false" access="public">
<cfargument name="objectAlias" type="string" />
<cfscript>
var Local = StructNew();
Local.object = Super.createGateway(Arguments.objectAlias);
autowire(Local.object);
return Local.object;
</cfscript>
</cffunction>
<cffunction name="createMetadata" returntype="any" output="false" access="public">
<cfargument name="objectAlias" type="string" />
<cfscript>
var Local = StructNew();
Local.object = Super.createMetadata(Arguments.objectAlias);
autowire(Local.object);
return Local.object;
</cfscript>
</cffunction>
<cffunction name="createRecord" returntype="any" output="false" access="public">
<cfargument name="objectAlias" type="string" />
<cfscript>
var Local = StructNew();
Local.object = Super.createRecord(Arguments.objectAlias);
autowire(Local.object);
return Local.object;
</cfscript>
</cffunction>
<cffunction name="createTo" returntype="any" output="false" access="public">
<cfargument name="objectAlias" type="string" />
<cfscript>
var Local = StructNew();
Local.object = Super.createTo(Arguments.objectAlias);
autowire(Local.object);
return Local.object;
</cfscript>
</cffunction>
<cffunction name="createValidator" returntype="any" output="false" access="public">
<cfargument name="objectAlias" type="string" />
<cfscript>
var Local = StructNew();
Local.object = Super.createValidator(Arguments.objectAlias);
autowire(Local.object);
return Local.object;
</cfscript>
</cffunction>
<!--- BeanFactory
getPlugin("beanFactory").autowire() thinks setBeanFactory() is a WireBox method and tries to call it and
pass in the ColdBox bean factory.
--->
<cffunction name="setBeanFactory" access="public" output="false" returntype="void" hint="I set a BeanFactory (Spring-interfaced IoC container) to inject into all created objects)." >
<cfargument name="factory" type="any" required="true" />
<cfscript>
if (IsInstanceOf(Arguments.factory, "coldspring.beans.BeanFactory")) {
Super.setBeanFactory(Arguments.factory);
}
</cfscript>
</cffunction>
<!------------------------------------------- PRIVATE METHODS ------------------------------------------>
<cffunction name="autowire" returntype="void" output="false" access="private">
<cfargument name="object" type="any" />
<cfscript>
var Local = StructNew();
// Search object's metadata structure for all autowire attributes
Local.autowireMD = StructFindKey(GetMetaData(Arguments.object), "autowire", "all");
// Only autowire objects with autowire="true" (either declared directly in object or an inherited object)
for (Local.i = 1; Local.i <= ArrayLen(Local.autowireMD); Local.i++) {
if (Local.autowireMD[Local.i].value is true) {
Instance.beanFactory.autowire(Arguments.object);
break;
}
}
</cfscript>
</cffunction>
</cfcomponent>
@RichardDavies
Copy link
Author

Don't forget to tell the ReactorLoader interceptor to use this custom factory by specifying the ReactorFactoryClassPath property.

Example:
// Reactor ORM
{ class="coldbox.system.orm.reactor.ReactorLoader",
properties = {
ReactorFactoryClassPath = "#appMapping#/model/reactor/reactorFactory",
dsnAlias = "myDSN",
pathToConfigXML = "config/Reactor.xml.cfm",
project = "myProject",
mapping = "/#appMapping#/model/reactor",
mode = "production"
}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment