[coldbox-3.8.1] Wirebox and alias question

This might seem a strange one, y’all, but weigh in. I know that you could (technically) define the same component twice or more in Wirebox with different aliases. What I’m wondering, assuming just one copy, is there any way to ask Wirebox “Hey, you instantiated me…what alias did you assign to me?”

For example, I know I could do this:

map(‘myServiceIronMan’).to(‘model.myService’);

I could do this, but really, really don’t want to:

map(‘myServiceIronMan’).to(‘model.myService’).initArg(‘iocAlias’, ‘myServiceIronMan’);

Instead, is there a way to do the first style, and assuming that I have a pointer to Wirebox inside my object, to say “Wirebox…what name did you create me with?” Or “For my class, what alias do you at least THING I was probably assigned?”

Any ideas?

No, there’s no such functionality. The complication is that an object can come from a full path, scan location, explicit mapping, DSL, builder, custom IOC factory, parent injector, etc. I’m not even sure how you would go about asking WireBox how it created something after the fact. The best you could do would be for WireBox to add some dynamic property to the CFC on creation that showed what was asked for.

Actually, you could do this yourself if you wanted using an interceptor that listens to the afterInstanceCreation event. The intercept data includes the target object as well as the mapping. You could add some metadata yourself at that point.

However, that really breaks ioc as your code isn’t supposed to know where objects came from.

Can you talk to us about the issue you’re trying to solve with this? There might be some other ways to solve it.

Thanks!

~Brad

ColdBox Platform Evangelist
Ortus Solutions, Corp

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

I’m developing a subject-observer system. But I need to support lazy-loading. An object can’t register as an observer if it hasn’t been created. But I know ahead of time what objects want to hear certain messages. If a message gets thrown out there, and I know serviceX needs to hear it, but isn’t awake, I need to load it up via Wirebox. However, if I’ve pre-registered it by its alias, and it wakes up on its own and says “Hey, here I am!”, I can’t be having it get double notifications. When it self-registers, the manager needs to say “Oh, you’re here. Okay, I’ll remove this alias and put you in its place.”

Make sense?

Are you using ColdBox interceptors for this? I’ve used interceptors in large applications that needed decoupling like you’re describing. One use was an online training site that had a number of “events” such as getting training assignments, starting training, completing training, and separate set of actions that were handled by observers such as sending notifications, rolling up completion to courses, archiving assignments. My models and handlers all announced my interception points with custom intercept data (like the user and the training they completed) and the interceptor simply passed the info on to another service (which was autowired in the interceptor) to process that event.

Also, on a side note, ColdBox Interceptors can be called asynchronously too.

If you’re wanting a much larger scale, or want to decouple separate systems, you can also look into message queues. Luis has started doing some cool work with message queues.

Thanks!

~Brad

ColdBox Platform Evangelist
Ortus Solutions, Corp

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

I just added a new interceptor. It checks the created object to see if it has a setIOCAlias() function and if so, takes care of it. (The check is to prevent objects that aren’t in my ServiceLayer from failing on create because they lack this function.)

<cfif structKeyExists(arguments.interceptData.target, ‘setIOCAlias’)>
<cfset arguments.interceptData.target.setIOCAlias(arguments.interceptData.mapping.getName()) />