Ninject in ASP.NET MVC 3
For the latest news on Ninject in MVC3 check this link:
Ninject.MVC3 22.214.171.124 released
Thanks to Microsoft IDependencyResolver DI in MVC 3 became a breeze.
Create new MVC 3 project in your Visual studio 2010.
Right click project and choose “Add Library Package Reference” from the menu (or click Project from the VS main menu and choose it from there).
This will open NuGet package, type “Ninject.MVC3″, you’ll see:
“Ninject.MVC3″ An MVC3 IDependencyResolver wrapper for ninject Created by : Microsoft
It will create AppStart_NinjectMVC3 file for you.
Open it, in RegisterServices(IKernel kernel) replace default placeholder with your interface to repository binding.
You are done!
As usual in your controller use something like that:
private readonly IRepository _reposit;
public HomeController(IRepository reposit)
_reposit = reposit;
For a full blown implementation on Ninject in mvc 3 check this link: http://www.planetgeek.ch/2010/11/13/official-ninject-mvc-extension-gets-support-for-mvc3/
Otherwise if you are, just like me only want to inject dependency into your controllers, use the method described above.
Behind the scene NuGet has added three references into your project:
Ninject dll, NinjectMVC3 wrapper and WebActivator.
WebActivator will call PreApplicationStartMethod which would execute Start() method inside AppStart_NinjectMVC3 file.
No changes are required in your Global.asax.