Your question is
"how to integrate a different DI tool (like SimpleInjector, Unity or
Autofac) into an Onion Project?"
I’m using StructureMap instead of Ninject, the way it’s integrated should be ok for any other DI framework.
As you said, only the Dependency Resolution Layer should reference all the other layers, it’s the outermost layer of your Onion Architecture. Well, to do so, I’ve created a project called BootStrapper. This is the only project where I reference StructureMap assemblies.
In the App_Start folder of this project, I have a file named StructureMapMvc.cs which looks like this:
[assembly: WebActivator.PreApplicationStartMethod(typeof(XXXX.BootStrapper.App_Start.StructuremapMvc), "Start")]
namespace XXXX.BootStrapper.App_Start
{
public static class StructuremapMvc
{
public static void Start()
{
IContainer container = IoC.Initialize();
System.Web.Mvc.DependencyResolver.SetResolver(new StructureMapDependencyResolver(container));
GlobalConfiguration.Configuration.DependencyResolver = new StructureMapHttpDependencyResolver(container);
ControllerBuilder.Current.SetControllerFactory(new StructureMapControllerFactory());
}
}
}
The interesting line is:
[assembly: WebActivator.PreApplicationStartMethod(typeof(XXXX.BootStrapper.App_Start.StructuremapMvc), "Start")]
According to the nugget package’s description:
WebActivator is a NuGet package that allows other packages to execute
some startup code in web apps.
Pretty cool, huh? The last thing you have to put in place is to be sure that the BootStrapper project assembly will be pushed to the /bin folder of your web application (easy to set up using a post build action or OutputTo nugget package). This will avoid you to reference the BootStrapper project in your MVC project and break the Onion Architecture principle.
So, with all this in place, it adheres totally with the Composition Root Pattern and when your app will start, modules will be composed together.
Hope this helps!