Skip to content

Ninject in ASP.NET MVC 3

2011 February 17
Posted by Alex Zolotar

For the latest news on Ninject in MVC3 check this link:
Ninject.MVC3 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
Install it.
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:
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.

2 Responses Leave One →
  1. April 29, 2011

    For some cause the picture is just not loading correctly, is presently there an issue?

  2. June 10, 2011

    Wohh precisely what I was searching for, appreciate it for posting .

Leave a Reply