Create object with DI from a class outside of ColdBox

From within a handler I am using getModel to create an object. During
the life of that object, it needs to create another object that relies
on DI and the ColdBox DSL to do its job. I can't call getModel from
within the first object because it doesn't have that method. I can't
instantiate a new WireBox and call getInstance because that WireBox
instance is independent of the ColdBox and the DSL is unavailable.
Any ideas one how I could approach this?

This is a specific situation. To generalize, how would you invoke
methods that are available in handlers and other parts of the
framework from within a model or other class that does not subclass a
framework class?

I’ll call the model you are creating within the handler using getModel() “modelA” and “modelB” will be the second object that needs to be created within ModelA.

If ModelB is a singleton, this is quite easy. Just add a property name=“ModelB” inject; into ModelA, If you need ModelB to be a transient then you can inject wirebox directly into ModelA using property name=“wirebox” inject=“wirebox”;.

One of those two solutions should work for you. If not, you can always grab it from the application scope. But, I wont encourage that here :slight_smile:

Does this response help you?

Thanks,

Aaron Greenlee

By grab “it” from the application scope I was referring to WireBox.

-A

Michael,

This problem lies with any model layer as it should be separate. However, the DI engine gives you the capability to inject itself so you can leverage any objects from within any object. You can do this in several ways:

property name=“factory” inject=“wirebox”;

That will inject the factory itself so you can call getInstance() on the factory.

Luis F. Majano
CEO
Ortus Solutions, Corp
www.ortussolutions.com

ColdBox Platform: http://www.coldbox.org
Linked In: http://www.linkedin.com/pub/3/731/483
Blog: http://www.luismajano.com
IECFUG Manager: http://www.iecfug.com

Social: twitter.com/lmajano facebook.com/lmajano

This will most likely fit my needs, but I'm tripping over the syntax.
I have this declaration:

<cfproperty name="wirebox"
    inject="wirebox"
    scope="variables"
    autowire="true" />

And I'm using it later on like this:
<cfset service = wirebox.getInstance(x)>

But that line errors out complaining that wirebox does not exist. I'm
able to inject other properties just fine. For example:

<cfproperty name="downloadDirectoryPath"
    type="string"
    inject="coldbox:setting:downloadDirectoryPath"
    scope="this"
    autowire="true" />

Is wirebox the correct DSL value to use? I don't see it in the DSL
documentation.
http://ortus.svnrepository.com/coldbox/trac.cgi/wiki/cbModelGuide

I hate to bump a thread like this, but does anyone know what the
proper syntax would be for this?

Since I couldn't work out the DSL syntax, I ended up ditching wirebox.
To work around it, during the CB initialization (the environment
functions, to be specific) I'm initializing what amounts to static
properties on the classes I care about:

getComponentMetaData("package.name.ClassName").static = {};
getComponentMetaData("package.name.ClassName").static.x = "x";

To create instances I used plain old ordinary
createObject("component","package.name.ClassName")

And then to access the properties in the individual CFCs I have

getMetadata(this).static.x

Since this uses createObject instead of WireBox, this works for any
class, regardless of if it is in a directory WireBox knows to watch or
if the instance being created is done in a class that does not have
access to WireBox.

Hmm, not sure If I would agree with you on the approach to tell you the truth. Then why use a DI engine then?

Luis F. Majano
CEO
Ortus Solutions, Corp
www.ortussolutions.com

ColdBox Platform: http://www.coldbox.org
Linked In: http://www.linkedin.com/pub/3/731/483
Blog: http://www.luismajano.com
IECFUG Manager: http://www.iecfug.com

Social: twitter.com/lmajano facebook.com/lmajano