原文:黄聪:Microsoft Enterprise Library 5.0 系列教程(十) Configuration Application Block

到目前为止,我们使用的模块都是在同一个配置文件中,这显然是不灵活的,我们希望每个功能模块能独立的在一个配置文件中体现,这样不仅易于管理,易于扩展,也易于阅读和分类.企业库当然考虑到了这个问题并且提供了解决方案,本章就是讲解如何使用Configuration Application Block来将各个模块分割到独立的配置文件中的.

和以前一样,我们先提需求,再用实例做讲解,这样比较形象生动些:

  1)      创建一个工程,里面用到企业库的Data 数据访问模块和Log日志处理模块

  2)      2个模块的配置分别保存在不同的配置文件中(Data.config和Log.config中)

  3)      在程序中调用这2个模块

1. 用VS 2008新建一个控制台应用程序.接着运行EntLibConfig.exe, 选择Blocks菜单 ,单击Add Data Settings .并设置Connection String属性,可以随意设置一个连接字符串,在此只做测试使用,配置好后保存成Data.config,保存到你的控制台程序目录下:

2. 再打开一个EntLibConfig.exe. 选择Blocks菜单 ,单击Add Logging Settings .设置如下所示,日志模块的详细介绍请参考我之前的文章.配置好之后保存成Log.config,并保存到你的控制台程序目录下.

3.   再打开一个EntLibConfig.exe. 选择Blocks菜单 ,单击Add Configuration Settings . 点击Sources面板右上角的加号按钮—Add SourcesAdd File-based Configuration Source.设置创建好的File-based Configuration SourceName属性为Data ConfigurationSource.接着将File Path属性设置为前面创建好的Data.config文件: 

 

4.   点击Redirected Sections面板右上角的加号按钮—Add Redirected Sections.设置Configuration Source的属性为Data Configuration Source: 

5. 同3,4步一样,我们再设置Log模块的配置,最后设置如下图所示:

6. 好了,保存该配置文件为App.config,同样保存到前面建立好的控制台程序目录下,这时候你的程序目录下应该有如下3个文件啦:

7. 创建一个新的控制台应用程序,将App.config添加到程序内,并加入需要的Dll文件:

添加引用:

using System.Configuration;using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;using Microsoft.Practices.EnterpriseLibrary.Data;using Microsoft.Practices.EnterpriseLibrary.Common.Configuration.Manageability;using Microsoft.Practices.EnterpriseLibrary.Data.Configuration;using Microsoft.Practices.EnterpriseLibrary.Logging;

