Blog

27
February 2014

Gavin Pickin

WireBox - Possible Gotcha with Binder Config File Settings and using Full CFC Path

CFML Language, ColdBox, Techie Gotchas, WireBox

My last post, I looked at Installing ColdBox's WireBox Standalone library into one of my legacy sites. We got the Core downloaded, mapped, we created an Injector, and looked at how to create your Objects using the Injector. We even looked at using Annotations and the Binder Config file to tell WireBox how you wanted those Objects wired.

At the end, we came across one of thos gotchas. When you use the Binder, whether you explicitly map an alias, or not, WireBox by convention creates an Alias for you... matching the name of the CFC... in our case "Theme". So when you use the following code, you get an interesting side effect.

<cfset ThemeService = application.injector.getInstance("CFCs.Theme")>

Wirebox looks through its list of CFCs mapped, and looks for CFCs.Theme… and it cannot find it in the list of Aliases, so it looks for the full path, and sets it up, but since there is no matched Aliases / Mapped settings, it does not set it up with the Binder Config, it uses annotations, if any, and thats it.

But wait? We did set it up, didn't we? Yes… we did, but, by default, Wirebox maps it with the Alias, and it does not use the path to check if that exists. I talked with Brad, and it seems like I'm one of the few who found this… because usually when you map a CFC, you would refactor your code to use the Alias, because Aliases are awesome, and usually that isn't a problem.

Don't get me wrong, refactoring is great, using an Alias is awesome, you can move your CFCs around, reorganize them, change the path in the binder, and it still works… but what about those people with 10000s of files, and refactoring is a nightmare? I spoke with Brad, and discussed a couple of simple fixes.

  1. Add an alias, which is the full path as an annotation
  2. Add an alias, which is the full path in the binder
  3. Write a plugin that looks for beforeInstanceInspection or afterInstanceInspection and adds the full path as an alias on the fly
  4. Make a pull request on Wirebox to do it automatically
  5. Bribe someone else to do it and make a pull request

1 and 2 are easy, and work great in this example. Its much easier than refactoring all of your code calling the injector, but in all reality, if you are installing Wirebox, and refactoring the injector in, you could just map all your CFCs at the same time, and use Aliases to begin with. 

1 - Add an alias, which is the full path as an annotation

In your component declaration or cfcomponent tag, just add this 

alias="CFCs.Theme"

 

And now, you have an alias, so your full path call will find the Mapped version, with that name. What if you want to use Theme as well as the full Path? Just do this

alias="Theme,CFCs.Theme.ThemeService" 

 

You have have multiple names/aliases, which will make this an easier transition... which will allow WireBox to match the full path, with the alias and the settings in the binder.

2 - Add an alias, which is the full path in the binder

Just like we did with the annotation, you can easily get around this with the binder config, by setting the mapped aliases there. Remember, when you map something to a CFC, the map is the aliasing part. So this would work.

component extends="coldbox.system.ioc.config.Binder"{

    function configure(){

        map("Theme,CFCs.Theme,ThemeService")
            .to("CFCs.theme")
            .asSingleton();

    }
}

 

Simple fix, now when you getInstance("Theme"), or getInstance("CFCs.Theme") or getInstance("ThemeService") all of them will work, and they will respect all annotations, and Binder config settings, just remember, Binder Config overrules the annotations.

Of course, if you are refactoring all of your code to use application.injector.getInstance("Full.Path"), you might as well just use application.injector.getInstance("Alias") while you're at it.

 

Hope this helps someone, I know I'll be using them as I slowly refactor.
Thanks for reading,

Gavin

by George Murphy
03/15/2014 07:25:12 PM

Nice post Gavin, curious if you put your app settings inside of wirebox and how you set it up?

Blog Search