I’m using Ninject because of the super-slick integration with ASP.NET MVC 3 via NuGet. It gives my a “correct” and easy to start with solution. The main part in this found in this App_Start static file, that is run before any other code with the Web Activator mechanism:
And converting that to Unity would be something like this:
Yup – actually that’s all it’s too it. For a while there I was “worried” that I would have to implement a ServiceLocator (line 22 above). That’s not to hard since just the IDependencyResolver interface that is needed to be implemented (2 methods). But I just so happens that Unity implemented that for me already with the UnityServiceLocator.
Actually I can now easily switch between the two dependency injection frameworks (why you want to do that I don’t know, but it’s cool), by simply commenting out the Web Activator attribute at the top of the class:
[assembly: WebActivator.PreApplicationStartMethod(typeof(Web.AppStart_Unity), "Start")]
So there you have it. Another area where ASP.NET MVC 3 is easier than you think.