我是否需要再使用ninject.mvc扩展名?

leo*_*ora 1 asp.net-mvc ninject

我看到Ninject与asp.net-mvc 集成有一个扩展,但看起来我可以将Ninject与mvc完美集成而没有这个扩展.例如:

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));
Run Code Online (Sandbox Code Playgroud)

这是一些遗留扩展还是仍然相关?我看到源代码的最新更新,所以我有点困惑

Rem*_*oor 5

您可以实现自己的依赖关系解析器.所以是的,你不需要它.你可以很容易地集成Ninject而不需要扩展.但问题是你为什么要这样做?Ninject.MVC3扩展提供了添加对Ninject的支持的所有内容,而无需实现自己的依赖关系解析器.这有几个好处:

  1. 与您提议的实现不同,此扩展的实现是正确的,并证明在许多应用程序中都有效.
  2. 它与Ninject核心一起保留.如果Ninject核心发生变化,将为您完成所有必要的更改.例如,Ninject 3.0.0核心不再具有InRequestScope,但是对于Ninject.MVC3,您仍然拥有此范围.
  3. 此扩展不仅仅是依赖性解析器.阅读文档!
  4. 它与其他Web技术并排运行,可以共享配置.例如MVC4 Web API,WCF,WebForms