download   home   source


richard@richarddavies.us's Gravatar   Richard Davies Mar 30, 2011 at 6:22 PM May 23, 2013 at 7:46 PM 1.1 3.0 Model Objects 804 382

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

  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.

Changelog

v1.1

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