2012-03-31 50 views
1

我看到there is an extension for Ninject integration與asp.net-mvc,但它看起來像我可以將Ninject與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)); 

這是一些傳統擴展還是仍然相關?我看到最近更新的源代碼,所以我有點困惑

回答

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
+0

關於#1,提出的實現有什麼問題? – leora 2012-03-31 23:34:18

+0

@leora - 我不知道副手,但是因爲Remo寫了現有的代碼並且看了代碼,它做了很多更多的事情(比如處理請求範圍,我猜?),我認爲至少有「支持請求範圍「(但不知道是否如此) – 2012-04-01 03:20:49

+0

@leora它不正確處理多個枚舉會導致奇怪的行爲是一些情況。由於TryGet,檢測一些配置問題將會非常困難。 – 2012-04-01 12:11:47

相關問題