1

I am starting a new project using MVC4 and would like to use dependency injection. I've not used it in the past but now seems like a good time to use it.

Can anyone tell me which DI tools now support MVC4 and which now offer the most easy set-up. My needs a fairly simple as the project is small. Most important for me is a system that is easy to learn and configure.

tereško
  • 58,060
  • 25
  • 98
  • 150
Samantha J T Star
  • 30,952
  • 84
  • 245
  • 427

3 Answers3

5

I used the Ninject.MVC3 NuGet package in an MVC 4 Beta project with success.

// Global.asax.cs

public static void Configure(HttpConfiguration config)
{
    var kernel = new StandardKernel();
    RegisterServices(kernel);

    // For ApiControllers
    config.ServiceResolver.SetResolver(
        t => kernel.TryGet(t),
        t => kernel.GetAll(t));

    // For Controllers
    DependencyResolver.SetResolver(
        t => kernel.TryGet(t),
        t => kernel.GetAll(t));
}

public static void RegisterServices(IKernel kernel)
{
    kernel.Bind<IRepository>().To<Repository>();
}

protected void Application_Start()
{
    ...

    Configure(GlobalConfiguration.Configuration);
}
Cory
  • 860
  • 8
  • 10
  • I just discovered that using ServiceResolver.SetResolver works fine when requests are made to controllers deriving from ApiController, but will result in a "No parameterless constructor defined for this object." error when requests are made to controllers deriving from Controller. As Jason Cornwall [points out on his blog Twenty6](http://twenty6-jc.blogspot.com/2012/02/idependencyresolver-and-aspnet-web-api.html), "IDependencyResolver for ApiControllers is a completely different type and is 'set' on a different resolver." – Cory Feb 25 '12 at 22:29
  • If you're using MVC 4 RC, check out [Michael Baird's reply](http://stackoverflow.com/a/10855037/186778) on a different SO thread. – Cory Jul 09 '12 at 21:37
2

I dont know MVC 4 (i just haven't had a chance to play with it yet) but i would recommend Ninject as a DI container. You can probably just install the Ninject MVC3 project (from nuget) in your solution and it may well just work with MVC4 If not use:

DependencyResolver.SetResolver(new NinjectDependencyResolver(myKernal));     

which was blogged about here http://blogs.microsoft.co.il/blogs/gilf/archive/2010/10/17/dependency-injection-in-mvc-3-was-made-easier.aspx

the blog post applies to unity but its pretty much the same for Ninject, implement a Dependancy Resolver to wrap ninject and put it into the global.ascx file

undefined
  • 33,537
  • 22
  • 129
  • 198
  • 1
    you recommend ninject yet paste unity code? : ) theres also unity.mvc3 but you have to specifically tell it when you want it to dispose of an object by using the hierarchicallifetimemanager – Adam Tuliper Dec 23 '11 at 15:20
  • The post was more around how to use any DI container with MVC3, personally i use the webactivator stuff provided by Ninject.Web.MVC3 but the other option is to do it yourself with the builtin dependancy resolver. Ive updated the code sample to say Ninject (but its actually not really any different) – undefined Dec 24 '11 at 08:39
0

For some reason Ninject using NinjectDependencyResolver didn't work for me with MVC4 (Developer preview).

I solved it using another overload of SetResolver taking two anonymous functions to resolve the dependencies.

IKernel kernel = new StandardKernel();    
kernel.Bind<...>().To(...);
DependencyResolver.SetResolver( t => kernel.Get(t), t => kernel.GetAll(t) );
Yodiz
  • 258
  • 2
  • 12