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

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

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.

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 . I’m kind of guessing it’s the latter.

All you should need is this:

Here’s the docs.
http://wiki.coldbox.org/wiki/WireBox.cfm#WireBox_Namespace

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: brad@coldbox.org
ColdBox Platform: http://www.coldbox.org
Blog: http://www.codersrevolution.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.

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.

injector.getInstance(“person”);

Thanks!

~Brad

ColdBox Platform Evangelist
Ortus Solutions, Corp

E-mail: brad@coldbox.org
ColdBox Platform: http://www.coldbox.org
Blog: http://www.codersrevolution.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) :slight_smile:

Yep - that worked like a charm. P.S. thanks for the quick response.

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.

Thanks!

~Brad

ColdBox Platform Evangelist
Ortus Solutions, Corp

E-mail: brad@coldbox.org
ColdBox Platform: http://www.coldbox.org
Blog: http://www.codersrevolution.com