Decaying Code

Where code comes to die

Using Ninject with ASP.NET MVC 3 dependency injection

Ninject is my favorite container for doing dependency injection. However, it always seemed a bit complicated to use with ASP.NET WebForms.

Things have improved with ASP.NET MVC 1 of course but now, with the release of ASP.NET MVC 3, things have become way easier to do.

Here is what my Global.asax.cs looks like when I want to register an IoC tool for ASP.NET MVC 3:

protected void Application_Start()
{
    AreaRegistration.RegisterAllAreas();

    RegisterGlobalFilters(GlobalFilters.Filters);
    RegisterRoutes(RouteTable.Routes);
    RegisterDependencyResolver();
}

private void RegisterDependencyResolver()
{
    var kernel = new StandardKernel();

    DependencyResolver.SetResolver(new NinjectDependencyResolver(kernel));
}

Well that look easy enough no? Well, to be fair… NinjectDependencyResolver doesn’t exist. It’s a custom class that I created to allow the easy mapping of the Kernel.

The only thing it needs is the Ninject StandardKernel. Once all the mappings are done, we give the kernel to the resolver and let MVC handle the rest.

Here is the code for NinjectDependencyResolver for all of you to use:

public class NinjectDependencyResolver : IDependencyResolver
{
    private readonly IKernel _kernel;

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

    public object GetService(Type serviceType)
    {
        return _kernel.TryGet(serviceType);
    }

    public IEnumerable<object> GetServices(Type serviceType)
    {
        try
        {
            return _kernel.GetAll(serviceType);
        }
        catch (Exception)
        {
            return new List<object>();
        }
    }
}

Comments (5) -

In "add Library Package Reference" have package "Ninject.MVC3"

Reply

Does it have a Dependency Resolver for ASP.NET MVC 3? I looked into Ninject.Web.Mvc and it didn't seem to have anything related to that.

Reply

"Ninject.MVC3" is nuGet package.
Description :
"An MVC3 IDependencyResolver wrapper for ninject"
Created by : Microsoft

Dependency resolver configuration in file : AppStart_NinjectMVC3.cs

Reply

@Francis; Alas, Ninject.MVC3 has been removed from NuGet; You now have to use Ninject.Web.Mvc3 which does _not_ include the NinjectServiceLocator class or the AppStart_NinjectMVC3 class.
As far as I can tell, Ninject.Web.MVC3 is far inferior because of that, as you now have to write the above code yourself and also set up WebActivator yourself if you don't want to use global.asax.
Very annoying. It's not a huge amount of work to write it yourself, but the whole point about NuGet is to make it real easy to get up and running with stuff.

Reply

Ups, sorry, I was wrong; Ninject.Web.MVC3 _does_ have a dependency resolver in
Ninject.Web.Mvc.NinjectDependencyResolver

It's just the WebActivator stuff you have to do yourself.

Reply

Pingbacks and trackbacks (1)+

Add comment

biuquote
  • Comment
  • Preview
Loading