Enterprise Library 2.0 - Hello IConfigurationSource

转载 2007年09月26日 17:00:00

Enterprise Library 2.0 - Hello IConfigurationSource

Enterprise Library 2.0 should be released sometime this month.  The latest version, the Enterprise Library 2.0 December Interim Community Drop, is quite stable and reliable enough to start playing with now.  I doubt there will be many, if any, changes between now and when it gets released this month.

 

Goodbye Configuration Application Block - Hello IConfigurationSource

The Configuration Application Block in Enterprise Library 1.0 has been replaced with an IConfigurationSource interface and two concrete classes that implement the interface, SystemConfigurationSource and FileConfigurationSource.  There is also a SqlConfigurationSource provided in one of the QuickStarts to show you how to build your own concrete implementation of IConfigurationSource, but it is not included in the core architecture.

The application blocks and your code utilize IConfigurationSource in order to properly access configuration information.  As you would expect, SystemConfigurationSource works with the App.config and Web.config in your winform and web applications, respectively, and FileConfigurationSource works with custom configuration files of your choice.

 

 

 

SystemConfigurationSource is Default

Chances are you will never have to think about IConfigurationSource in your applications as you will probably choose to put your application settings in App.config or Web.config , which Enterprise Library 2.0 is expecting by default.

Here is an example of an App.config that tells the Enterprise Library 2.0 Data Access Application Block that my default connection is to the Northwind Database on my local instance of SQL Server:

 

<configuration>
  <configSections>
    <section name="dataConfiguration"
type="Microsoft.Practices.EnterpriseLibrary.
Data.Configuration.DatabaseSettings, Microsoft.
Practices.EnterpriseLibrary.Data
" /> configSections> <connectionStrings> <add name="Northwind" providerName="System.Data.SqlClient" connectionString="Data Source=(local);Initial Catalog=
Northwind;Integrated Security=True
" /> connectionStrings> <dataConfiguration defaultDatabase="Northwind"/> configuration>

 

The part used by the Enterprise Library Data Access Application Block is this:

 

<dataConfiguration defaultDatabase="Northwind"/>

 

essentially telling DAAB that if a database name is unspecified use Northwind.

You can start using the Northwind Database straight away using the configuration above and the following static factory methods that have always been a part of the Enterprise Library Data Access Application Block:

 

Database northwind = DatabaseFactory.CreateDatabase();
Database northwind = DatabaseFactory.CreateDatabase("Northwind");

 

The static factory saves you the work of specifying the following:

 

IConfigurationSource source = new SystemConfigurationSource();
    
DatabaseProviderFactory factory = new DatabaseProviderFactory(source);
    
Database northwind = factory.Create("Northwind");

 

FileConfigurationSource

If you want to place the configuration information in a custom file, “My.config”, you can use the following code:

 

IConfigurationSource source = new FileConfigurationSource("My.config");
    
DatabaseProviderFactory factory = new DatabaseProviderFactory(source);
    
Database northwind = factory.Create("Northwind");

 

However, that gets a little tiresome.  Better that we change the default settings of Enterprise Library 2.0 so that it uses FileConfigurationSource as the default IConfigurationSource and let it know to use “My.config.”

You can do this by adding a couple of things in your App.config or Web.config depending on your situation.

First, register a new config section:

 

<section
    name="enterpriseLibrary.ConfigurationSource"
    type="Microsoft.Practices.EnterpriseLibrary.Common.
Configuration.ConfigurationSourceSection,
Microsoft.Practices.EnterpriseLibrary.Common" />

 

You then add the new section telling Enterprise Library 2.0 that we want the default setting to be FileConfigurationSource and the default file to be “My.config“:

 

<enterpriseLibrary.ConfigurationSource selectedSource="fileSource">
   <sources>
       <add 
         name="fileSource"
         type="Microsoft.Practices.EnterpriseLibrary.
