我們正在評估SimpleInjector和LightInject,以便在我們的應用程序中替換當前的Unity實現。 Unity有一個方法,通過調用CreateChildContainer()來允許容器層次結構。是否有SimpleInjector和LightInject的等價物?Unity具有CreateChildContainer,SimpleInjector或LightInject中是否存在等價物
回答
LightInject使用PerScopeLifetime和PerRequestLifetime這一點。
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.Mvc和LightInject.WebApi中找到。
這幾乎是簡單注射器的工作原理,除了它們被稱爲WebRequestLifestyle和LifetimeScopeLifestyle。 – Steven
Steven,我查看了http://featuretests.apphb.com/DependencyInjection.html的比較表。你能告訴我,該頁面上關於SimpleInjector的信息是否有錯? LightInject似乎有更高的分數。我想知道現在頁面是否過時。 – Ray
@射線:比較仍然準確。由於兩個原因,簡單注射器得分相當低。 1.支持某些功能,但不適合通用測試套件(即當您看到'筆記'時)。 2.許多功能被故意排除,因爲我們認爲它將開發人員轉向錯誤的方向(例如自動註冊Lazy
- 1. Java中是否存在std :: bitset等價物(或類似物)?
- 2. 是否有.gemrc.local或等價物?
- 3. TabBarIOS - 是否有viewDidAppear或viewWillAppear等價物?
- 4. JavaScript中是否存在getColorBoundsRect()等價物?
- 5. 什麼是CastleWindsor的容器的等價物。在LightInject中發佈?
- 6. 在Python中是否有sessionInfo()等價物?
- 7. 在Java中是否有TweenMax等價物
- 8. 在java中是否有array_intersect()等價物?
- 9. 在Doxygen中是否有@inheritDoc等價物?
- 10. 在Python中是否有Rake等價物?
- 11. 在Ramda中是否有R.notEquals等價物?
- 12. 在Windows中是否有XEmbed等價物?
- 13. WeakHashMap是否存在java.util.concurrent等價物?
- 14. Axiis是否存在非Adobe等價物?
- 15. 是否存在0x80的ascii等價物?
- 16. IBM Worklight - 是否存在Window.open()等價物?
- 17. PowerShell中是否有Maven或Yum或Chocolatey等價物?
- 18. Android中是否有MethodHandle的等價物?
- 19. 廚師中是否有puppet_nist等價物?
- 20. 是否存在與SELECT ... COUNT(*)... GROUP BY ...等價的等價物?
- 21. rustc /貨物是否具有-march =本地等價物?
- 22. 在Java中是否有SignatureOutputStream(或等價物)?
- 23. Unity 5.3:什麼是UnitEngine.Application.loadedLevel的等價物?
- 24. Unity中有SCNAction,SCNTransaction或SKAction的等價物嗎?
- 25. C中是否有%*或Environment.CommandLine的等價物?
- 26. 是否有.Net System.Data的Java等價物?
- 27. Filehelpers,Super CSV:是否有Python等價物?
- 28. 是否有Android的Application :: onDestroy()等價物?
- 29. 是否有Python的RedBeanPHP等價物?
- 30. excel vlookup是否有Java的等價物?
簡單的注射器有LifetimeScope的生活方式。你能舉一些你想達到的具體例子嗎? – Steven
我在哪裏可以獲得這兩種框架的限制列表?即這些框架與Unity相比還沒有做的列表。 – Ray
您可以參考[此功能比較](http://featuretests.apphb.com/DependencyInjection.html),它基於[本博客](http://blog.ashmind.com/2008/08/19 /比較淨二IOC的框架部分-1 /)。 – Steven