實(shí)驗(yàn)估計(jì)時(shí)間:25mins
?
介紹
在實(shí)驗(yàn)中,你將會(huì)學(xué)習(xí)通過程序配置文件去創(chuàng)建一個(gè)Unity Container。使用配置文件有些類似于上篇實(shí)驗(yàn)中的調(diào)用配置API。實(shí)際上,配置文件方式可以看成腳本調(diào)用API。
先在 這里 下載實(shí)驗(yàn)代碼。
?
任務(wù)1:使用配置文件保存Container的配置信息
在這次任務(wù)中,主要是用配置文件替換Container的創(chuàng)建代碼。
添加引用的有:Microsoft.Practices.Unity.Configuration和System.Configuration。
打開Program.cs文件,添加代碼 using Microsoft.Practices.Unity.Configuration。將container的一堆RegisterType方法用LoadConfiguration方法替換。如下:
using (IUnityContainer container = new UnityContainer())
{
// TODO replace with application of configuration information here
//container
// .RegisterType<IStocksTickerView, StocksTickerForm>()
// .RegisterType<IStockQuoteService, MoneyCentralStockQuoteService>(
// new InjectionProperty("Logger"))
// .RegisterType<ILogger, ConsoleLogger>()
// .RegisterType<ILogger, TraceSourceLogger>(
// "UI",
// new ContainerControlledLifetimeManager(),
// new InjectionConstructor("UI"))
// .RegisterType<StocksTickerPresenter>(
// new InjectionProperty(
// "Logger",
// new ResolvedParameter<ILogger>("UI")));
?
container.LoadConfiguration();
?
StocksTickerPresenter presenter
= container.Resolve<StocksTickerPresenter>();
?
Application.Run((Form)presenter.View);
}
LoadConfiguration擴(kuò)展方法是最簡(jiǎn)單方便的方式配置container。該方法從程序配置文件里檢索相關(guān)的配置文件信息,而不是執(zhí)行程序的配置文件。
更多信息關(guān)于container加載配置的內(nèi)容請(qǐng)看Unity2.0文檔中的"Loading Configuration File Information into a Container" http://msdn.microsoft.com/en-us/library/ff660935(v=PandP.20).aspx#load_config 。
創(chuàng)建配置文件
下面主要使用到了一些Unity的配置元素,Unity配置元素的更多內(nèi)容看Unity2.0文檔中的"The Unity Configuration Schema" http://msdn.microsoft.com/en-us/library/ff660914(v=PandP.20).aspx 。
打開App.config文件;
添加unity配置區(qū)域的聲明:
<section name=
"unity"
type=
"Microsoft.Practices.Unity.Configuration.UnityConfigurationSection,
Microsoft.Practices.Unity.Configuration, Version=2.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35">
</section>
unity這個(gè)name只是一個(gè)配置域的約定,任何名字都有效。
添加一個(gè)untiy的元素:
<unity xmlns=
"http://schemas.microsoft.com/practices/2010/unity"
>
</unity>
configuration>
這個(gè)元素名字必須與注冊(cè)u(píng)nity configSections一致。
該untiy標(biāo)簽中的xmlns不是配置運(yùn)行時(shí)必須的。假如你在unity標(biāo)簽中使用了該xmlns,Visual Studio XML editor加載了Unity Configuration XSD文件,editor會(huì)自動(dòng)提示標(biāo)簽,給你配置unity帶來方便。
增加一個(gè)alias標(biāo)簽。
<unity xmlns=
"http://schemas.microsoft.com/practices/2010/unity"
>
<alias alias=
"TraceSource"
type=
"System.Diagnostics.TraceSource, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
/>
</unity>
類型的別名不是必須的。類型的別名、assembly和namespace標(biāo)簽的作用都是減少配置文件的代碼量,并便于閱讀。.NET中已經(jīng)有一些別名了,像int、string和singleton。
下面定義assembly和namespace:
<assembly name=
"StockTicker"
/>
< namespace name= "StockTicker.Loggers" />
< namespace name= "StockTicker.StockQuoteServices" />
< namespace name= "StockTicker.UI" />
</unity>
添加一個(gè)container標(biāo)簽。
< namespace name= "StockTicker.UI" />
?
<container></container>
這是一個(gè)name為缺省值的container,被不帶name的LoadConfiguration方法調(diào)用。
在container中添加一個(gè)register標(biāo)簽,將IStockTickerView接口與StocksTickerForm建立映射。
下面添加注冊(cè)信息:
<container>
<register type= "IStocksTickerView" mapTo= "StocksTickerForm" />
<register type= "IStockQuoteService" mapTo= "RandomStockQuoteService" >
<property name=
"Logger"
></property>
</register>
<register type= "ILogger" mapTo= "ConsoleLogger" />
<register name= "UI" type= "ILogger" mapTo= "TraceSourceLogger" >
<lifetime type=
"singleton"
/>
<constructor>
<param name= "traceSourceName" value = "UI" />
</constructor>
</register>
<register type=
"StocksTickerPresenter"
>
<property name=
"Logger"
>
<dependency name=
"UI"
/>
</property>
</register>
</container>
類似于上個(gè)實(shí)驗(yàn)調(diào)用API,運(yùn)行程序……
Unity Application Block Hands-on Lab for Enter-Lib 5.0:Lab 3-Using a Configuration File[Translation]
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元
