Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC3 + Ninject - How to?

I've just started playing with IoC containers and therefore chosed Ninject.

After several hours of sweat and tears I still cant figure out how to setup my MVC3 application with Ninject.

So far I have:

Global.asax.cs

public class MvcApplication : Ninject.Web.Mvc.NinjectHttpApplication
{
    public static void RegisterGlobalFilters(GlobalFilterCollection filters)
    {
        filters.Add(new HandleErrorAttribute());
    }

    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}/{id}", // URL with parameters
            new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
        );

    }

    protected void Application_Start() 
    {
        DependencyResolver.SetResolver(new MyDependencyResolver(CreateKernel()));
        RegisterGlobalFilters(GlobalFilters.Filters);
        RegisterRoutes(RouteTable.Routes);
    }

    protected override IKernel CreateKernel()
    {
        var modules = new [] { new ServiceModule() };
        return new StandardKernel(modules);
    }

}

ServiceModule.cs

internal class ServiceModule : NinjectModule
{
    public override void Load()
    {
        Bind<IGreetingService>().To<GreetingService>();
    }
}

MyDependencyResolver.cs

public class MyDependencyResolver : IDependencyResolver
{
    private IKernel kernel;

    public MyDependencyResolver(IKernel kernel)
    { 
        this.kernel = kernel; 
    }

    public object GetService(System.Type serviceType)
    {
        return kernel.TryGet(serviceType);

    }

    public System.Collections.Generic.IEnumerable<object> GetServices(System.Type serviceType)
    {
        return kernel.GetAll(serviceType);

    }
}

GreetingService.cs

public interface IGreetingService
{
    string Hello();
}

public class GreetingService : IGreetingService
{
    public string Hello()
    {
        return "Hello from GreetingService";
    }
}

TestController.cs

public class TestController : Controller
{

    private readonly IGreetingService service;

    public TestController(IGreetingService service)
    {
        this.service = service;
    }

    public ActionResult Index()
    {
        return View("Index", service.Hello());
    }

}

Each time I try to load the Index view it either just throws a overflow exception or a HTTP 404 error - If I remove all the Ninject code it works perfectly, whats wrong?

like image 510
ebb Avatar asked Dec 05 '10 10:12

ebb


1 Answers

You are mixing an own dependency resolver with the MVC extension. I'd suggest either going with your own dependency resolver or with using the MVC extension but not both. When using the MVC extension you have to use OnApplicationStarted instead of Application_Start.

See http://www.planetgeek.ch/2010/11/13/official-ninject-mvc-extension-gets-support-for-mvc3/ and have a look at the SampleApplication that comes with the source code of the MVC extension https://github.com/ninject/ninject.web.mvc.

Also the fix is not used anymore when you use the current version for the build server: http://teamcity.codebetter.com


UPDATE: The Ninject.MVC3 package continues to be updated and works OOTB against MVC4 RTM (and RC). See this page in the wiki for details.

like image 117
Remo Gloor Avatar answered Oct 23 '22 21:10

Remo Gloor