Unity 学习笔记(2) -- 配置文件的使用
2009-04-25 20:24
761 查看
在Unity的配置中,使用配置文件也是一种非常灵活的方式,毕竟能够通过修改配置文件的文本就能达到改动的目的,而不需要对源码进行改动、重新编译。使用配置文件对Unity进行配置,需要增加两个程序集的引用:System.Configuration和Microsoft.Practices.Unity.Configration,并且在代码中用相应的两个命名空间:
在configSections节点中,加入Unity的section配置信息
接下来,需要在configuration节点中增加Unity配置节点,格式如下:
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的例子,来实现用配置文件注册类型,配置文件示例:
使用配置文件,还有许多方便的地方,比如对于程序的扩展而言,新增的模块不再需要修改已编译好的程序,而只需要修改配置文件就可以方便的实现新模块的注册,对于系统的稳定性和可维护性都非常有好处。
usingSystem.Configuration;[code]usingMicrosoft.Practices.Unity.Configuration;.csharpcode,.csharpcodepre{font-size:small;color:black;font-family:consolas,"CourierNew",courier,monospace;background-color:#ffffff;/*white-space:pre;*/}.csharpcodepre{margin:0em;}.csharpcode.rem{color:#008000;}.csharpcode.kwrd{color:#0000ff;}.csharpcode.str{color:#006080;}.csharpcode.op{color:#0000c0;}.csharpcode.preproc{color:#cc6633;}.csharpcode.asp{background-color:#ffff00;}.csharpcode.html{color:#800000;}.csharpcode.attr{color:#ff0000;}.csharpcode.alt{background-color:#f4f4f4;width:100%;margin:0em;}.csharpcode.lnum{color:#606060;}此外,需要修改应用程序的配置文件:
在configSections节点中,加入Unity的section配置信息
<configSections>[code]...
<sectionname="unity"
type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection,Microsoft.Practices.Unity.Configuration,Version=1.2.0.0,Culture=neutral,PublicKeyToken=31bf3856ad364e35"/>
</configSections>.csharpcode,.csharpcodepre{font-size:small;color:black;font-family:consolas,"CourierNew",courier,monospace;background-color:#ffffff;/*white-space:pre;*/}.csharpcodepre{margin:0em;}.csharpcode.rem{color:#008000;}.csharpcode.kwrd{color:#0000ff;}.csharpcode.str{color:#006080;}.csharpcode.op{color:#0000c0;}.csharpcode.preproc{color:#cc6633;}.csharpcode.asp{background-color:#ffff00;}.csharpcode.html{color:#800000;}.csharpcode.attr{color:#ff0000;}.csharpcode.alt{background-color:#f4f4f4;width:100%;margin:0em;}.csharpcode.lnum{color:#606060;}其中name是section的名称,type就是处理该section的程序类型,Unity提供了UnityConfigurationSection,负责处理配置文件信息,它包含在程序集Microsoft.Practices.Unity.Configuration中
接下来,需要在configuration节点中增加Unity配置节点,格式如下:
<unity>[code]<typeAliases>
<typeAliasalias=""type=""/>
<typeAliases/>
<containers>
<container>
<types>
<typetype=""mapTo=""/>
</types>
<instances>
<addname=""type=""value=""/>
</instances>
</container>
</containers>
</unity>[/code].csharpcode,.csharpcodepre{font-size:small;color:black;font-family:consolas,"CourierNew",courier,monospace;background-color:#ffffff;/*white-space:pre;*/}.csharpcodepre{margin:0em;}.csharpcode.rem{color:#008000;}.csharpcode.kwrd{color:#0000ff;}.csharpcode.str{color:#006080;}.csharpcode.op{color:#0000c0;}.csharpcode.preproc{color:#cc6633;}.csharpcode.asp{background-color:#ffff00;}.csharpcode.html{color:#800000;}.csharpcode.attr{color:#ff0000;}.csharpcode.alt{background-color:#f4f4f4;width:100%;margin:0em;}.csharpcode.lnum{color:#606060;}.csharpcode,.csharpcodepre{font-size:small;color:black;font-family:consolas,"CourierNew",courier,monospace;background-color:#ffffff;/*white-space:pre;*/}.csharpcodepre{margin:0em;}.csharpcode.rem{color:#008000;}.csharpcode.kwrd{color:#0000ff;}.csharpcode.str{color:#006080;}.csharpcode.op{color:#0000c0;}.csharpcode.preproc{color:#cc6633;}.csharpcode.asp{background-color:#ffff00;}.csharpcode.html{color:#800000;}.csharpcode.attr{color:#ff0000;}.csharpcode.alt{background-color:#f4f4f4;width:100%;margin:0em;}.csharpcode.lnum{color:#606060;}
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的例子,来实现用配置文件注册类型,配置文件示例:
<?xmlversion="1.0"encoding="utf-8"?>[code]<configuration>
<configSections>
<sectionname="unity"type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection,Microsoft.Practices.Unity.Configuration"/>
</configSections>
<unity>
<containers>
<container>
<types>
<typetype="UnityDemo.IMonitor,UnityDemo"mapTo="UnityDemo.Monitor,UnityDemo"/>
<typetype="UnityDemo.INotify,UnityDemo"mapTo="UnityDemo.EmailNotify,UnityDemo"/>
</types>
</container>
</containers>
</unity>
</configuration>.csharpcode,.csharpcodepre{font-size:small;color:black;font-family:consolas,"CourierNew",courier,monospace;background-color:#ffffff;/*white-space:pre;*/}.csharpcodepre{margin:0em;}.csharpcode.rem{color:#008000;}.csharpcode.kwrd{color:#0000ff;}.csharpcode.str{color:#006080;}.csharpcode.op{color:#0000c0;}.csharpcode.preproc{color:#cc6633;}.csharpcode.asp{background-color:#ffff00;}.csharpcode.html{color:#800000;}.csharpcode.attr{color:#ff0000;}.csharpcode.alt{background-color:#f4f4f4;width:100%;margin:0em;}.csharpcode.lnum{color:#606060;}程序代码修改如下:
usingSystem;[code]
usingMicrosoft.Practices.Unity;
usingMicrosoft.Practices.Unity.Configuration;
usingSystem.Configuration;
namespaceUnityDemo
{
classProgram
{
staticvoidMain(string[]args)
{
IUnityContainercontainer=newUnityContainer();
UnityConfigurationSectionsection=(UnityConfigurationSection)ConfigurationManager.GetSection("unity");
section.Containers.Default.Configure(container);
IMonitormonitor=container.Resolve<IMonitor>();
monitor.Alarm();
Console.ReadLine();
}
}
}.csharpcode,.csharpcodepre{font-size:small;color:black;font-family:consolas,"CourierNew",courier,monospace;background-color:#ffffff;/*white-space:pre;*/}.csharpcodepre{margin:0em;}.csharpcode.rem{color:#008000;}.csharpcode.kwrd{color:#0000ff;}.csharpcode.str{color:#006080;}.csharpcode.op{color:#0000c0;}.csharpcode.preproc{color:#cc6633;}.csharpcode.asp{background-color:#ffff00;}.csharpcode.html{color:#800000;}.csharpcode.attr{color:#ff0000;}.csharpcode.alt{background-color:#f4f4f4;width:100%;margin:0em;}.csharpcode.lnum{color:#606060;}编译运行结果:
使用配置文件,还有许多方便的地方,比如对于程序的扩展而言,新增的模块不再需要修改已编译好的程序,而只需要修改配置文件就可以方便的实现新模块的注册,对于系统的稳定性和可维护性都非常有好处。
相关文章推荐
- Unity Application Block 学习笔记之一使用配置文件
- spring框架学习笔记3:使用注解代替配置文件
- MYSQL学习笔记(5)配置:使用选项文件
- WPF学习笔记--使用配置文件修改控件、面板颜色
- [原创]java WEB学习笔记99:Spring学习---Spring Bean配置:自动装配,配置bean之间的关系(继承/依赖),bean的作用域(singleton,prototype,web环境作用域),使用外部属性文件
- FPGA学习笔记之Altera FPGA使用JIC文件配置固化教程(转)
- Spring学习笔记(18)----使用Spring配置文件实现事务管理
- 【saltstack学习笔记之十】使用sls安装nginx并管理nginx的配置文件
- Unity学习笔记1-以配置文件方式注册组件
- Log4cplus配置文件的使用(学习笔记)
- [dotnetCore2.0]学习笔记之二: ASP.NET Core中,如何灵活使用静态文件和加载自定义配置
- Spring学习笔记 在XML配置文件中使用properties文件的键值
- Java框架spring 学习笔记(十):bean管理(注解和配置文件混合使用)
- java学习笔记6 - 使用commons configuration管理配置文件
- Spring-Boot学习笔记(二) 配置文件之初步使用
- Spring学习笔记 在XML配置文件中使用properties文件的键值
- Unity Application Block 学习笔记 之 在VS2012中 配置文件 自动感应
- lua学习笔记(4)——lua配置文件和table使用
- ITCAST视频-Spring学习笔记(使用Spring配置文件实现AOP)