8. 测试:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Configuration;using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;using Microsoft.Practices.EnterpriseLibrary.Data;using Microsoft.Practices.EnterpriseLibrary.Common.Configuration.Manageability;using Microsoft.Practices.EnterpriseLibrary.Data.Configuration;using Microsoft.Practices.EnterpriseLibrary.Logging;namespace test{    class Program    {        staticvoid Main(string[] args)        {            //从Data.config配置文件获取设置            DatabaseProviderFactory dbFactory =                new DatabaseProviderFactory(GetFileConfigurationSource("Data Configuration Source"));            Database db = dbFactory.Create("Connection String");            Console.WriteLine(db.ConnectionString);            //从Log.config的配置文件获取设置            LogWriter lw =new LogWriterFactory(GetFileConfigurationSource("Log Configuration Source")).Create();            lw.Write("");        }        privatestatic FileConfigurationSource GetFileConfigurationSource(string SourceName)        {            //获取App.config            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);            //获取资源节点集合            ConfigurationSourceSection section =                (ConfigurationSourceSection)config.GetSection(ConfigurationSourceSection.SectionName);            //获取重定向配置文件资源配置节点            FileConfigurationSourceElement elem =                (FileConfigurationSourceElement)section.Sources.Get(SourceName);            //获取重定向配置文件资源            FileConfigurationSource fileSource =new FileConfigurationSource(elem.FilePath);            return fileSource;        }    }}

9. 运行结果:

打开rolling.log可以看见如下内容:

至此,我们就解决了前面提出的所有需求啦,我们将数据库模块和日志模块的配置分别创建在Data.config和Log.config文件中,并用App.config作为字典来查询各个模块和配置文件的从属关系,这样使得各个模块间的耦合度大大降低,简化了管理.好了,Microsoft Enterprise Library的系列教程到此算是告一段落了.6,7月份要准备期考啦,考完试我再做其他的系列教程吧,请大家届时关注,哈哈

黄聪:Microsoft Enterprise Library 5.0 系列教程(十) Configuration Application Block的更多相关文章

  1. 黄聪:Microsoft Enterprise Library 5.0 系列教程(六) Security Application Block

    原文:黄聪:Microsoft Enterprise Library 5.0 系列教程(六) Security Application Block 开发人员经常编写需要安全功能的应用程序.这些应用程序 ...

  2. 黄聪:Microsoft Enterprise Library 5.0 系列教程(四) Logging Application Block

    原文:黄聪:Microsoft Enterprise Library 5.0 系列教程(四) Logging Application Block 企业库日志应用程序模块工作原理图:   从上图我们可以 ...

  3. 黄聪:Microsoft Enterprise Library 5.0 系列教程(三) Validation Application Block (高级)

    原文:黄聪:Microsoft Enterprise Library 5.0 系列教程(三) Validation Application Block (高级) 企业库验证应用程序模块之配置文件模式: ...

  4. 黄聪:Microsoft Enterprise Library 5.0 系列教程(三) Validation Application Block (初级)

    原文:黄聪:Microsoft Enterprise Library 5.0 系列教程(三) Validation Application Block (初级) 企业库提供了一个很强大的验证应用程序模 ...

  5. 黄聪:Microsoft Enterprise Library 5.0 系列教程(二) Cryptography Application Block (高级)

    原文:黄聪:Microsoft Enterprise Library 5.0 系列教程(二) Cryptography Application Block (高级) 本章介绍的是企业库加密应用程序模块 ...

  6. 黄聪:Microsoft Enterprise Library 5.0 系列教程(二) Cryptography Application Block (初级)

    原文:黄聪:Microsoft Enterprise Library 5.0 系列教程(二) Cryptography Application Block (初级) 企业库加密应用程序模块提供了2种方 ...

  7. 黄聪:Microsoft Enterprise Library 5.0 系列教程(一) Caching Application Block (高级)

    原文:黄聪:Microsoft Enterprise Library 5.0 系列教程(一) Caching Application Block (高级) Caching Application Bl ...

  8. 黄聪:Microsoft Enterprise Library 5.0 系列教程(一) : Caching Application Block (初级)

    原文:黄聪:Microsoft Enterprise Library 5.0 系列教程(一) : Caching Application Block (初级) 本篇文章具体官方解释请参照以下链接: h ...

  9. Microsoft Enterprise Library 5.0 系列教程(四) Logging Application Block

    Download dll: http://www.microsoft.com/en-us/download/confirmation.aspx?id=15104 http://www.cnblogs. ...

随机推荐

  1. javacsript (十) 数据类型

    类型声明的时候,直接使用 var  varname=xxx 数字.字符.布尔,声明都直接使用var就可以, JavaScript 数组 下面的代码创建名为 cars 的数组: var cars=new ...

  2. Net 并行知识学习

    园子里有很多介绍并行库TPL的这里列举2个 1 并行之Task讲解:http://www.cnblogs.com/yanyangtian/archive/2010/05/21/1741193.html ...

  3. qt新进程工作目录的设置(工作目录确实是被子进程继承的,但也可以设置)

    经过试验,qt启动一个新的进程时,这个进程的工作目录是继承父进程的,无论是通过start还是startDetached来启动. 其实对于linux系统,qt底层应该也是调用fork.exec之类的函数 ...

  4. 一场刺激的游戏——很文艺的山东省第四届ACM赛总结(菜鸟版)

               人生就像一个个节点,节点中或许有成功,失败,满足,遗憾,但是只要它是不可复制的,在日后,便是美好.                                         ...

  5. 基于Predictive Parsing的ABNF语法分析器(十)——AbnfParser文法解析器之数值类型(num-val)

    ANBF语法中的数值类型有3种:二进制.十进制和十六进制,可以是一个以点号分隔的数列,也可以是一个数值的范围.例如,%d11.22.33.44.55表示五个有次序的十进制数字“11.22.33.44. ...

  6. Swift - 使用表格组件(UITableView)实现单列表

    1,样例说明: (1)列表内容从Controls.plist文件中读取,类型为Array. (2)点击列表项会弹出消息框显示该项信息. (3)按住列表项向左滑动,会出现删除按钮.点击删除即可删除该项. ...

  7. 五、Linux/UNIX操作命令积累【cp、mv、cat、grep、ps】

    在使用Linux/UNIX下,常常会使用文本界面去设置系统或操作系统,作者本人在工作的过程也在不断接触这方面的命令,所以为此特酝酿.准备.開始了本文的编写.本文主要记录自己平时遇到的一些Linux/U ...

  8. 数字证书, 数字签名, SSL(TLS) , SASL .

    因为项目中要用到TLS + SASL 来做安全认证层. 所以看了一些网上的资料, 这里做一个总结. 1. 首先推荐几个文章: 数字证书: http://www.cnblogs.com/hyddd/ar ...

  9. Web Api集成Swagger

    WebApi集成Swagger 1.新建一个WebApi空项目 2.新建一个Person实体类: public class Person { public int ID { get; set; } p ...

  10. ThinkPHP页面跳转、Ajax技巧详细介绍(十八)

    原文:ThinkPHP页面跳转.Ajax技巧详细介绍(十八) ThinkPHP页面跳转.Ajax技巧详细介绍 一.页面跳转 $this->success('查询成功',U('User/test' ...