[coldbox-3.6.0] Injection metadata and/or mapping to inject scopeRegistration instance of WireBox

48 views
Skip to first unread message

jinglesthula

unread,
Jun 17, 2013, 4:28:07 PM6/17/13
to col...@googlegroups.com
I have the following in my WireBox.cfc in the (wireBox struct):

scopeRegistration = {
enabled = true,
scope   = "application", // server, cluster, session, application
key = "myHappyLilWireBox"
}

What I want is that when I have components with the following injection metadata at the top
<cfproperty name="injector" inject />
that this will end up injecting the instance of WireBox that has already been registered in the application scope.

I know I can do mapPath("coldbox.system.ioc.Injector").asSingleton(); later on in my Wirebox.cfc, but
a) I don't know if this will get init() -ed with my WireBox.cfc binder and
b) I know 2 is better than n, but I'd like to just have a single instance hanging around

Perhaps I'm misunderstanding and Wirebox knows that if it's being asked for an instance of itself it will use the application scope one instead of creating a separate one for the singleton internal scope.  Not sure.


jinglesthula

unread,
Jun 17, 2013, 4:36:58 PM6/17/13
to col...@googlegroups.com
eh, I think I am misunderstanding.  I have an instance of wirebox injected in one of my models and when I ask it for an instance of something mapped in my binder it knows all about it.  So somehow there's got to be a shared reference to the mappings that are set up in my binder, because the mapPath("coldbox.system.ios.Injector").asSingleton() line of code doesn't have any reference to the binder, so somewhere, somehow the injected instance of WireBox is finding out about the mappings given to the instance created when ColdBox is initialized, or is being smart enough to return a reference to itself when it sees <cfproperty name="injector" inject />.  I'm kind of guessing it's the latter.

br...@bradwood.com

unread,
Jun 17, 2013, 4:38:39 PM6/17/13
to col...@googlegroups.com
All you should need is this:
 
<cfproperty name="wirebox" inject="wirebox">
 
Here's the docs.
 
You don't want to create a mapping, or you will be asking WireBox to create a second instance of itself.  WireBox is already self-aware, you just need to use the "wirebox" namespace.
 
Also, I'm curious if there's a reason to change the key in the config for your WireBox registration.  Are you planning on having multiple WireBox injectors in your app?

Thanks!

~Brad

ColdBox Platform Evangelist
Ortus Solutions, Corp

E-mail: br...@coldbox.org
ColdBox Platform: http://www.coldbox.org
Blog: http://www.codersrevolution.com 
 
 
--------- Original Message ---------
Subject: [coldbox:18614] [coldbox-3.6.0] Injection metadata and/or mapping to inject scopeRegistration instance of WireBox
From: "jinglesthula" <mithlon...@gmail.com>
Date: 6/17/13 3:28 pm
To: col...@googlegroups.com

I have the following in my WireBox.cfc in the (wireBox struct):
 
scopeRegistration = {
enabled = true,
scope   = "application", // server, cluster, session, application
key= "myHappyLilWireBox"
}
 
What I want is that when I have components with the following injection metadata at the top
<cfproperty name="injector" inject />
that this will end up injecting the instance of WireBox that has already been registered in the application scope.
 
I know I can do mapPath("coldbox.system.ioc.Injector").asSingleton(); later on in my Wirebox.cfc, but
a) I don't know if this will get init() -ed with my WireBox.cfc binder and
b) I know 2 is better than n, but I'd like to just have a single instance hanging around
 
Perhaps I'm misunderstanding and Wirebox knows that if it's being asked for an instance of itself it will use the application scope one instead of creating a separate one for the singleton internal scope.  Not sure.
 
 

 

--
--
You received this message because you are subscribed to the Google Groups "ColdBox Platform" group.
For News, visit http://blog.coldbox.org
For Documentation, visit http://wiki.coldbox.org
For Bug Reports, visit https://ortussolutions.atlassian.net/browse/COLDBOX
---
You received this message because you are subscribed to the Google Groups "ColdBox Platform" group.
To unsubscribe from this group and stop receiving emails from it, send an email to coldbox+u...@googlegroups.com.
To post to this group, send email to col...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

br...@bradwood.com

unread,
Jun 17, 2013, 4:45:04 PM6/17/13
to col...@googlegroups.com
The wirebox mapping you are creating has no binder because you're not passing it one.  You don't want to do that.  The ColdBox framework has already created WireBox for you and initialized it correctly with the binder config.
 
