`
pcajax
  • 浏览: 2106557 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

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
              {
              static void 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("123");
              }

              private static 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月份要准备期考啦,考完试我再做其他的系列教程吧,请大家届时关注,哈哈

                分享到:
                评论

                相关推荐

                Global site tag (gtag.js) - Google Analytics