Asp.net mvc 我还需要使用ninject.mvc扩展吗?

Asp.net mvc 我还需要使用ninject.mvc扩展吗?,asp.net-mvc,ninject,Asp.net Mvc,Ninject,我看到了asp.net-mvc,但是看起来我可以在没有这个扩展的情况下很好地将Ninject与mvc集成。例如: public class NinjectDependencyResolver : IDependencyResolver { private readonly IResolutionRoot _resolutionRoot; public NinjectDependencyResolver(IResolutionRoot resolutionRoot

我看到了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));

这是传统的扩展还是仍然相关?我看到了源代码的最新更新,所以我有点困惑

您可以实现自己的依赖项解析器。所以,是的,你不需要它。您可以很容易地集成Ninject,而无需扩展。但问题是你为什么要这样做?MVC3扩展提供了添加对Ninject的支持的一切,而无需实现自己的依赖项解析程序。这有几个优点:

与您建议的实现不同,此扩展的实现是正确的,并且在许多应用程序中都被证明是有效的。 它与Ninject核心一起被覆盖。如果Ninject core发生更改,所有必要的更改都将为您完成。例如,Ninject 3.0.0 core不再具有InRequestScope,但使用Ninject.MVC3,您仍然具有此范围。 这个扩展不仅仅是依赖项解析器。阅读 它与其他web技术并行运行,配置可以共享。例如MVC4 Web API、WCF、WebForms
您可以实现自己的依赖项解析器。所以,是的,你不需要它。您可以很容易地集成Ninject,而无需扩展。但问题是你为什么要这样做?MVC3扩展提供了添加对Ninject的支持的一切,而无需实现自己的依赖项解析程序。这有几个优点:

与您建议的实现不同,此扩展的实现是正确的,并且在许多应用程序中都被证明是有效的。 它与Ninject核心一起被覆盖。如果Ninject core发生更改,所有必要的更改都将为您完成。例如,Ninject 3.0.0 core不再具有InRequestScope,但使用Ninject.MVC3,您仍然具有此范围。 这个扩展不仅仅是依赖项解析器。阅读 它与其他web技术并行运行,配置可以共享。例如MVC4 Web API、WCF、WebForms
关于1,建议的实现有什么问题???@leora-我现在还不知道,但是由于Remo编写了现有的实现,并且查看了代码,我想它会做更多的事情,比如处理请求范围?,我假设至少有支持请求范围,但不确定这是真是假not@leora它不能正确处理多个枚举,在某些情况下会导致奇怪的行为。由于TryGet,很难检测到一些配置问题。关于1,建议的实现有什么问题?@leora-我不知道,但由于Remo编写了现有的一个,并且查看代码,它会做很多事情,比如处理请求范围,我想?,我假设至少有支持请求范围,但不确定这是真是假not@leora它不能正确处理多个枚举,在某些情况下会导致奇怪的行为。由于TryGet的存在,很难检测到一些配置问题。