[转载]Enterprise Library 2.0 - 从配置应用程序块(Configuration Block )到IConfigurationSource接口(SystemConfigurati...

Enterprise Library 2.0 - From Configuration Block to IConfigurationSource

by David Hayden ( Florida .NET Developer )

 

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 using now.  I doubt there will be many, if any, changes between now and when Enterprise Library 2.0 gets released this month.

 

Goodbye Configuration Application Block - Hello IConfigurationSource

The Configuration Application Block, which was once a big part of Enterprise Library, is gone.  I think everyone is happy about that.  Although great in theory, the Configuration Application Block greatly complicated the deployment of the other application blocks.

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.

 

enterpriselibraryconfig.jpg

 

 

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:

 










 

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

 

 
 

 

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 facade methods that have always been a part of the Enterprise Library Data Access Application Block:

 

 

The facade saves you the work of specifying the following:

 



 

 

FileConfigurationSource

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

 



 

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:

 


 

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“:

 









 

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

 

 

Conclusion

The new IConfigurationSource, SystemConfigurationSource, and FileConfigurationSource used in Enterprise Library 2.0 is a breath of fresh air over the old Configuration Application Block in Enterprise Library 1.0.

 

Source:  David Hayden ( Florida .NET Developer )

转载于:https://www.cnblogs.com/rippleyong/archive/2006/03/27/360073.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值