Unity 学习笔记(2) — 配置文件的使用

779 views 四月 25, 09 by Timothy

在Unity的配置中,使用配置文件也是一种非常灵活的方式,毕竟能够通过修改配置文件的文本就能达到改动的目的,而不需要对源码进行改动、重新编译。使用配置文件对Unity进行配置,需要增加两个程序集的引用:System.Configuration和Microsoft.Practices.Unity.Configration,并且在代码中用相应的两个命名空间:

   1:  using System.Configuration;
   2:  using Microsoft.Practices.Unity.Configuration;

此外,需要修改应用程序的配置文件:

在configSections节点中,加入Unity的section配置信息


...
type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration, Version=1.2.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
<configSections>
...

type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration, Version=1.2.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>

其中name是section的名称,type就是处理该section的程序类型,Unity提供了UnityConfigurationSection,负责处理配置文件信息,它包含在程序集Microsoft.Practices.Unity.Configuration中

接下来,需要在configuration节点中增加Unity配置节点,格式如下:

   1:  
   2:      
   3:        "" type="" />
   4:      
   5:      
   6:        
   7:           
   8:             "" mapTo="" />
   9:           
  10:           
  11:             "" type="" value="" />
  12:           
  13:        
  14:      
  16:  

unity的子元素,包含节点大致如上,其宗typeAliases是type别名,能够简化下面types中type的配置。containers节点中可以包含多个container的配置。container主要包含的子元素有types元素,instance元素,types元素可以包含多个type元素,用以添加注册类型,instance主要用来添加实例到容器中。type元素,主要包含四个属性:

name:表示注册类型的名称,此属性在配置中可选。

type:注册的源类型

mapto:注册的目标类型

lifetime:设置注册类型的生命周期

此外,还有instances元素,包括name,type,value,typeConverter四个属性。value表示注册实例的初始值,typeConverter是用以转换提供的值到实例的匹配类型的类型转换器。

具体的元素含义,可以参考Unity的帮助文档。

下面我们还是采用Monitor的例子,来实现用配置文件注册类型,配置文件示例:

   1:  "1.0" encoding="utf-8" ?>
   2:  
   3:    
   4:  
"unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration" />
   5:    
   6:  
   7:    
   8:      
   9:        
  10:          
  11:            "UnityDemo.IMonitor,UnityDemo" mapTo="UnityDemo.Monitor,UnityDemo" />
  12:            "UnityDemo.INotify,UnityDemo" mapTo="UnityDemo.EmailNotify,UnityDemo" />
  13:          
  14:        
  15:      
  16:    
  17:  
  18:  

程序代码修改如下:

   1:  using System;
   2:  
   3:  using Microsoft.Practices.Unity;
   4:  using Microsoft.Practices.Unity.Configuration;
   5:  using System.Configuration;
   6:  
   7:  namespace UnityDemo
   8:  {
   9:      class Program
  10:      {
  11:          static void Main(string[] args)
  12:          {
  13:              IUnityContainer container = new UnityContainer();
  14:              UnityConfigurationSection section = (UnityConfigurationSection)ConfigurationManager.GetSection("unity");
  15:              section.Containers.Default.Configure(container);
  16:  
  17:              IMonitor monitor = container.Resolve();
  18:              monitor.Alarm();
  19:  
  20:              Console.ReadLine();
  21:          }
  22:      }
  23:  }

编译运行结果:

使用配置文件,还有许多方便的地方,比如对于程序的扩展而言,新增的模块不再需要修改已编译好的程序,而只需要修改配置文件就可以方便的实现新模块的注册,对于系统的稳定性和可维护性都非常有好处。

分享到:

声明: 此Blog中的文章和随笔仅代表作者在某一特定时间内的观点和结论,对其完全的正确不做任何担保或假设
本站文章均采用 知识共享署名-相同方式共享3.0 协议进行授权,除非注明,本站文章均为原创,转载请注明转自 Timothy's Space 并应以链接形式标明本文地址!

你可能也对下列文章感兴趣


Add your comment

4 Responses to "Unity 学习笔记(2) — 配置文件的使用"

  1. zhangpeng chen CHINA Mozilla Firefox Windows 说道:

    谢谢你,这三篇关于Unity的好文章。 : )

  2. Cooldog CHINA Google Chrome Windows 说道:

    @zhangpeng chen
    我也是一边学一边试的,有问题一起交流吧

  3. sagaris CHINA Google Chrome Windows 说道:

    看了你的文章 ,谢谢分享!


Leave a Reply

 您已输入0

(Ctrl+Enter)