2014-02-12 33 views
2

我們正在評估SimpleInjector和LightInject,以便在我們的應用程序中替換當前的Unity實現。 Unity有一個方法,通過調用CreateChildContainer()來允許容器層次結構。是否有SimpleInjector和LightInject的等價物?Unity具有CreateChildContainer,SimpleInjector或LightInject中是否存在等價物

+0

簡單的注射器有LifetimeScope的生活方式。你能舉一些你想達到的具體例子嗎? – Steven

+0

我在哪裏可以獲得這兩種框架的限制列表?即這些框架與Unity相比還沒有做的列表。 – Ray

+0

您可以參考[此功能比較](http://featuretests.apphb.com/DependencyInjection.html),它基於[本博客](http://blog.ashmind.com/2008/08/19 /比較淨二IOC的框架部分-1 /)。 – Steven

回答

3

LightInject使用PerScopeLifetimePerRequestLifetime這一點。

PerScopeLifetime爲每個作用域創建一個給定服務的一個實例,如果它實現了IDisposable,則將該服務實例置於作用域的末尾。

PerRequestLifetime爲每個請求(GetInstance)創建新實例並在請求結束時處理所有實例。

範圍本身是使用BeginScope方法開始的。

PerScopeLifetime

container.Register<IFoo, Foo>(new PerScopeLifetime()); 
using(container.BeginScope()) 
{ 
    var firstInstance = container.GetInstance<IFoo>(); 
    var secondInstance = container.GetInstance<IFoo>(); 
    Assert.AreSame(firstInstance, secondInstance); 
} //<- Instances implementing IDisposable are disposed here. 

PerRequestLifetime

container.Register<IFoo, Foo>(new PerRequestLifetime()); 
using(container.BeginScope()) 
{ 
    var firstInstance = container.GetInstance<IFoo>(); 
    var secondInstance = container.GetInstance<IFoo>(); 
    Assert.AreNotSame(firstInstance, secondInstance); 
} //<- Instances implementing IDisposable are disposed here. 

通常情況下,BeginScope方法是什麼,是通過提供給LightInject的各種擴展名爲。

這方面的例子可以在LightInject.Web,LightInject.MvcLightInject.WebApi中找到。

+0

這幾乎是簡單注射器的工作原理,除了它們被稱爲WebRequestLifestyle和LifetimeScopeLifestyle。 – Steven

+0

Steven,我查看了http://featuretests.apphb.com/DependencyInjection.html的比較表。你能告訴我,該頁面上關於SimpleInjector的信息是否有錯? LightInject似乎有更高的分數。我想知道現在頁面是否過時。 – Ray

+0

@射線:比較仍然準確。由於兩個原因,簡單注射器得分相當低。 1.支持某些功能,但不適合通用測試套件(即當您看到'筆記'時)。 2.許多功能被故意排除,因爲我們認爲它將開發人員轉向錯誤的方向(例如自動註冊Lazy 和Func ),而如果需要可以輕鬆實現這些功能。 (注意:如果你問一個問題以確保該人得到通知,請不要忘記在名字前使用@符號,我無意中偶然發現了你的問題。) – Steven

相關問題