Unity Application Block Hands-on Lab for Enter-Lib 5.0:Lab 3-Using a Configuration File[Translation]
2012-02-17 13:19
441 查看
实验估计时间:25mins
先在这里下载实验代码。
任务1:使用配置文件保存Container的配置信息
在这次任务中,主要是用配置文件替换Container的创建代码。
添加引用的有:Microsoft.Practices.Unity.Configuration和System.Configuration。
打开Program.cs文件,添加代码usingMicrosoft.Practices.Unity.Configuration。将container的一堆RegisterType方法用LoadConfiguration方法替换。如下:
LoadConfiguration扩展方法是最简单方便的方式配置container。该方法从程序配置文件里检索相关的配置文件信息,而不是执行程序的配置文件。
更多信息关于container加载配置的内容请看Unity2.0文档中的"LoadingConfigurationFileInformationintoaContainer"http://msdn.microsoft.com/en-us/library/ff660935(v=PandP.20).aspx#load_config。
创建配置文件
下面主要使用到了一些Unity的配置元素,Unity配置元素的更多内容看Unity2.0文档中的"TheUnityConfigurationSchema"http://msdn.microsoft.com/en-us/library/ff660914(v=PandP.20).aspx。
打开App.config文件;
添加unity配置区域的声明:
unity这个name只是一个配置域的约定,任何名字都有效。
添加一个untiy的元素:
这个元素名字必须与注册unityconfigSections一致。
该untiy标签中的xmlns不是配置运行时必须的。假如你在unity标签中使用了该xmlns,VisualStudioXMLeditor加载了UnityConfigurationXSD文件,editor会自动提示标签,给你配置unity带来方便。
增加一个alias标签。
类型的别名不是必须的。类型的别名、assembly和namespace标签的作用都是减少配置文件的代码量,并便于阅读。.NET中已经有一些别名了,像int、string和singleton。
下面定义assembly和namespace:
添加一个container标签。
这是一个name为缺省值的container,被不带name的LoadConfiguration方法调用。
在container中添加一个register标签,将IStockTickerView接口与StocksTickerForm建立映射。
下面添加注册信息:
类似于上个实验调用API,运行程序……
介绍
在实验中,你将会学习通过程序配置文件去创建一个UnityContainer。使用配置文件有些类似于上篇实验中的调用配置API。实际上,配置文件方式可以看成脚本调用API。先在
任务1:使用配置文件保存Container的配置信息
在这次任务中,主要是用配置文件替换Container的创建代码。
添加引用的有:Microsoft.Practices.Unity.Configuration和System.Configuration。
打开Program.cs文件,添加代码usingMicrosoft.Practices.Unity.Configuration。将container的一堆RegisterType方法用LoadConfiguration方法替换。如下:
using(IUnityContainercontainer=newUnityContainer())
{
//TODOreplacewithapplicationofconfigurationinformationhere
//container
//.RegisterType<IStocksTickerView,StocksTickerForm>()
//.RegisterType<IStockQuoteService,MoneyCentralStockQuoteService>(
//newInjectionProperty("Logger"))
//.RegisterType<ILogger,ConsoleLogger>()
//.RegisterType<ILogger,TraceSourceLogger>(
//"UI",
//newContainerControlledLifetimeManager(),
//newInjectionConstructor("UI"))
//.RegisterType<StocksTickerPresenter>(
//newInjectionProperty(
//"Logger",
//newResolvedParameter<ILogger>("UI")));
container.LoadConfiguration();
StocksTickerPresenterpresenter
=container.Resolve<StocksTickerPresenter>();
Application.Run((Form)presenter.View);
}
LoadConfiguration扩展方法是最简单方便的方式配置container。该方法从程序配置文件里检索相关的配置文件信息,而不是执行程序的配置文件。
更多信息关于container加载配置的内容请看Unity2.0文档中的"LoadingConfigurationFileInformationintoaContainer"
创建配置文件
下面主要使用到了一些Unity的配置元素,Unity配置元素的更多内容看Unity2.0文档中的"TheUnityConfigurationSchema"
打开App.config文件;
添加unity配置区域的声明:
<sectionname="unity"
type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection,
Microsoft.Practices.Unity.Configuration,Version=2.0.414.0,Culture=neutral,PublicKeyToken=31bf3856ad364e35">
</section>
unity这个name只是一个配置域的约定,任何名字都有效。
添加一个untiy的元素:
<unityxmlns="http://schemas.microsoft.com/practices/2010/unity">
</unity>
configuration>
这个元素名字必须与注册unityconfigSections一致。
该untiy标签中的xmlns不是配置运行时必须的。假如你在unity标签中使用了该xmlns,VisualStudioXMLeditor加载了UnityConfigurationXSD文件,editor会自动提示标签,给你配置unity带来方便。
增加一个alias标签。
<unityxmlns="http://schemas.microsoft.com/practices/2010/unity">
<aliasalias="TraceSource"
type="System.Diagnostics.TraceSource,System,Version=2.0.0.0,Culture=neutral,PublicKeyToken=b77a5c561934e089"/>
</unity>
类型的别名不是必须的。类型的别名、assembly和namespace标签的作用都是减少配置文件的代码量,并便于阅读。.NET中已经有一些别名了,像int、string和singleton。
下面定义assembly和namespace:
<assemblyname="StockTicker"/>
<namespacename="StockTicker.Loggers"/>
<namespacename="StockTicker.StockQuoteServices"/>
<namespacename="StockTicker.UI"/>
</unity>
添加一个container标签。
<namespacename="StockTicker.UI"/>
<container></container>
这是一个name为缺省值的container,被不带name的LoadConfiguration方法调用。
在container中添加一个register标签,将IStockTickerView接口与StocksTickerForm建立映射。
下面添加注册信息:
<container>
<registertype="IStocksTickerView"mapTo="StocksTickerForm"/>
<registertype="IStockQuoteService"mapTo="RandomStockQuoteService">
<propertyname="Logger"></property>
</register>
<registertype="ILogger"mapTo="ConsoleLogger"/>
<registername="UI"type="ILogger"mapTo="TraceSourceLogger">
<lifetimetype="singleton"/>
<constructor>
<paramname="traceSourceName"value="UI"/>
</constructor>
</register>
<registertype="StocksTickerPresenter">
<propertyname="Logger">
<dependencyname="UI"/>
</property>
</register>
</container>
类似于上个实验调用API,运行程序……
相关文章推荐
- Unity Application Block Hands-on Lab for Enter-Lib 5.0:Lab 2-Using the Configuration API[Translation]
- Unity Application Block Hands-on Lab for Enter-Lib 5.0:Lab 1-Using a Unity Container[Translation]
- Unity Application Block Hands-on Lab for Enter-Lib 5.0:Lab 4-Configuring Containers[Translation]
- Unity Application Block Hands-on Lab for Enter-Lib 5.0:Lab 4-Configuring Containers[Translation]
- Caching Application Block Hands-On Lab(缓存对象持久化)
- This file is based on the Microsoft Data Access Application Block for .NET
- 黄聪:Microsoft Enterprise Library 5.0 系列教程(十) Configuration Application Block
- Sampled Softmax 论文笔记:On Using Very Large Target Vocabulary for Neural Machine Translation
- Step-by-Step:Installation and Configuration of a secure web application using MySQL,OpenSA Web Server, Tomcat and OpenSSL,on a N
- Configuration Management Application Block for .NET
- Using ICSharpCode.SharpZipLib for zip file
- Sampled Softmax 论文笔记:On Using Very Large Target Vocabulary for Neural Machine Translation
- Microsoft Enterprise Library 5.0 系列(十) Configuration Application Block
- Microsoft Enterprise Library 5.0 系列(十) Configuration Application Block
- Microsoft Enterprise Library 5.0 系列(十) Configuration Application Block
- Step-By-Step Guide To Create Physical Standby On Normal File System For ASM Primary using RMAN (文档 I
- On Using Very Large Target Vocabulary for Neural Machine Translation
- Microsoft Enterprise Library 5.0 系列教程(十) Configuration Application Block
- How to: Publish Metadata for a Service Using a Configuration File - Ref 1
- Microsoft Enterprise Library 5.0 系列 Configuration Application Block