wirebox/binder access from Colbox.cfc and afterConfigurationLoad

I need to “override” an existing Wirebox mapping (currently in Wirebox.cfc) in a coldbox app from Colbox.cfc

I have a client who needs a different Service mapped for their application

i.e in Wirebox.cfc I have:

map(“thisService”).to(“models.thisService”);

and I want to add in Coldbox.cfc

function afterConfigurationLoad() {
wirebox.getBinder().map(“thisService”).to(“customerA.models.thisService”);
}

However, wirebox and coldbox are at this point just Structs as they’re “in” Coldbox.cfc.

How can I “get” access to the wirebox binder?

Thanks,

Tom.

The code snippet you had in there doesn’t work? I would expect WireBox to be fully loaded up and operational in the “afterConfigurationLoad” interceptor point.

Thanks!

~Brad

ColdBox Platform Evangelist
Ortus Solutions, Corp

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

Oh wait, I just saw that you’re adding your interception point directly in the ColdBox config file. There is a draw back to doing this, I don’t think Coldbox.cfc behaves exactly like regular interceptors do that are declared in their file. You’re accessing the configuration structs in the variables scope instead of the injected objects that regular interceptors get access to.

I would put that interceptor in its own CFC and register it as an interceptor in the config. Then it should behave like you expect.

Thanks!

~Brad

ColdBox Platform Evangelist
Ortus Solutions, Corp

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

Yes that’s exactly the issue. Ok - I was hoping there was “another” way to get to coldbox/wirebox, maybe application.wirebox.getBinder() or something like that as I’d have preferred to have kept it in one place.

If I have to separate it into an interceptor it’s not the end of the world.

Thanks Brad.

Tom.

Tom, there is a trick you can do which worked for me for something else.

What I worked on was using the afterInstanceCreation() in an interceptor.

Basically I used this to check the type of request and replace it with the object I wanted, I probably should be using the before creation (if there is one), but this was a quick and dirty solution for the time.

Now, I think having to change the mapping in your case raises a lot more issues as this could affect the other client/customer as well. Using this method is essential a factory pattern, as it checks the conditions and anything else and then loads the required object. Being transient would make this work well, I could see some potential issues with Singletons with this method, in the same way scope widening is an issue.

Now, and of course if the URL (Event) had what identifies the client/customer, this could then be included inside a module that could be excluded to your other customers/clients by using the eventpattern.

I am not sure that fixes your needs, but it may help in looking at it from another angle.

I believe the controller is injected into instance.controller. From there, you should be able to get anything you need.

instance.controller.getWireBox().getBinder().etc()

Thanks!

~Brad

ColdBox Platform Evangelist
Ortus Solutions, Corp

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

Hmm - ok, I’ve gone the interceptor route, but my service isn’t being autowired property.

When I go to use my service, I get variable “xxx” is not defined, even though it is:

i.e - in thisService.cfc I have

property name=“searchService” inject=“id:searchService”;

function test() {
return searchService.get(“user=1”);
}

When I do thisService.test(); I get: Error: variable [SEARCHSERVICE] doesn’t exist

I’m guessing maybe it’s just wiring the new service, but not autowiring the other dependencies within it?

Ok - If I add the mapping into Wirebox, it works. When I try to overwrite the mapping from
within my interceptor, it doesn’t.

Could it be that because the mapping already “exists” and I’m overwriting it, Wirebox already thinks it’s done the dependency injection for that mapping already?

Without seeing your code, I’m having a little troubles following you. If the “old” mapping has already been injected somewhere, updating WireBox don’t affect that-- only new injections going forward.

The error that the variable doesn’t exist though is interesting because typically that means autowiring didn’t run at all. Otherwise, WireBox would have errored since it couldn’t resolve your dependency.

Where are you trying to inject the searchService?

Thanks!

~Brad

ColdBox Platform Evangelist
Ortus Solutions, Corp

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

Perhaps this gist will explain better.

Essentially, I want to “update” an existing wirebox mapping so it uses a different model.

https://gist.github.com/tgmweb/c12151bc4f68746733d5

Thanks,

Tom.

Ahh, so it’s not the custom search service that’s not being found-- it’s the java search service that injected INTO the custom search service that’s not being found!

What version of ColdBox are you using? Is this CB 3.x or CB 4.x with the “java:” namespace registered via the cbjavaloader module?

Thanks!

~Brad

ColdBox Platform Evangelist
Ortus Solutions, Corp

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

Yes exactly!

I just used the java dependency as an example so it was easy to see what was happening / how one service was different to the other.

It doesn’t look like it’s being autowired at all.

The funny thing is, both services actually DO have the same dependency.

If I update the wirebox.cfc config to use the mapping thats in the interceptor, everything works.

So there’s definately something not happening, either because a.) afterConfigurationLoad is the issue, b.) the mapping names being the same has some affect, or c.) the dependency inside the new service has the same name as the old one and it’s not rewiring that one…

Ok - taking a different approach, and using a custom binder and extending the default seems to work, i,e

Basically the same technique I’m using for Coldbox.cfc

Thanks for you help anyway Brad

Tom.