Common.Configuration.FileConfigurationSource,
Microsoft.Practices.EnterpriseLibrary.Common
" filePath ="My.config" /> <add name="systemSource" type="Microsoft.Practices.EnterpriseLibrary.
Common.Configuration.SystemConfigurationSource,
Microsoft.Practices.EnterpriseLibrary.Common
" /> sources> enterpriseLibrary.ConfigurationSource>

 

These changes will now allow us to use the same static factory methods for the Enterprise Library Data Access Application Block using configuration settings in My.Config.

 

Database northwind = DatabaseFactory.CreateDatabase();
Database northwind = DatabaseFactory.CreateDatabase("Northwind");
 

C#利用微软企业库Enterprise Library操作mysql数据库

在C#项目中,很多时候到要用到Enterprise Library。这里只是用一个很简单的小例子来演示一下Enterprise Library在VS2010中操作mysql数据库的流程。 1,利用E...
  • ZHM977863924
  • ZHM977863924
  • 2015年02月05日 16:48
  • 2816

OS X升级到10.10之后使用pod出现问题的解决方法

最新对mac 10.10的强大功能好奇,于是将系统升级到了10.10,结果发现使用pod出现了下面的问题:/System/Library/Frameworks/Ruby.framework/Versi...
  • dqjyong
  • dqjyong
  • 2014年07月19日 11:28
  • 28596

一个cocoapods问题的解决,希望能帮助到遇到类似情况的人

之前10.7的系统上运行过cocoapods没有问题。现在系统版本升级到了10.9,尝试使用cocoapods遇到问题,报告了类似下面的错误: Psych::SyntaxError - (/Us...
  • openglnewbee
  • openglnewbee
  • 2014年04月30日 16:27
  • 7565

Enterprise Library2.0

  • 2007年04月25日 13:07
  • 3.55MB
  • 下载

C#利用微软企业库Enterprise Library操作mysql数据库

在C#项目中,很多时候到要用到Enterprise Library。这里只是用一个很简单的小例子来演示一下Enterprise Library在VS2010中操作mysql数据库的流程。 1,利用E...
  • ZHM977863924
  • ZHM977863924
  • 2015年02月05日 16:48
  • 2816

在数据库访问项目中使用微软企业库Enterprise Library,实现多种数据库的支持

在我们开发很多项目中,数据访问都是必不可少的,有的需要访问Oracle、SQLServer、Mysql这些常规的数据库,也有可能访问SQLite、Access,或者一些我们可能不常用的PostgreS...
  • dayu9216
  • dayu9216
  • 2017年11月06日 10:07
  • 640

Enterprise Library 5 数据访问之一(初级)体验篇:直接返回业务对象数据

功能描述:在执行玩一条sql语句后如果直接将结果的DataTable或Datsset,DataReader这些东西省掉直接返回我们一个业务对象岂不是更爽。Enterprise Library 5 已经...
  • liushengpiaoxu
  • liushengpiaoxu
  • 2011年07月11日 15:00
  • 1695

DbHelperSQL Enterprise Library

转自:http://blog.csdn.net/litp/article/details/1495802 Enterprise Library 2.0很多人都已熟悉,有的人也在很多项目里做了应用, ...
  • fengchao1000
  • fengchao1000
  • 2012年07月06日 22:55
  • 573

C#日志扩展包之Microsoft Enterprise Library - Logging Application Block之配置

配置Logging Application Block可以使用两种方法:1、使用app.config文件配置,xml编辑器或Enterprise Library Configuration Tool;...
  • jianglgang
  • jianglgang
  • 2015年08月17日 17:38
  • 364

Enterprise Library 企业库

1. Enterprise  Library 企业库介绍         是微软模式与 实践团队 开的应用程序块        安装Enterprise  Library           ...
  • qq_35019337
  • qq_35019337
  • 2017年04月10日 14:44
  • 229
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Enterprise Library 2.0 - Hello IConfigurationSource
举报原因:
原因补充:

(最多只允许输入30个字)