14

In MVC 3 they added a Dependency Resolver what I been using. While answering someone question someone commented on you should use the Ninject MVC 3 plugin.

So my question is why use it over the built in one? If it is the way to go how do you setup it up?

Question

So above is the link to the question that I answered.

Community
  • 1
  • 1
chobo2
  • 83,322
  • 195
  • 530
  • 832

2 Answers2

15

ASP.NET MVC 3 provides a dependency injection service which will hook into whatever dependency resolver you choose to implement. The Ninject MVC 3 plugin is very simple in its function because all it must do is implement the type-resolution methods defined in System.Web.Mvc.IDependencyResolver and call the appropriate Ninject methods to return a requested type.

Whether you choose to use your own IDependencyResolver and map it to Ninject (or any other dependency injection framework), or you choose to use the freely available Ninject MVC 3 plugin, is mostly a trivial distinction.

Here's a fully-functional example of what a hand-rolled, Ninject-compatible IDependencyResolver might look like. The Ninject MVC 3 plugin would be fundamentally very similar:

public class NinjectDependencyResolver : IDependencyResolver
{
    private readonly IKernel _kernel;

    public NinjectDependencyResolver(IKernel kernel) {
        _kernel = kernel;
    }

    public object GetService(Type serviceType) {
        return _kernel.TryGet(serviceType, new IParameter[0]);
    }

    public IEnumerable<object> GetServices(Type serviceType) {
        return _kernel.GetAll(serviceType, new IParameter[0]);
    }
}

The key point here is that ASP.NET MVC does not provide a full-fledged dependency injection framework; it provides only the layer needed to retrieve a required type instance by way of an IoC container (i.e. Ninject) at specific points throughout the ASP.NET MVC request pipeline (Controller resolution, View resolution, etc).

Note: if any of the terminology I used isn't quite accurate, please inform me.

Nathan Taylor
  • 24,423
  • 19
  • 99
  • 156
  • That looks pretty much what I have now(see the other post). I heard(or I think I heard) that doing it yourself like this without the mvc plugin won't kill sessions when using nhibernate. – chobo2 Mar 01 '11 at 20:56
  • @chobo2 I haven't really got any experience with nHibernate or the problem you're describing, so I can't speak to it specifically. :[ – Nathan Taylor Mar 01 '11 at 21:53
  • @chobo2, perhaps what you're referring to is Ninject's `OnePerRequestModule` this will wipe out anything bound InRequestScope as soon as the Request is over. It will also execute before any other EndRequest handlers are run. Unfortunately doing this yourself will not prevent this from happening. – Vadim Mar 01 '11 at 23:11
13

The Ninject.Web.MVC extension (or Ninject.MVC3 NuGet package) use a dependency resolver internally too. So basically it is using the same mechanism. But there are several reasons to use the extension rather than implementing an own dependency resolver:

  1. Why implementing an own dependency resolver when there's already an extension doing exactly the same? Using the same implementation than others makes it much easier to support you when you have a problem. Furthermore the more using the same implementation the more stable it gets. (See point 4).
  2. The extension is more than just a dependency resolver. See http://www.planetgeek.ch/2010/11/13/official-ninject-mvc-extension-gets-support-for-mvc3/ for a list of all features that come with the extension.
  3. It adds support for fast deactivation of objects InRequestScope after the request ends by default. This prevents that applications with a heavy load run into an OutOfMemory exception.
  4. The dependency resolver in your post and the one above both have a problem. In some situations under heavy load your application will crash and only display yellow pages of death until the application is restarted. I don't like to answers all the question that will come in future only because a faulty dependency resolver is used. Add at least a .ToList() to the GetServices
  5. Support for InRequestScope will be removed in Ninject 2.4 to remove the dependency to System.Web to reduce the number of build targets. This is a breaking change. But projects based on one of the Web extensions will only need a very minimalistic change to get it running again. InRequestScope will still be available to projects using one of these extensions. Custom implementations will have to add support themselves.
Remo Gloor
  • 32,665
  • 4
  • 68
  • 98
  • @Remo Gloor - Whats the difference between Web.MVc extension and Ninject.MVC3? – chobo2 Mar 15 '11 at 23:17
  • It's the same. We keeped the name of the NuGet package Ninject.MVC3 as it existed before. – Remo Gloor Mar 16 '11 at 02:01
  • @ Remo Gloor - So it is the same thing then? – chobo2 Mar 16 '11 at 22:32
  • @ Remo Gloor - How much of my code that I already have needs to be changed? – chobo2 Mar 16 '11 at 22:33
  • None. Just remove the App_Start part added by the nuget package See https://github.com/ninject/ninject.web.mvc/wiki/Setting-up-an-MVC3-application – Remo Gloor Mar 17 '11 at 00:19
  • @Remo Gloor - I am looking at it and it creates a bootstrapper(according to the github link). I never needed that in mine and I am not too found of it so do I really need it? – chobo2 Mar 17 '11 at 14:15
  • Repeating: Just delete the App_Start folder. – Remo Gloor Mar 17 '11 at 15:10
  • @Remo Gloor - Ok switched over. Is there a difference between what I had and what was in the app_start? – chobo2 Mar 17 '11 at 18:04
  • Sry seems I missed that comment: There is no difference. That are just two ways of starting an MVC application using the extension. – Remo Gloor Apr 01 '11 at 15:13
  • @Remo Gloor - why it is it being removed? I don't think I am using it but I will have to check. I always forget what most of the scope ones do. – chobo2 Apr 01 '11 at 19:06
  • Updated point 5: We want reduce the number of build targets. InRequestScope simply doesn't belong to Ninject core as it is not only for web projects. – Remo Gloor Apr 01 '11 at 19:29
  • @ Remo Gloor - Ah I do use it. So can you point me to what I will need to keep it going once it changes? Also do you have a list of the scopes that will be available in 2.4 and what each one does. – chobo2 Apr 01 '11 at 19:42
  • @chobo2: Be a little patience. We will update the docu and write blog posts before the release explaining everything. – Remo Gloor Apr 01 '11 at 22:10
  • @Remo Gloor - Ok can you edit this post once they become available? – chobo2 Apr 01 '11 at 22:34
  • I started a new project and I am using Ninject MVC 3. I see no InRequestScope. What do I use? Or where do I get InRequestScope? – chobo2 Aug 22 '12 at 18:54