Unity Configuration Schematic
Source Schema for the Unity Application Block
下面列出用于配置Unity Application Block(Unity)的元素和属性。配置文件具有如下 section-handler 声明:
<configSections>
<section name="unity"
type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection,
Microsoft.Practices.Unity.Configuration, Version=1.2.0.0,
Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
</configSections>
section-handler 声明包含配置名称和处理配置数据的类 Microsoft.Practices.Unity.Configuration.UnityConfigurationSection。
unity 元素
unity 元素指定 Unity Application Block 配置。该元素是必须的。
<unity>
<typeAliases>
...
</typeAliases>
<containers>
<container name="containerOne">
<types>
...
</types>
<instances>
...
</instances>
<extensions>
...
</extensions>
<extensionConfig>
...
</extensionConfig>
</container>
</containers>
</unity>
其具有如下子元素:
- typeAliases
- containers
typeAliases 元素
typeAliases 元素包含可选类型别名的集合,使你可以轻松指定映射(mappings)、生命周期(lifetime)、实例(instances)、扩展(extensions),Unity 容器其他配置地方也能进行这些配置。当在扩展点(extensibility points),如 typeConfig 小节和自定义注入值上指定元素类型时,你不能使用类型别名。
该元素唯一可用的子元素是一个 typeAlias 元素的集合。
<typeAliases>
<!-- Lifetime manager types -->
<typeAlias alias="singleton"
type="Microsoft.Practices.Unity.ContainerControlledLifetimeManager,
Microsoft.Practices.Unity" />
<typeAlias alias="external"
type="Microsoft.Practices.Unity.ExternallyControlledLifetimeManager,
Microsoft.Practices.Unity" />
<typeAlias alias="perThread"
type="Microsoft.Practices.Unity.PerThreadLifetimeManager,
Microsoft.Practices.Unity" />
<!-- User-defined type aliases -->
<typeAlias alias="IMyInterface"
type="MyApplication.MyTypes.MyInterface, MyApplication.MyTypes" />
<typeAlias alias="MyRealObject"
type="MyApplication.MyTypes.MyRealObject, MyApplication.MyTypes" />
<typeAlias alias="MyCustomLifetime"
type="MyApplication.MyLifetimeManager, MyApplication.MyTypes" />
</typeAliases>
typeAlias 元素
typeAlias 元素定义一个单独的别名,你可以在配置的其他地方使用它。
typeAlias 元素的属性如表所示:
属性 |
描述 |
alias |
可以在配置的其他地方使用的别名和速记名,指代指定的类型。该属性是必须的。 |
type |
该别名的类型全名。该属性是必须的。 |
containers 元素
containers 元素包含 Unity 容器的集合 。唯一的子元素是 container 元素的集合。
container 元素
container 元素包含一个单独容器的细节。
<container name="containerOne">
<types>
...
</types>
<instances>
...
</instances>
<extensions>
...
</extensions>
<extensionConfig>
...
</extensionConfig>
</container>
container 元素具有如下表的属性:
属性 |
描述 |
name |
容器的名称。该属性可选。 |
container 元素具有如下子元素:
- types 元素
- instances 元素
- extensions 元素
- extensionConfig 元素
types 元素
types 元素包含一个已注册 type 的集合。types 元素包含如下:
- 包含一系列 type 元素,都是添加的单独 type;
- 包含描述如何完成注入的规范。
元素的唯一子元素是 type 元素。
type 元素
type 元素定义 Unity 容器的类型映射。如果你指定 name,那么 name 用于类型映射,否则,为指定类型创建一个默认映射。
你可以为每个映射指定 lifetime 管理。如果没有显式配置 lifetime,将执行 transient lifetime。
<types>
<!-- Type mapping with no lifetime — defaults to "transient" -->
<type type="Custom.MyBaseClass" mapTo="Custom.MyConcreteClass" />
<!-- Type mapping using aliases -->
<type type="IMyInterface" mapTo="MyRealObject" />
<!-- Lifetime managers specified using the type aliases -->
<type type="Custom.MyBaseClass" mapTo="Custom.MyConcreteClass">
<lifetime type="singleton" />
</type>
<type type="IMyInterface" mapTo="MyRealObject" name="RealObject">
<lifetime type="external" />
</type>
<type type="IMyInterface" mapTo="MyRealObject" name="RealObject">
<lifetime type="perThread" />
</type>
<!-- Lifetime manager specified using the full type name -->
<!-- Any initialization data specified for the lifetime manager -->
<!-- will be converted using the default type converter -->
<type type="Custom.MyBaseClass" mapTo="Custom.MyConcreteClass">
<lifetime value="sessionKey"
type="MyApplication.MyTypes.MyLifetimeManager,
MyApplication.MyTypes" />
</type>
<!-- Lifetime manager initialization using a custom TypeConverter -->
<type type="IMyInterface" mapTo="MyRealObject" name="CustomSession">
<lifetime type="MyCustomLifetime" value="ReverseKey"
typeConverter="MyApplication.MyTypes.MyTypeConverter,
MyApplication.MyTypes" />
</type>
<!-- type with injection parameters define in configuration -->
<!-- Type mapping using aliases defined above -->
<type type="IMyService" mapTo="MyDataService" name="DataService">
<typeConfig>
<constructor>
<param name="connectionString" parameterType="string">
<value value="AdventureWorks"/>
</param>
<param name="dataService" parameterType="IMyService">
<dependency />
</param>
</constructor>
<property name="MyRealObject" propertyType="IMyInterface" />
<method name="Initialize">
<param name="connectionString" parameterType="string">
<value value="contoso"/>
</param>
<param name="dataService" parameterType="IMyService">
<dependency />
</param>
</method>
</typeConfig>
</type>
</types>
下表列出 type 元素的属性:
属性 |
描述 |
name |
注册类型时使用。该属性是可选的。 |
type |
The source type to configure in the container. The type of the object to map from if this is a mapping registration or the type of the object if this is a singleton registration. Can be a user-defined alias specified in the typeAliases section of the configuration. 该属性是必须的。 |
mapTo |
The destination type for type mapping. The type of the object to map to if this is a mapping registration. 该属性是可选的。 |
type 元素具有如下子元素:
- lifetime 元素
- typeConfig 元素
lifetime 元素
lifetime 元素包含生命周期管理的细节。
<!-- Standard singleton lifetime manager specified using a type alias -->
<type type="Custom.MyBaseClass" mapTo="Custom.MyConcreteClass">
<lifetime type="singleton" />
</type>
<!-- Custom lifetime manager specified using a type alias -->
<!-- and initialized using a custom TypeConverter -->
<type type="Custom.MyBaseClass" mapTo="Custom.MyConcreteClass">
<lifetime type="MyCustomLifetime" value="ReverseKey"
name="CustomSessionLifetime"
typeConverter="MyApplication.MyTypes.MyTypeConverter, MyApplication.MyTypes" />
</type>
下表列出 lifetime 元素的属性:
属性 |
描述 |
name |
注册生命周期管理时使用的名称。该属性是可选的。 |
type |
The type of the lifetime manager to use for this mapping. Can be a user-defined alias specified in the typeAliases section of the configuration or one of the default aliases singleton or external. 该属性是必须的。 |
value |
需要初始化生命周期管理的任何值。该属性是可选的。 |
typeConverter |
The type converter to use to convert the value provided to match the type of the instance. If not specified, the default converter for the specified type is used. Aliases are allowed. 该属性是可选的。 |
typeConfig 元素
该元素包含如下子元素:
- constructor 元素
- property 元素
- method 元素
(略)
调试程序时,提示不支持该属性。因此,也略过如下元素:constructor 元素、property 元素、method 元素、param 元素、value 元素、dependency 元素、array 元素。
instances Element
The instances element holds a collection of the existing object instances for this container. These are objects registered with the container using the RegisterInstance method. You can register anything with RegisterInstance. Instances added through configuration tend to be simple because it is hard to represent them with a string value, but they could be arbitrarily complex if there is a type converter that can handle the conversion from a string. The instances element contains a series of add elements that add individual instances.
<instances>
<add name="MyInstance1" type="System.String" value="Some value" />
<add name="MyInstance2" type="System.DateTime" value="2008-02-05T17:50:00" />
</instances>
The only child element of the instances element is a collection of the add element for instances.
The add element for instances defines an instance mapping to insert into the Unity container.
下表列出 add 元素的属性:
Attribute |
Description |
name |
The name to use when registering this instance. This attribute is optional. |
type |
The type of this instance. This attribute is optional. Can be a user-defined alias specified in the typeAliases section of the configuration. If omitted, the assumed type is System.String. |
value |
A string representation of the desired instance that is used to convert from one instance to another. This attribute is required. |
typeConverter |
The type converter to use to convert the value provided to match the type of the instance. If not specified, the default converter for the specified type is used. This attribute is optional. |
extensions Element
调试程序时,提示不支持该属性。
(略)
extensionConfig Element
调试程序时,提示不支持该属性。
(略)
interceptors Element
The <interceptors> element encloses the list of interceptors in the configuration and contains a series of <interceptor> elements that specify the individual interceptors.
interceptor Element
default Element
key Element
policies Element
policy Element
matchingRules Element
matchingRule Element
injection Element
callHandlers Element
callHandler Element
参考资料
- Unity Application Block 1.2 - October 2008
- Unity Application Block Methods(RegisterType、Resolve、BuildUp)
- Source Schema for the Unity Application Block
以上链接虽然不再更新,但还是有一定参考价值。关于最新的 Unity 信息在 Unity Application Block site