AutoFac文檔
目錄
- 開始
- Registering components
- 控制范圍和生命周期
- 用模塊結構化Autofac
- xml配置
- 與.net集成
- 深入理解Autofac
- 指導
- 關於
- 詞匯表
關系類型
Autofac提供拆箱即用的關系類型。
Autofac會對下面列出的關系類型特別解釋。下面列出的每一種關系類型,Autofac都會在需要的時候提供一種默認實現。
比如說,當Autofac會 IEnumerable 類型的構造函數參數進行注入時,它不會尋找提供 IEnumerable 的component,而是容器會找出所有的ITasks實現然后全部注入。
(你可以顯示注冊這些類型的實現來覆蓋默認行為)
Relationship |
Type |
Meaning |
A needs B |
B |
Dependency |
A needs B at some point in the future |
Lazy<B> |
Delayed instantiation |
A needs B until some point in the future |
Owned<B> |
Controlled lifetime |
A needs to create instances of B |
Func<B> |
Dynamic instantiation |
A provides parameters of types X and Y to B |
Func<X,Y,B> |
Parameterisation |
A needs all the kinds of B |
IEnumerable<B> |
Enumeration |
A needs to know X about B |
Meta<T> and Meta<B,X> |
Metadata interrogation |
A needs to choose B based on X |
IIndex<X,B> |
Lookup |
組合關系類型
關系類型可以被組合使用,所以:
IEnumerable<Func<Owned<ITask>>>
上面這行代碼將向下面這樣被解釋:
- 屬性所有的實現
- 返回一個工廠
- 生命周期被控制
- ITask服務
標准的類型組合
.NET Type |
Equivalent to |
Lazy<T,M> |
Meta<Lazy<T>, M> |
ExportFactory<T> |
Func<Owned<T>> |
ExportFactory<T,M> |
Meta<Func<Owned<T>>> |
注意, ExportFactory 類型包含在Silverlight4中而不是.NET中,所以還不被Autofac支持
關系類型和容器的獨立性
Autofac中自定義的容器類型不會強迫你將程序緊緊的和Autofac綁在一起。它們會為容器配置提供一種一致的編程模型,和你寫其他component一樣
例如,你仍然可以創建一個自定義的ITaskFactory ,但是如果有需要的話你可以提供基於 Func<Owned>實現的AutofacTaskFactory