For a recent project at work, I had the opportunity to use TinyIoC, a single-file IoC container implementation, for a command line program used for quality assurance testing of images. The QA testing itself was in a separate assembly, written for an earlier, WPF application that used MvvmLight and SimpleIoC. The thing is, the assembly which actually did the work was written with service location and dependency injection in mind. I didn’t want to have to add MvvmLight to the command line app just to get SimpleIoC, so I added TinyIoC via NuGet instead, in hopes that I could just use that.
The thing is, that work assembly was relying on Common Service Locator (i.e. Microsoft.Practices.ServiceLocation) to get services both from itself and provided by the assemblies that used it. And TinyIoC doesn’t come with an adapter for Common Service Locator. Fortunately, writing up an adapter isn’t that difficult; you merely need to supply a class implementing
IServiceLocator and have that class map the interface’s calls to methods on your container. The following Gist provides an adapter implementation for TinyIoC:
There’s not much else to it but this. Of course, to actually put it to use, you’ll need some code to let CSL know to use
TinyIoCServiceLocator – and, of course, register your services with the container:
Voila, TinyIoC and Common Service Locator, hand in hand for your next application.