Dependency Injection with ninject.mvc3 nuget

29th Jun 2012

Quick post, just because this is mega easy.

Traditionally, adding dependency injection with any standard injection package to an MVC solution would normally mean that you would have to write a Controller Factory and wire it up in your Application_Start() method.

If you want to add dependency injection into an MVC solution, just add one of the pre-baked nuget packages that does the MVC wire up for you:

It's just much easier - these packages contain code that has done the legwork of wiring up a custom controller factory for you.

Looking specifically at the Ninject.MVC3 package, simply add the NuGet reference. This will add a "NinjectWebCommon.cs" file into your "App_Start" folder:

You can then edit the RegisterServices(IKernel kernel) method to configure your bindings:

private static void RegisterServices(IKernel kernel) 
{
kernel.Bind<IWorkRepository>().To<WorkRepository>();
}