leora
leora

Reputation: 196891

Do I need to use ninject.mvc extension anymore?

I see there is an extension for Ninject integration with asp.net-mvc but it looks like I can integrate Ninject with mvc fine without this extension. For example:

public class NinjectDependencyResolver : IDependencyResolver
    {
        private readonly IResolutionRoot _resolutionRoot;

    public NinjectDependencyResolver(IResolutionRoot resolutionRoot)
    {
        _resolutionRoot = resolutionRoot;
    }

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

    public IEnumerable<object> GetServices(Type serviceType)
    {
        return _resolutionRoot.GetAll(serviceType);
    }
}


public class MvcApplication : HttpApplication
{
    void Application_Start()
    {
        var modules = new INinjectModule[] { new ServiceModule() };
        var kernel = new StandardKernel(modules);

        DependencyResolver.SetResolver(new NinjectDependencyResolver(kernel));

Is this some legacy extension or is it still relevant? I see recent updates to the source code so I was a bit confused

Upvotes: 1

Views: 310

Answers (1)

Remo Gloor
Remo Gloor

Reputation: 32725

You can implement your own Dependency Resolver. So yes you dont need it. You can integrate Ninject quite easily without the extension. But the question is why should you do this? The Ninject.MVC3 extension provides everything to add support for Ninject without having to implement an own Dependency Resolver. This has several advantages:

  1. Unlike the implementation you are proposing, the implementation of this extension is correct and proved to work in many applications.
  2. It is mantained together with Ninject core. In case Ninject core changes all the necessary changes will be done for you. E.g. Ninject 3.0.0 core does not have InRequestScope anymore, but with Ninject.MVC3 you still have this scope.
  3. This extension is much more than a Dependency Resolver. Read the documentation!
  4. It runs side aside with other web technologies and the configuration can be shared. E.g. MVC4 Web API, WCF, WebForms

Upvotes: 5

Related Questions