本文内容

  • Unity 配置示意图
  • Unity 的 XML 架构
  • 参考资料

研究配置文件总是很麻烦,而且很可能因为版本问题,会稍有不同。如果你不确定 Unity 是否支持以及如何支持某个元素,就看下相关文档,或是看下 Unity 源代码中的 Unity.Configuration 项目,该项目中每个支持的元素都有一个类。

Unity XML 配置文件可以用来完成依赖注入的配置,即便不通过配置文件,也可以通过代码来完成,它们是等价的。

下载 Unity 3

Unity 配置示意图


Unity 的 XML 架构


下面列出用于配置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 元素
  • register 元素。看了几个 Unity 的 XML 配置文件,貌似 register 元素 和 type 元素(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 元素。

register 元素

register 元素是任何配置的基本构建块。它使你能够为一个类型指定映射和注入配置。如果你指定一个 name,那么 name 用于类型映射。否则,它会创建一个指定类型的默认映射。你也可以为每个映射指定一个生命周期管理器。如果你没有为一个类型显式配置生命周期管理器,那么它会使用瞬态生命周期管理器(transient lifetime manager)。

下表列出 register 元素的属性:

属性

描述

type

The type that is being registered. This is the type that will be requested when calling the Resolve method. 该属性是必须的。

name

The name of the registration; if omitted the default registration for the type will be created. 该属性是可选的。

mapTo

Type which will actually be created and returned when Resolve is called. This sets up a type mapping. It can be a user-defined alias or one of the default aliases. 该属性是可选的。

下面 XML 文档说明 register 元素的一般用法:

<container>

  <register type="MyService"> ... </register> <!-- Default registration for type MyService -->

  <register type="ILogger" mapTo="EventLogLogger" /> <!-- type mapping -->

  <register type="ILogger" mapTo="PageAdminLogger" name="emergency" /> <!-- named registration -->

</container>

在运行时注册类型,使用 RegisterType 方法。下面代码段在运行时注册 EventLogLogger 类:

// Register a default (un-named) type mapping with a transient lifetime

// Specify the registered type as an interface or object type 

// and the target type you want returned for that type

myContainer.RegisterType<ILogger, EventLogLogger>();

register 元素具有如下子元素:

  • lifetime 元素

    constructor 元素

    property 元素

    method 元素

instances 元素

instances 元素包含容器中已存在对象实例的集合。这些对象都是用 RegisterInstance 方法注册的。 instances 元素包含一系列添加单独实例的 add 元素。

<instances>

  <add name="MyInstance1" type="System.String" value="Some value" />

  <add name="MyInstance2" type="System.DateTime" value="2008-02-05T17:50:00" />

</instances>

instances 元素的唯一子元素是 add 元素的集合。

add 元素定义了一个实例映射,插入到 Unity 容器中。

下表列出 add 元素的属性:

属性

描述

name

The name to use when registering this instance. 该属性是可选的。

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.该属性是必须的。

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. 该属性是可选的。

extensions 元素和 extensionConfig 元素

(略)

调试程序时,提示不支持该属性。

interceptors 元素

interceptors 元素包含配置中的拦截器列表,其中包含了一系列的指定单独拦截器的 interceptor 元素。

interceptor 元素

interceptors 元素中的每个 interceptor 元素指定了一个单独的拦截器配置。interceptor 元素具有以下子元素:

  • key
  • default

定义 interceptors 的方式与定义 lifetime 管理一样。

下表列出 interceptor 元素的属性:

属性

描述

name

The name to use when registering this interceptor. This attribute is required only when there are two or more entries for the same interceptor type.

type

The type of the interceptor. 该属性是必须的。

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. 该属性是可选的。

Value

Any value required to initialize the interceptor. 该属性是可选的。

default 元素

default 子元素设置一个类型的默认拦截器。它具有唯一的 type 属性。type 属性指定 interceptor 的类型。

key 元素

interceptor 元素的 key 子元素用于设置指定生成秘钥的 interceptor。

下表列出 key 元素的属性:

属性

描述

name

The name to use when registering the build key. 该属性是可选的。

type

The type for the build key. 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. 该属性是必须的。

policies 元素

policies 元素包含一系列策略列表,其包含了一系列指定单独策略的 policy 元素。

下面 XML 例子说明 policies 元素及其子元素。

<policies>

  <policy name="foot">

    <matchingRules>

      <matchingRule name="rule1">

        <lifetime type="singleton" />

      </matchingRule>

    </matchingRules>

    <callHandlers>

      <callHandler name="handler1" type="GlobalCountCallHandler">

        <lifetime type="singleton" />

      </callHandler>

      <callHandler name="handler2" type="GlobalCountCallHandler">

      </callHandler>

    </callHandlers>

  </policy>

</policies>

policy Element

policies 元素中的每个 policy 元素通过两个子元素 matchingRulescallHandlers 指定一个单独策略的完成配置。

下表列出 policy 元素的属性:

属性

描述

name

The name to use when registering this policy. This attribute is required.

type

The type of the policy. Can be a user-defined alias specified in the typeAliases section of the configuration. The extension must contain a class that can read the contents of the policy configuration. This attribute is required.

matchingRules 元素

matchingRules 元素包含了一系列政策的匹配规则,其中包含了一系列指定单独匹配规则的 matchingRule 元素。matchingRules 元素内的匹配规则组合必须为 True才能应用。

matchingRule 元素

matchingRules 元素中的每个 matchingRule 元素指定一个单独的匹配规则的细节。matchingRule 元素具有 injectionlifetime 两个子元素。

下表列出 matchingRule 元素的属性:

属性

描述

name

The name by which code in the application block and the configuration tools will refer to this matching rule.

type

The name of the matching rule class, the name of the containing assembly, the version, the culture information, and the public key token.

matchingRule 元素具有如下子元素:

  • injection
  • lifetime

lifetime 元素指定队形的生命周期行为。它具有唯一的 type 属性,指定生命周期管理。

下面 XML 说明 matchingRule 元素及其子元素。

<matchingRules>

  <matchingRule name="rule1">

    <lifetime type="singleton" />

  </matchingRule>

</matchingRules>

injection 元素

injection 元素描述了如何在 matchingRule 元素的情况下,创建一个指定的元素。它很像 typeConfig 元素,除了 injection 元素配置一个实例的注入外,而  typeConfig 是一个扩展点。injection 元素与 typeConfig 的默认情况具有相同的子元素。

callHandlers 元素

callHandlers 元素包含一个策略的调用句柄,其中包含一系列指定单独调用句柄的 callHandler 元素。

callHandler 元素

callHandlers 元素中的每个 callHandler 元素指定一个单独调用句柄的细节。callHandler 元素具有 injection 和 lifetime 子元素。

下表列出 callHandler 元素的属性:

属性

描述

name

The name by which code in the application block and the configuration tools will refer to this call handler.

type

The type of the callhandler. Can be a user-defined alias specified in the typeAliases section of the configuration.

ChildcallHandler 元素具有如下子元素:

  • injection
  • lifetime

下面 XML 文件说明带 injection 子元素的 callHandler 元素。该例子注入一个属性。

<callHandlers>

  <callHandler name="handler1" type="GlobalCountCallHandler">

    <injection>

      <constructor>

        <param name="name" parameterType="string">

          <value value="handler1" />

        </param>

      </constructor>

      <--sets the position of the callhandlers 

      &lt;-- within the policy handler chain/>

      <property name="Order" propertyType="int">

        <value value="10" type="int"/>

      </property>

    </injection>

  </callHandler>

</callHandlers>

参考资料

 

以上链接虽然不再更新,但还是有一定参考价值。关于最新的 Unity 信息在 Unity Application Block site

 

下载 Unity 3

 

.NET 用 Unity 依赖注入——概述注册和解析类型(1)

.NET Unity XML 配置文件(2)的更多相关文章

  1. (转)Unity 导出XML配置文件,动态加载场景

    参考:http://www.xuanyusong.com/archives/1919 http://www.omuying.com/article/48.aspx   主要功能: 1.导出场景的配置文 ...

  2. Mybatis 源码分析--Configuration.xml配置文件加载到内存

    (补充知识点: 1 byte(字节)=8 bit(位) 通常一个标准英文字母占一个字节位置,一个标准汉字占两个字节位置:字符的例子有:字母.数字系统或标点符号) 1.创建SqlSessionFacto ...

  3. 转-springAOP基于XML配置文件方式

    springAOP基于XML配置文件方式 时间 2014-03-28 20:11:12  CSDN博客 原文  http://blog.csdn.net/yantingmei/article/deta ...

  4. xml 配置文件规范 校验

    背景:做的数据同步框架,数据同步种类通过xml配置文件添加.为了系统的稳定性,我们只能认为将来写这个运行配置xml的人是一个傻瓜,那么对xml格式校验就很重要. 通过dom4j,是可以完成对xml格式 ...

  5. Spring中加载xml配置文件的六种方式

    Spring中加载xml配置文件的六种方式 博客分类: Spring&EJB XMLSpringWebBeanBlog  因为目前正在从事一个项目,项目中一个需求就是所有的功能都是插件的形式装 ...

  6. 史上最全web.xml配置文件元素详解

    一.web.xml配置文件常用元素及其意义预览 <web-app> <!--定义了WEB应用的名字--> <display-name></display-na ...

  7. Spring XML配置文件示例(二)——web.xml

    <?xml version="1.0" encoding="UTF-8"?> <web-app id="WebApp_ID" ...

  8. hibernate.cfg.xml配置文件和hbm.xml配置文件

    http://blog.sina.com.cn/s/blog_a7b8ab2801014m0e.html hibernate.cfg.xml配置文件格式 <?xml version=" ...

  9. struts2中struts.xml配置文件详解【未整理】

    1.    深入Struts2的配置文件 本部分主要介绍struts.xml的常用配置. 1.1.    包配置: Struts2框架中核心组件就是Action.拦截器等,Struts2框架使用包来管 ...

随机推荐

  1. Android Monkey压力测试环境搭建及使用

    Android Monkey压力测试学习笔记 步骤:下载SDK -> 解压进入SDK Manager下载系统 -> 配置环境变量 -> 创建虚拟设备或连接真机 -> 进入命令模 ...

  2. 胖哈勃杯Pwn400、Pwn500详解

    概述 这次的胖哈博杯我出了Pwn400.Pwn500两道题目,这里讲一下出题和解题的思路.我个人感觉前两年的Pwn题更多的是考察单一的利用技巧,比我这有个洞怎么利用它拿到权限.但是我研究了一些最近的题 ...

  3. Spring的核心之IoC容器创建对象

    Spring的Ioc容器,是Spring的核心内容: 作用:对象的创建和处理对象的依赖关系. Spring容器创建对象有以下几种方式: 1:调用无参数的构造器 <!-- 默认无参的构造器 --& ...

  4. .NET中JSON序列化(数据集转JSON)

    Json序列化和反序列化指的是:对象序列化为JSON,并可用于从 JSON 反序列化对象 在.net 3.5中已支持JSON,引用命名空间: using System.Web.Script.Seria ...

  5. python错误、调试、测试

    1.错误, Python内置的try...except...finally用来处理错误十分方便.出错时,会分析错误信息并定位错误发生的代码位置才是最关键的. 程序也可以主动抛出错误,让调用者来处理相应 ...

  6. linux 中的./configuration --prefix=安装路径 的用法(指定源码安装方式的安装路基)

    源码的安装一般由3个步骤组成:配置(configure).编译(make).安装(make install). Configure是一个可执行脚本,它有很多选项,在待安装的源码路径下使用命令./con ...

  7. Codeforces Round #392 (Div. 2)-D. Ability To Convert

    D - Ability To Convert 题目大意:给你一个数字 n 接下来再输入一个数字 w(<10^60),表示w这个数字是 n 进制的, 并且超过十进制也用数字表示,这样就有多种组合了 ...

  8. 【CSS3】响应式布局

    准备工作1:设置Meta标签 首先我们在使用Media的时候需要先设置下面这段代码,来兼容移动设备的展示效果: 1 <meta name="viewport" content ...

  9. 023 SpringMVC拦截器

    一:拦截器的HelloWorld 1.首先自定义拦截器 只要实现接口就行. package com.spring.it.interceptors; import javax.servlet.http. ...

  10. 关于final中的几个忽略的点的再次阐述

    final : 最终.作为一个修饰符,可以感性的认识,但是总是在背后会忽略特殊的角落. 1,可以修饰类,函数,变量. 2,被final修饰的类不可以被继承.为了避免被继承,被子类复写功能. 这一个点容 ...