BoxLang 🚀 A New JVM Dynamic Language Learn More...

Reactor factory with autowire support

v1.1 Projects
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()
  1. Save ReactorFactory.cfc somewhere inside your app (ex. /model/reactor/ReactorFactory.cfc)
  2. This component extends the original ReactorFactory object, so modify the extends attribute if Reactor isn't installed in the default location (reactor.reactorFactory).
  3. 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"
}
}
It's also necessary to make a few changes to your onAppInit() ColdBox event:
  1. Add autowire="true" to the <cfcomponent> tag of the event handler containing your onAppInit() method (usually Main.cfc)
  2. Add the following code after the opening <cfcomponent> line:
    <cfproperty name="reactor" inject="ocm" scope="instance" />
    <cfset Instance = StructNew() />
  3. Add the following line at the top of your onAppInit() event method code:
    getPlugin("beanFactory").autowire(Instance.reactor);
That's it! Now you can add autowire="true" to your Reactor objects and they will be automatically injected upon creation. Here is an example Reactor record that's injected with a ColdBox config custom setting:

<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.

v1.1
  • Minor performance enhancement to only autowire objects with autowire=true somewhere in object's inheritance chain.
v1.0
  • Original release.

$ box install Reactor-factory-with-autowire-support

No collaborators yet.
 
  • {{ getFullDate("2011-03-30T18:22:32Z") }}
  • {{ getFullDate("2016-06-10T09:51:20Z") }}
  • 4,601
  • 3,190