Reactor factory with autowire support
Automatically autowires Reactor objects
Description
This is a custom Reactor Factory object that will automatically autowire objects created by the factory. This way you can use Reactor and also take advantage of ColdBox's dependency injection.
This factory will autowire any objects created with the following factory methods:
- createDao()
- createDictionary()
- createGateway()
- createMetadata()
- createRecord()
- createTo()
- createValidator()
Installation Instructions
- Save ReactorFactory.cfc somewhere inside your app (ex. /model/reactor/ReactorFactory.cfc)
- This component extends the original ReactorFactory object, so modify the extends attribute if Reactor isn't installed in the default location (reactor.reactorFactory).
- Tell the ReactorLoader interceptor to use this custom factory by
specifying the ReactorFactoryClassPath property in your ColdBox configuration settings. For 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"
}
}
- Add autowire="true" to the <cfcomponent> tag of the event handler containing your onAppInit() method (usually Main.cfc)
- Add the following code after the opening <cfcomponent> line:
<cfproperty name="reactor" inject="ocm" scope="instance" />
<cfset Instance = StructNew() /> - Add the following line at the top of your onAppInit() event method code:
getPlugin("beanFactory").autowire(Instance.reactor);
<cfcomponent hint="I am the database agnostic custom Record object for the user object. I am generated, but not overwritten if I exist. You are safe to edit me."
extends="reactor.project.myproject.Record.userRecord" autowire="true">
<!--- Place custom code here, it will not be overwritten --->
<!--- Autowire dependencies --->
<cfproperty name="adminEmail" inject="coldbox:setting:adminEmail" scope="instance" />
<cfset Instance = StructNew() />
<!--- Get display authorization array or specific value from array index --->
<cffunction name="getUsername" access="public" hint="I get the 'username' value." output="false" returntype="any" _returntype="string">
<!--- This function now has access to the adminEmail setting from the ColdBox config --->
<cfset Instance.adminEmail />
</cffunction>
</cfcomponent>
Inside an event handler you can then use the object just like you would any other Reactor object. Any calls to reactor.createRecord("user") will return a user record automatically injected with the specified properties.
Changelog
v1.1
- Minor performance enhancement to only autowire objects with autowire=true somewhere in object's inheritance chain.
- Original release.
Menu
Forge Entry Types


