entlib 5.0学习笔记 通过配置方式实现注入
1. 配置文件简介
2. 代码下载
1. 配置文件简介
1.1 . 配置文件结构
其中configSections的的作用概括如下:对于自定义的配置节,应该先在 <configSections>中声明要配置的节与类型,接着,在后面定义要配置的具体内容,正如定义一个变量。 简单的理解就是引入下面需要配置需要的类型。
1.2 通过实例学习配置选项
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="StocksTicker.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</sectionGroup>
<!-- TODO add unity section declaration here -->
<section name="unity" type=" Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration, Version=2.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
</configSections>
<applicationSettings>
<StocksTicker.Properties.Settings>
<setting name="StocksTicker_com_microsoft_office_MoneyCentralRemote"
serializeAs="String">
<value>http://office.microsoft.com/Research/Providers/MoneyCentral.asmx</value>
</setting>
</StocksTicker.Properties.Settings>
</applicationSettings>
<system.diagnostics>
<sources>
<source name="Service" switchValue="All">
<listeners>
<add name="file"
initializeData="service.log"
traceOutputOptions="DateTime"
type="System.Diagnostics.TextWriterTraceListener, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
</listeners>
</source>
<source name="UI" switchValue="All">
<listeners>
<add name="file"
initializeData="ui.log"
traceOutputOptions="DateTime"
type="System.Diagnostics.TextWriterTraceListener, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
</listeners>
</source>
</sources>
</system.diagnostics>
<!-- TODO add unity section here -->
<!-- xmlns用来代码提示 -->
<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" />
<!--
使用assembly或者是namespace能够减少
编写alias的工作量,entlib会自动在其
中查找相关类型
-->
<assembly name="StocksTicker"/>
<namespace name="StocksTicker.Loggers"/>
<namespace name="StocksTicker.StockQuoteServices"/>
<namespace name="StocksTicker.UI"/>
<container>
<register type="IStocksTickerView" mapTo="StocksTickerForm"/>
<register type="IStockQuoteService" mapTo="MoneyCentralStockQuoteService">
<!-- a child property element to configure the Logger property to be injected. -->
<property name="Logger"/>
</register>
<!-- ILogger interface to the ConsoleLogger class -->
<register type="ILogger" mapTo="ConsoleLogger"/>
<!-- register element to map the ILogger interface to the TraceSourceLogger using the "UI" name -->
<register name="UI" type="ILogger" mapTo="TraceSourceLogger">
<!--the built-in singleton alias-->
<lifetime type="singleton"/>
<constructor>
<param name="traceSourceName" value="UI"/>
</constructor>
</register>
<register type="StocksTickerPresenter">
<property name="Logger">
<!--
The property element configures the Logger
property to be injected with the value of
resolving the ILogger interface (the property’s
type), with the "UI" name
-->
<dependency name="UI"/>
</property>
</register>
</container>
</unity>
</configuration>