我正在尋找Unity的一些基本示例/解釋。我很難理解這個概念。我對注入模式有基本的瞭解,因爲Unity似乎與它緊密相關。我感謝任何幫助。什麼是Microsoft Unity?
9
A
回答
11
團結是several DI Containers for .NET之一。當有問題的類型遵循Dependency Inversion Principle時,它可用於組成對象圖。
做到這一點的最簡單的方法是使用構造函數注入模式:
public class Foo : IFoo
{
private readonly IBar bar;
public Foo(IBar bar)
{
if (bar == null)
throw new ArgumentNullException("bar");
this.bar = bar;
}
// Use this.bar for something interesting in the class...
}
現在,您可以在應用程序的Composition Root配置統一:
container.RegisterType<IFoo, Foo>();
container.RegisterType<IBar, Bar>();
的是註冊階段的Register Resolve Release pattern。在解決相container
將自動線無需進一步配置對象圖:
var foo = container.Resolve<IFoo>();
這自動地工作,因爲所涉及的類的靜態結構包括的所有信息的容器需要構成對象圖形。
0
1
相關問題
- 1. 什麼是Microsoft OSLO?
- 2. 什麼是Unity InjectionConstructor屬性?
- 3. 什麼是Microsoft Application Verifier?
- 4. Microsoft WebMatrix:它是什麼?
- 5. 什麼是用於Microsoft Windows
- 6. WPF/Prism:什麼是UNITY容器?
- 7. Unity中的AutoFac SingleInstance()是什麼?
- 8. 爲什麼Unity在Unity中看起來是紫色的?
- 9. Microsoft Unity基類攔截
- 10. Microsoft Unity容器的性能
- 11. Microsoft Unity-解決問題
- 12. 什麼是Microsoft SQL加入使用
- 13. 什麼是爲Microsoft文件格式
- 14. 什麼是Microsoft Team Foundation Server中的「$(SourceDir)」?
- 15. Microsoft ProClarity的當前狀態是什麼?
- 16. 什麼是Microsoft Document Explorer 2008用於?
- 17. 什麼是Microsoft SQL服務器,爲什麼需要安裝它?
- 18. Microsoft Unity IoC不起作用和ASP.NET mvc4應用程序。爲什麼?
- 19. Microsoft Unity,構造函數中的參數
- 20. 攔截的Microsoft Unity XML配置
- 21. Microsoft Unity的Programatic屬性注入
- 22. Unity:初始化Microsoft Media Foundation失敗
- 23. Microsoft Unity - 是否可以在運行時更改註冊類型?
- 24. 什麼是Microsoft消息隊列(MSMQ)?它是如何工作的?
- 25. 什麼是溫莎城堡的Depends在Unity中的等值?
- 26. 爲什麼Unity啓動這個實例不是一個單例?
- 27. 實現Unity Singleton的最佳方式是什麼?
- 28. 實際fontSize在Unity中的含義是什麼?
- 29. 什麼是Firebase-Unity插件中的'transaction'命令?
- 30. 什麼是在Unity容器上調用dispose的最佳方式?
爲了增加馬克的回答(希望他不介意),如果你想了解更多關於依賴注入,Mark自己寫了一本非常好的書,名爲「.NET中的依賴注入」。 http://www.manning.com/seemann/ – Phill
@phill:請不要這樣做。馬克討厭人們提到他的書時:-) – Steven
:(對不起,不會再這樣做。 – Phill