I have a console app that has references to many dlls. The console app needs to instantiate objects in these dlls. The dlls have dependencies that I'd like to inject into their constructors. My requirement is to do all registrations in the console app - it's the composition root. I can get this far, but how can I persist these registrations into the libraries without passing/referencing the UnityContainer?
I want to avoid a service locator pattern and purely use constructor/method injection. None of this, if possible:
_container.Resolve<IData>() // not what I want
Here's what I've got in the console app:
static IUnityContainer _container;
static void Main(string[] args)
{
LoadContainer();
var worker = new Worker(_container.Resolve<IData>()); // I don't like this
worker.Start();
}
private static void LoadContainer()
{
_container = new UnityContainer();
// This will register all types with a ISample/Sample naming convention
_container.RegisterTypes(
AllClasses.FromLoadedAssemblies(),
WithMappings.FromMatchingInterface,
WithName.Default);
}
When I'm in the class Worker, how would I inject dependencies to other class's constructors? In MVC I can easily handle this in the UnityConfig, but can't figure it out for this scenario. I feel that I should be able to bootstrap everything in the console and be done with it. Are there Unity extensions that can help with this?