Here's the rule of thumb-- if you want to inject a singleton (like a service, a DAO, or LogBox) somewhere (like into a handler, interceptor, or model) just reference it.  WireBox will provide the reference when creating the object and you don't need to inject a reference to the actual injector.
 
 <cfproperty name="myService" inject="myService">

If you are wanting to turn an object of your own (like a service) into a factory so it can create other objects (usually transients), then inject a reference to the WireBox injector like you are trying and ask the injector for the instances as needed.
 
 <cfproperty name="injector" inject="WireBox">
 
injector.getInstance("person");

Thanks!

~Brad

ColdBox Platform Evangelist
Ortus Solutions, Corp

E-mail: br...@coldbox.org
ColdBox Platform: http://www.coldbox.org
Blog: http://www.codersrevolution.com 
 
 
--------- Original Message ---------
Subject: [coldbox:18616] Re: [coldbox-3.6.0] Injection metadata and/or mapping to inject scopeRegistration instance of WireBox
From: "jinglesthula" <mithlon...@gmail.com>
Date: 6/17/13 3:36 pm
To: col...@googlegroups.com

eh, I think I am misunderstanding.  I have an instance of wirebox injected in one of my models and when I ask it for an instance of something mapped in my binder it knows all about it.  So somehow there's got to be a shared reference to the mappings that are set up in my binder, because the mapPath("coldbox.system.ios.Injector").asSingleton() line of code doesn't have any reference to the binder, so somewhere, somehow the injected instance of WireBox is finding out about the mappings given to the instance created when ColdBox is initialized, or is being smart enough to return a reference to itself when it sees <cfproperty name="injector" inject />.  I'm kind of guessing it's the latter.

On Monday, June 17, 2013 2:28:07 PM UTC-6, jinglesthula wrote:
I have the following in my WireBox.cfc in the (wireBox struct):
 
scopeRegistration = {
enabled = true,
scope   = "application", // server, cluster, session, application
key= "myHappyLilWireBox"
}
 
What I want is that when I have components with the following injection metadata at the top
<cfproperty name="injector" inject />
that this will end up injecting the instance of WireBox that has already been registered in the application scope.
 
I know I can do mapPath("coldbox.system.ioc.Injector").asSingleton(); later on in my Wirebox.cfc, but
a) I don't know if this will get init() -ed with my WireBox.cfc binder and
b) I know 2 is better than n, but I'd like to just have a single instance hanging around
 
Perhaps I'm misunderstanding and Wirebox knows that if it's being asked for an instance of itself it will use the application scope one instead of creating a separate one for the singleton internal scope.  Not sure.
 
 

 

--

jinglesthula

unread,
Jun 17, 2013, 5:11:55 PM6/17/13
to col...@googlegroups.com
Right - for services we want injected we are doing it with cfproperty, and are only using the injector directly when we call getInstance for our transients.  

To answer your previous question about multiple wirebox instances, yes - we have 2 separate instances with separate bindings.  The reason is our code base is so huge we have to migrate to ColdBox a bit at a time.  The way we're managing the task and keeping our existing code working is to extend our non-CB models from the CB ones and allow a separate (standalone) instance of wirebox with different mappings handle the injection when our non-CB code makes calls in to the CB model superclass.  It's fairly mind-bending, but the way our old code is structured it's the only way we can keep the thing running while we convert to the CB framework.

Thanks for pointing me to the WireBox namespace part in the docs - I'd missed that in my reading (or else just didn't have the mental model built enough to know where to put that piece of info when I did read it) :)

jinglesthula

unread,
Jun 17, 2013, 5:19:40 PM6/17/13
to col...@googlegroups.com
Yep - that worked like a charm.  P.S. thanks for the quick response.


On Monday, June 17, 2013 2:45:04 PM UTC-6, Brad Wood wrote:

br...@bradwood.com

unread,
Jun 17, 2013, 5:23:24 PM6/17/13
to col...@googlegroups.com
That makes sense.  If you need to use the ColdBox and non-ColdBox version of your models at the same time, that is probably the most straightforward way.  If you are only using a model in one place OR the other, then you could set up one injector to the be "parent" of the other injector.  Then, always ask the child injector for your models, and if it can't find a model by that ID, it will ask the parent injector for it automatically.  That could be useful if you are converting models over from one injector to the other as you wouldn't need to touch your code at all when moving them.
Reply all
Reply to author
Forward
0 new messages