3

I understand some of the functionality of Ninject and have been able to use it for IoC.

When I go to add a reference to Ninject to a project in VS2010, using NuGet, I see other Ninject extensions in the list. Specifically Ninject.MVC3. Also on the Ninject website under extensions ( http://www.ninject.org/extensions.html ) I see Ninject.Web.Mvc.

If I am creating MVC3 applications do I need to use this extension of Ninject? Does my basic use of Ninject for IoC with classes/interfaces require anything beyond the standard library?

What is the difference between Ninject and Ninject.MVC3/Ninject.Web.Mvc in an MVC3 project?

seangwright
  • 17,245
  • 6
  • 42
  • 54

1 Answers1

5

ninject.web.mvc is a ninject (core) complement for the ASP MVC (3) applications. Basically - you should use it, when you want to use Ninject in an ASP MVC project.

From the documentation:

This extension allows integration between the Ninject core and ASP.NET MVC projects. To use it, just make your HttpApplication (typically in Global.asax.cs) extend NinjectHttpApplication:

MVC 3 extension contains the crucial methods to wire up the DI composition root into MVC application - it means that Ninject will be responsible for instantiating your controllers, that has dependencies on other components (Ninjects 'overrides' the use of DefaultControllerFactory which is only able to create controllers with parameterless constructors).

In the documentation there are mentioned two methods, how to do it: either extending the NinjectHttpApliaction in global.asax or using the NinjectWebCommon class inside the App_Start folder.

There is also ninject.web.common extension, which is required for ninject.web.mvc. It contains e.g. the definition of InRequestScope.

mipe34
  • 5,596
  • 3
  • 26
  • 38
  • Are you saying that ninject.web.mvc allows ninject to interact with the MVC specific classes/interfaces of an MVC app, whereas standard ninject doesn't support them? I probably need to look more into the various reasons and examples for Ninject handling controllers in an MVC app. – seangwright Mar 31 '13 at 23:25
  • Not really. The key thing is that `ninject.web.mvc` provides extended controller factory that is able to inject dependencies (according to bindings defined in composition root - usually global.asax) in controllers (using the `ninject.core`). So you do not need to somehow pass an instance of ninject `kernel` through your application - which would smell with service locator pattern - and can enjoy the advantages of (constructor) dependency injection. – mipe34 Apr 01 '13 at 18:25