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

时间:2012-03-31 21:06:21

标签: asp.net-mvc ninject

我用asp.net-mvc看到there is an extension for Ninject integration但看起来我可以将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));

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

1 个答案:

答案 0 :(得分:5)

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

  1. 与您提议的实现不同,此扩展的实现是正确的,并证明可以在许多应用程序中使用。
  2. 与Ninject核心一起保存。如果Ninject核心发生变化,将为您完成所有必要的更改。例如。 Ninject 3.0.0核心不再具有InRequestScope,但是使用Ninject.MVC3你还有这个范围。
  3. 此扩展程序不仅仅是依赖项解析程序。阅读the documentation
  4. 与其他网络技术并排运行,可以共享配置。例如。 MVC4 Web API,WCF,WebForms
相关问题