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>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?