亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb

首頁 > 學院 > 開發設計 > 正文

.NETUnityXML配置文件(2)

2019-11-14 16:03:46
字體:
來源:轉載
供稿:網友

本文內容

  • Unity 配置示意圖
  • Unity 的 xml 架構
  • 參考資料

研究配置文件總是很麻煩,而且很可能因為版本問題,會稍有不同。如果你不確定 Unity 是否支持以及如何支持某個元素,就看下相關文檔,或是看下 Unity 源代碼中的 Unity.Configuration 項目,該項目中每個支持的元素都有一個類。

Unity XML 配置文件可以用來完成依賴注入的配置,即便不通過配置文件,也可以通過代碼來完成,它們是等價的。

下載 Unity 3

Unity 配置示意圖


IC242522

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 Application Block 1.2 - October 2008
  • Unity Application Block Methods(RegisterType、Resolve、BuildUp)
  • Source Schema for the Unity Application Block 
  • The Unity Configuration Schema

 

以上鏈接雖然不再更新,但還是有一定參考價值。關于最新的 Unity 信息在 Unity Application Block site

 

下載 Unity 3

 

.NET 用 Unity 依賴注入——概述注冊和解析類型(1)


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb
久久精品国产久精国产一老狼| 日本高清视频一区| 成人免费视频a| 欧美一级大片视频| 国产在线精品成人一区二区三区| 日韩黄色av网站| 欧美在线视频网| 国产精品视频专区| 国产精品视频999| 日韩国产激情在线| 亚洲图片在线综合| 久久久国产精品亚洲一区| 欧美国产激情18| 精品亚洲国产视频| 久久伊人色综合| 最新69国产成人精品视频免费| 一区二区亚洲精品国产| 91人人爽人人爽人人精88v| 国产亚洲精品美女久久久| 久久精品国产精品亚洲| 激情成人在线视频| 欧美在线视频一二三| 国产精品久久久久久久久| 成人国产精品一区二区| 久久视频这里只有精品| 日日噜噜噜夜夜爽亚洲精品| 欧美精品videofree1080p| 91视频免费网站| 97视频在线免费观看| 国产亚洲精品激情久久| 亚洲精品98久久久久久中文字幕| 一区二区三区视频免费| 欧美国产视频日韩| 91大神福利视频在线| 亚洲精品一区二区三区不| 亚洲国产成人91精品| 久久精品在线播放| 欧美高清自拍一区| 欧美高清视频在线| 亚洲国产欧美一区二区三区同亚洲| 欧美性资源免费| 欧美亚洲成人xxx| 久久精品视频亚洲| 亚洲欧美日韩综合| 久久精品在线播放| 欧美极品少妇xxxxx| 亚洲人成伊人成综合网久久久| 欧美色xxxx| 在线观看91久久久久久| 日韩va亚洲va欧洲va国产| 热草久综合在线| 国语自产精品视频在线看抢先版图片| 中文字幕在线看视频国产欧美| 久久精品国产电影| 欧美日韩免费区域视频在线观看| 欲色天天网综合久久| 欧美资源在线观看| 国产精品久久久久久久午夜| 亚洲性线免费观看视频成熟| 欧美电影《睫毛膏》| 久热在线中文字幕色999舞| 成人黄在线观看| 亚洲另类xxxx| 日本欧美精品在线| 欧美中文字幕在线视频| 欧美成人中文字幕在线| 国产日产欧美a一级在线| 国产精品白嫩初高中害羞小美女| 久久久精品2019中文字幕神马| 免费av一区二区| 欧美午夜视频一区二区| 91久久精品国产91性色| 成人性生交大片免费看视频直播| 日韩精品久久久久久久玫瑰园| 亚洲黄色av网站| 亚洲最大成人网色| 久久国产精彩视频| 亚洲天堂免费观看| 亚洲国产精品视频在线观看| 不卡毛片在线看| 亚洲天堂av图片| 国产精品入口夜色视频大尺度| 亚洲人成在线观看网站高清| 久久精品在线视频| 亚洲精品免费一区二区三区| 午夜剧场成人观在线视频免费观看| 7777精品视频| 亚洲精品自在久久| 国内成人精品视频| 久久人人爽人人爽人人片av高清| 精品久久中文字幕久久av| 久久99精品国产99久久6尤物| 亚洲欧美激情视频| 亚洲国产精品va在线观看黑人| 97成人在线视频| 亚洲精品中文字幕av| 91在线色戒在线| 久久影视电视剧凤归四时歌| 欧美一区二区大胆人体摄影专业网站| 日韩国产欧美精品一区二区三区| 国产大片精品免费永久看nba| 97国产精品视频人人做人人爱| 欧美国产中文字幕| 国产精品午夜一区二区欲梦| 亚洲精品小视频在线观看| 亚洲精品日韩av| 亚洲激情第一页| 欧美在线性爱视频| 美日韩在线视频| 中文字幕欧美精品日韩中文字幕| 成人精品一区二区三区电影黑人| 国产精品视频网站| 久久综合色影院| 欧美精品在线极品| 日韩欧美国产骚| 国产精品福利在线观看网址| 国产97在线|日韩| 亚洲aaa激情| 在线观看91久久久久久| 亚洲人成网站色ww在线| 综合av色偷偷网| 亚洲一级一级97网| 亚洲国产成人久久| 91色在线视频| 国产精品27p| 热99在线视频| 欧美一级大片在线免费观看| 亚洲欧洲国产一区| 亚洲精品久久7777777| 亚洲网站在线观看| 日韩一区二区在线视频| 亚洲男人天堂九九视频| 日韩欧美大尺度| 日韩精品在线免费观看视频| 538国产精品一区二区在线| 国内免费久久久久久久久久久| 91精品久久久久久久久久久久久| 欧美怡红院视频一区二区三区| 91国产视频在线播放| 久久久久久91| 亚洲天堂男人的天堂| 亚洲第一区中文99精品| 久久99久久99精品中文字幕| 欧美交受高潮1| 国产在线精品播放| 久久综合久久美利坚合众国| 96精品视频在线| 亚洲美女久久久| 国产精品国产自产拍高清av水多| 欧美精品videosex极品1| 成人精品aaaa网站| 国模gogo一区二区大胆私拍| 成人黄色大片在线免费观看| 久久久久久久香蕉网| 精品美女永久免费视频| 97成人超碰免| 精品高清美女精品国产区| 成人免费淫片aa视频免费| 少妇激情综合网| 欧美老女人xx| 久久久精品一区| 成人性生交xxxxx网站| 亚洲一区二区三区四区视频| 亚洲精品美女视频|