Inject Service Via Dynamic Service Name

I am fairly new to ColdBox but have been coding in ColdFusion for over 15 years. One of the feature I love about ColdBox is the injection of service via WireBox especially the following command.
property name=“service” inject=“service”;

Anyways, is there a way I can inject a service dynamically by changing the inject value with a variable? For example, for an event the service I would like to inject could change based on the value passed in by the user. I probably could just do a CreateObject but was curious if an injection could be done via Wirebox. Thanks and looking forward to the response.

Firstly, I don’t think dynamic properties are possible. If they were, it wouldn’t work because they are only parsed once and WireBox then caches the metadata of the component being injected into.

Now, that’s being said, there’s certainly many ways to accomplish what you are needing. The first approach would be to simply inject WireBox itself and then dynamically call wirebox.getInstance() to get the service that you need on-the-fly. Just make sure you store it in a local variable so you don’t have scoping issues. Perhaps you can share more about what you are trying to do.

There should not be a need to ever use CreateObject in your code. Assuming the service itself has dependencies of its own that need wired in, you’ll always want to use your friendly neighborhood Object Factory, WireBox to handle creation for you so you get the benefits of autowiring and persistence.

Thanks!

~Brad

ColdBox Platform Evangelist
Ortus Solutions, Corp

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

Jae

For this you will need to inject wirebox itself and then request the instance as it can change.

Property name=“wirebox” inject=“wirebox”;

Then just use it

Wirebox.getInstance( varname )

Brad and Luis, thank you. Your suggestion worked perfectly. Just realized I have being using Wirebox to getInstance. Sometimes too consumed with the project overlooked the answer when it was right in front of me. Thanks again.