---
title: "Unity"
author: "strong_fee"
date: "2012-04-19T22:26:41Z"
tags: []
categories: ["编程语言"]
slug: "unity"
draft: false
lastmod: "2012-04-19T22:31:33Z"
id: 1490815
url: "https://feiqiang.iteye.com/blog/1490815"
---

# Reference：

> [使用Unity解耦你的系统—PART2——了解Unity的使用方法（1）](http://www.cnblogs.com/kyo-yo/archive/2010/11/08/Learning-EntLib-Tenth-Decoupling-Your-System-Using-The-Unity-PART2-Learn-To-Use-Unity-One.html)
> 
> [使用Unity解耦你的系统—PART2——了解Unity的使用方法（2）](http://www.cnblogs.com/kyo-yo/archive/2010/11/10/1873328.html)
> 
> [使用Unity解耦你的系统—PART2——了解Unity的使用方法（3）](http://www.cnblogs.com/kyo-yo/archive/2010/11/22/Learning-EntLib-Tenth-Decoupling-Your-System-Using-The-Unity-PART2-Learn-To-Use-Unity-Three.html)
> 
> [download《Unity Instructions CS.pdf》](http://www.coridius.com/wp-content/uploads/2011/06/Unity-Instructions-CS4.pdf)

# 一、初始化UnityContainer

1. 1. 使用API
      
      > IUnityContainer container = new UnityContainer()
      > 
      > // Register 映射。
   2. 使用配置文件
      
      > &lt;configSections&gt; &lt;section name=”unity” type=”Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration”/&gt; &lt;/configSections&gt;
      > 
      > &lt;unity xmlns=”[http://schemas.microsoft.com/practices/2010/unity”](http://schemas.microsoft.com/practices/2010/unity%22)&gt;  
      >   &lt;alias alias=”IClass” type=”UnityStudyConsole.IDemo.IClass, UnityStudyConsole” /&gt;    
      >   &lt;container name=”First”&gt;  
      >     &lt;register type=”IClass” mapTo=”MyClass” /&gt;  
      >   &lt;/container&gt;  
      > &lt;/unity&gt;
      
      > IUnityContainer container = new UnityContainer()；
      > 
      > // 使用LoadConfiguration默认使用unity section加载unity（无名字）节点。
      > 
      > container.LoadConfiguration();
      
      > // 获取section，加载有名字的unity配置。
      > 
      > UnityConfigurationSection section = (UnityConfigurationSection)ConfigurationManager.GetSection(“unity”);  
      > container.LoadConfiguration(section, “First”);
      
      参考：
      
      > [Using Design-Time Configuration](http://msdn.microsoft.com/en-us/library/ff660935%28v=PandP.20%29.aspx#load_config)

# 二、注册

1. 1. 使用RegisterType
      
      > container .RegisterType&lt;IStocksTickerView, StocksTickerForm&gt;() .RegisterType&lt;IStockQuoteService, MoneyCentralStockQuoteService&gt;();
      > 
      > 注：使用RegisterType注册的默认生命周期为transient lifetime（每次使用Resolve或者ResolveAll或者属性注入的时候都实例化一个新实例）。
   2. 使用RegisterInstance
      
      > // 注册实例，“UI”为实例名。
      > 
      > RegisterInstance(new TraceSource(“UI”, SourceLevels.All));
      > 
      > // 注册接口与实例的映射（MySingleton为映射名）
      > 
      > myContainer.RegisterInstance&lt;IMyObject&gt;(“MySingleton”, MyRealObject);
      > 
      > // 注册带有生命周期的接口与实例的映射。
      > 
      > myContainer.RegisterInstance&lt;IMyObject&gt;(“MySingleton”, MyRealObject, new ContainerControlledLifetimeManager());
      > 
      > 注：使用RegisterInstance注册的实例的生命周期为ContainerControlledLifetimeManager；使用该方法无法为实例制定**TransientLifetimeManager**生命周期，否则会产生异常。
   3. 属性注入(方法与构造函数注入类似)
      
      1. 【使用标注】在属性前加\[Dependency]标注，如：
         
         > \[Dependency]  
         > public ILogger Logger   
         > {   
         > get { return logger; }   
         > set { logger = value; }   
         > }
         
         对于实体类，解析的时候可以使用默认的规则解析。对于接口活虚类，需要注册。如：
         
         > RegisterType&lt;ILogger, ConsoleLogger&gt;();
         
         使用带有名字的的属性注入，如：
         
         > \[Dependency(“UI”)]  
         > public ILogger Logger   
         > {   
         > get { return logger; }   
         > set { logger = value; }   
         > }
         
         注册带有名字的Logger，如：
         
         > RegisterType&lt;ILogger, TraceSourceLogger&gt;(“UI”);
      2. 【使用API】使用API进行属性注入
         
         注册不带名字的属性
         
         > RegisterType&lt;IStockQuoteService, MoneyCentralStockQuoteService&gt;( new InjectionProperty(“Logger”))
         > 
         > 注：其中Logger为MoneyCentralStockQuoteService类中的一个类型为ILogger的属性。
         
         注入带有名字的属性：
         
         > RegisterType&lt;StocksTickerPresenter&gt;( new InjectionProperty(“Logger”, new ResolvedParameter&lt;ILogger&gt;(“UI”)));
         > 
         > 注：其中new InjectionProperty第二参数是指示要解析一个名字为“UI”的ILogger。
         
         注册多个属性:
         
         > ```
         > //注册对象关系时初始化对象的属性
             container.RegisterType&lt;IClass, MyClass&gt;(
                 new InjectionProperty("Name", "A班"),
                 new InjectionProperty("Description", "A班的描述"));
         > ```
         
         ```
         重载调用：
         ```
         
         > ```
         > //var myClass = container.Resolve&lt;IClass&gt;(new PropertyOverride
         > ```
         > 
         > ```
         > // ("Name", "重载后的A班"),
         //    new PropertyOverride("Description", "重载后的A班的描述"));
             var myClass = container.Resolve&lt;IClass&gt;(new PropertyOverrides()
             {
                 {"Name","重载后的A班"},
                 {"Description","重载后的A班的描述"}
             }.OnType&lt;MyClass&gt;());
         > ```
         
         ```
         注意：The PropertyOverride is not limited to a single type, 
         ```
         
         ```
         so it will apply to all resolved objects with a “Logger” property.
         ```
         
         ```
         参考：本篇中的构造函数注入
         ```
   4. 构造函数注入
      
      1. 【使用标注】如果一个类有多个构造函数，则需要在使用的构造参数前加标注：
         
         > \[InjectionConstructor]
      2. 【使用API】 注入构造函数
         
         > RegisterType&lt;ILogger, TraceSourceLogger&gt;( “UI”, new InjectionConstructor(“UI”))
         > 
         > 注：其中第一个参数“UI”为ILogger, TraceSourceLogger的映射名，第二个参数new InjectionConstructor(“UI”)为构造函数传入一个字符串类型的参数“UI”。如此，TraceSourceLogger将调用一个带有string类型的构造函数。
         > 
         > 使用这个API注入比用“InjectionConstructor”标注的构造函数的优先级别更高。
         
         如：
         
         > ```
         > container.RegisterType&lt;IClass, YourClass&gt;(
             new InjectionConstructor("my", new MyClass()));
         container.Resolve&lt;IClass&gt;();
         > ```
         
         ```
         在调用的时候我们想更换原先注册的值,可以通过ParameterOverride和ParameterOverrides
         ```
         
         ```
         来实现，其中ParameterOverride是针对一个参数，而ParameterOverrides是针对参数列表:
         ```
         
         > ```
         > // 在使用ParameterOverrides进行重载参数时，
         > ```
         > 
         > ```
         > // 可以使用如上面代码的方式进行指定，但是同样需要使用OnType来指定，
         > ```
         > 
         > ```
         > // 不过这个的OnType指定的类型是注册的对象类型。
         > ```
         > 
         > ```
         > container.Resolve&lt;IClass&gt;(new ParameterOverrides()
             {
                 {"test","test"},
                 {"my",new MyClass()}
             }.OnType&lt;YourClass&gt;());
         > ```
         > 
         > ```
         > // 在使用ParameterOverride方法来重载参数时，如果注册的参数是一个
         > ```
         > 
         > ```
         > // 具体的对象就需要使用OnType这个扩展方法来指定对应的类型，否则会报错。
         > ```
         > 
         > ```
         > container.Resolve&lt;IClass&gt;(new ParameterOverride("test", "test"), 
         > ```
         > 
         > ```
         >    new ParameterOverride("my", "new MyClass").OnType&lt;MyClass&gt;());
         > ```
         > 
         > ```
         >  
         > ```
   5. DependencyOverride
      
      Unity还为我们提供了一个DependencyOverride重载，其使用方法和参数重载、属性重载类似，这边就不演示了，不过需要注意的是DependencyOverride是针对所注册对象类型中所包含的对象类型重载，例如在A类中有构造函数参数是B类，同时也有个属性依赖于B类，当使用了DependencyOverride后，这个A对象原先注册的有关B类的依赖将全部改变。
   6. 注册泛型
      
      1. 使用API
         
         > container .RegisterType( typeof(IRepository&lt;&gt;), typeof(ValidatingRepository&lt;&gt;), “validating”);  
         > 注意：open generic types cannot be used as generic type arguments。
         
         可以在开闭泛型之间建立映射，也可以在闭映射和非泛型之间建立映射，如：
         
         > RegisterType&lt;IValidator&lt;StockQuote&gt;, RandomStockQuoteValidator&gt;();
         
         使用overrides解析泛型：
         
         > StocksTickerPresenter presenter = container.Resolve&lt;StocksTickerPresenter&gt;( new ParameterOverride( “repository”, new ResolvedParameter&lt;IRepository&lt;StockQuote&gt;&gt;(“validating”)) .OnType&lt;StocksTickerPresenter&gt;());
      2. 使用配置文件
         
         闭泛型
         
         > &lt;register type=”IRepository\[StockQuote]” mapTo=”DebugRepository\[StockQuote]“/&gt;
         
         开泛型
         
         > &lt;register type=”IRepository\[]” mapTo=”DebugRepository\[]“/&gt;
         
         例：
         
         > &lt;register type=”IRepository\[]” mapTo=”DebugRepository\[]“/&gt;
         
         > public StocksTickerPresenter( IStocksTickerView view, IStockQuoteService stockQuoteService, IRepository&lt;StockQuote&gt; repository) { … }
         
         解析IRepository&lt;StockQuote&gt;过程：首先查找是否有相应的闭泛型，如果有，则进一步解析构造函数，…；如果没有，则查找开泛型，IRepository&lt;&gt;被找到，此时，开泛型DebugRepository&lt;&gt;被使用，StockQuote被传作泛型参数，解析完毕。
   7. 数组注入
      
      CompositeLogger的构造函数为：
      
      > public CompositeLogger(ILogger\[] loggers)  
      >         {  
      >             this.loggers = (IEnumerable&lt;ILogger&gt;)loggers.Clone();  
      >         }
      
      注册方法为：
      
      > RegisterType&lt;ILogger, CompositeLogger&gt;( “composite”, new InjectionConstructor( new ResolvedArrayParameter&lt;ILogger&gt;( typeof(ILogger), new ResolvedParameter&lt;ILogger&gt;(“UI”))));
      
      其中：第一个参数typeof(ILogger是new ResolvedParameter&lt;ILogger&gt;())的简写。
      
      注意：当解析Array时，只有有名字的实例会被包含进来。

<!--THE END-->

# 三、解析

1. 1. 语法
      
      > StocksTickerPresenter presenter = container.Resolve&lt;StocksTickerPresenter&gt;();
      > 
      > 注：如果StocksTickerPresenter的构造函数有参数，则需要先注册参数。
   2. 延迟获取对象
      
      > ```
      > public static void DeferringResolve()
      {
          var resolver = container.Resolve&lt;Func&lt;IClass&gt;&gt;();
      
          //注册IClass与MyClass之间的关系
          container.RegisterType&lt;IClass, MyClass&gt;();
          //获取MyClass实例
          var myClass = resolver();
      
          var resolver2 = container.Resolve&lt;Func&lt;IEnumerable&lt;IClass&gt;&gt;&gt;();
      
          //注册与IClass相关的对象。
          container.RegisterType&lt;IClass, MyClass&gt;("my");
          container.RegisterType&lt;IClass, YourClass&gt;("your");
          //获取与IClass关联的所有命名实例
          var classList = resolver2();
      }
      > ```

<!--THE END-->

1. 1. 检索容器中注册信息
      
      > ```
      > public static void DisplayContainerRegistrations(
      > ```
      > 
      > ```
      > IUnityContainer theContainer)
      {
          string regName, regType, mapTo, lifetime;
          Console.WriteLine("容器中 {0} 个注册信息:",
                  theContainer.Registrations.Count());
          foreach (ContainerRegistration item in theContainer.Registrations)
          {
              regType = item.RegisteredType.Name;
              mapTo = item.MappedToType.Name;
              regName = item.Name ?? "[默认]";
              lifetime = item.LifetimeManagerType.Name;
              if (mapTo != regType)
              {
                  mapTo = " -&gt; " + mapTo;
              }
              else
              {
                  mapTo = string.Empty;
              }
              lifetime = lifetime.Substring(0, lifetime.Length - 
      > ```
      > 
      > ```
      >                "生命周期管理器".Length);
              Console.WriteLine("+ {0}{1}  '{2}'  {3}", regType, mapTo, 
      > ```
      > 
      > ```
      >               regName, lifetime);
          }
      }
      > ```

# 四、生命周期

1. 1. 使用生命周期
      
      1. 使用API
         
         > RegisterType&lt;ILogger, TraceSourceLogger&gt;( “UI”, new ContainerControlledLifetimeManager(), new InjectionConstructor(“UI”))
      2. 使用配置文件
         
         > ```
         > &lt;register type="IClass" mapTo="MyClass"&gt;
           &lt;lifetime type="transient" /&gt;
           &lt;!--&lt;lifetime type="SessionLifetimeManager"
               value="Session#1" typeConverter="SessionLifetimeConverter" /&gt;
         > ```
         > 
         > ```
         >   --&gt;
         &lt;/register&gt;<br>
         > ```
   2. 生命周期的类型
      
      1. **ContainerControlledLifetimeManager：**维护一个对象实例的强引用，每次调用的时候都会返回同一对象。
      2. **ExternallyControlledLifetimeManager：**作用类似于singleton，但是当实例没有被引用的时候，垃圾回收期将回收这个实例。 如果被GC回收后再次调用Resolve方法将会重新创建新的对象。调用GC的代码如下：
         
         > GC.Collect();
         
         配置文件如下：
         
         > &lt;register type=”IClass” mapTo=”MyClass” name=”ccl”&gt;  
         > &lt;lifetime type=”external” /&gt;  
         > &lt;/register&gt;
      3. **PerThreadLifetimeManager：**每个线程拥有一个实例。对于一个线程来说，作用类似于singleton。 一般来说不建议在使用RegisterInstance对已存在的对象注册关系时使用PerThreadLifetimeManager，因为此时的对象已经在一个线程内创建了，如果再使用这个生命周期管理器，将无法保证其正确调用。
         
         > &lt;register type=”IClass” mapTo=”MyClass” name=”ccl”&gt;  
         > &lt;lifetime type=”perthread” /&gt;  
         > &lt;/register&gt;
      4. **TransientLifetimeManager：**每次调用Resolve or ResolveAll或者属性注入的时候都实例化一个新实例时创建新实例。
         
         > &lt;register type=”IClass” mapTo=”MyClass” name=”ccl”&gt;  
         > &lt;lifetime type=”transient” /&gt;  
         > &lt;/register&gt;
      5. **HierarchicalLifetimeManager：**类似于ContainerControlledLifetimeManager，不同之处在于父容器和子容器维护各自的生命周期。也就是说，用子容器注册一个映射之后，用父容器和子容器解析解析出来的实例是不同的。Unity这种分级容器的好处就在于我们可以对于有不同生命周期的对象放在不同的容器中，如果一个子容器被释放，不会影响到其它子容器中的对象，但是如果根节点处父容器释放后，所有的子容器都将被释放。
         
         ```
         <register type="IClass" mapTo="MyClass" name="hl">
               <lifetime type="hierarchical">
         </register>
         ```
      6. **PerResolveLifetimeManager:**类似于TransientLifetimeManager，区别在于在第一调用的时候会创建一个新的对象，而再次通过循环引用访问到的时候就会返回先前创建的对象实例。如：

public class View : IView

```
{
    [Dependency]
    public IPresenter Presenter { get; set; }
}
public class MockPresenter : IPresenter
{
    [Dependency]
    public IView View { get; set; }
}
解析IView得到的实例Presenter 中的View与第一次解析IView的实例为同一个。
```