Create object with DI from a class outside of ColdBox

21 views
Skip to first unread message

Michael VanDaniker

unread,
Jan 30, 2012, 2:15:07 PM1/30/12
to ColdBox Platform
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?

Aaron Greenlee

unread,
Feb 1, 2012, 4:46:02 PM2/1/12
to col...@googlegroups.com
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 :)

Does this response help you?

Thanks,

Aaron Greenlee

Aaron Greenlee

unread,
Feb 1, 2012, 4:46:41 PM2/1/12
to col...@googlegroups.com
By grab "it" from the application scope I was referring to WireBox.

-A

Luis Majano

unread,
Feb 1, 2012, 5:23:29 PM2/1/12
to col...@googlegroups.com
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.

--
You received this message because you are subscribed to the Google Groups "ColdBox Platform" group.
To post to this group, send email to col...@googlegroups.com
To unsubscribe from this group, send email to coldbox-u...@googlegroups.com
For more options, visit this group at http://groups-beta.google.com/group/coldbox
For News, visit http://blog.coldbox.org
For Documentation, visit http://wiki.coldbox.org

Michael VanDaniker

unread,
Feb 9, 2012, 1:17:23 PM2/9/12
to ColdBox Platform
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


On Feb 1, 5:23 pm, Luis Majano <lmaj...@gmail.com> wrote:
> 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, Corpwww.ortussolutions.com(http://www.ortussolutions.com)
> Social: twitter.com/lmajano (http://twitter.com/lmajano) facebook.com/lmajano (http://facebook.com/lmajano)
>
>
>
>
>
>
>
> On Monday, January 30, 2012 at 11:15 AM, Michael VanDaniker wrote:
> > 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?
>
> > --
> > You received this message because you are subscribed to the Google Groups "ColdBox Platform" group.
> > To post to this group, send email to col...@googlegroups.com (mailto:col...@googlegroups.com)
> > To unsubscribe from this group, send email to coldbox-u...@googlegroups.com (mailto:coldbox-u...@googlegroups.com)

Michael VanDaniker

unread,
Feb 21, 2012, 8:58:56 AM2/21/12
to ColdBox Platform
I hate to bump a thread like this, but does anyone know what the
proper syntax would be for this?

On Feb 9, 1:17 pm, Michael VanDaniker <mvand...@gmail.com> wrote:
> 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 thatwireboxdoes not exist. I'm
> able toinjectother properties just fine. For example:
>
> <cfproperty name="downloadDirectoryPath"
>                 type="string"
>                inject="coldbox:setting:downloadDirectoryPath"
>                 scope="this"
>                 autowire="true" />
>
> Iswireboxthe correct DSL value to use? I don't see it in the DSL
> documentation.http://ortus.svnrepository.com/coldbox/trac.cgi/wiki/cbModelGuide
>
> On Feb 1, 5:23 pm, Luis Majano <lmaj...@gmail.com> wrote:
>
>
>
>
>
>
>
> > Michael,
>
> > This problem lies with any model layer as it should be separate.  However, the DI engine gives you the capability toinjectitself so you can leverage any objects from within any object.  You can do this in several ways:
>
> > property name="factory"inject="wirebox";
>
> > That willinjectthe factory itself so you can call getInstance() on the factory.

Michael VanDaniker

unread,
Mar 14, 2012, 9:28:59 AM3/14/12
to ColdBox Platform
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.

On Feb 21, 9:58 am, Michael VanDaniker <mvand...@gmail.com> wrote:
> I hate to bump a thread like this, but does anyone know what the
> proper syntax would be for this?
>

Luis Majano

unread,
Mar 14, 2012, 5:11:13 PM3/14/12
to col...@googlegroups.com
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

Reply all
Reply to author
Forward
0 new messages