Enterprise Library 2.0 - Hello IConfigurationSource
来源:互联网 发布:doug paisley知乎 编辑:程序博客网 时间:2024/05/22 00:48
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");
- Enterprise Library 2.0 - Hello IConfigurationSource
- Enterprise Library 2.0 中文教程
- Enterprise Library
- Enterprise Library
- Enterprise Library 2.0 -- Security Application Block
- Enterprise Library 2.0 -- Data Access Application Block
- Enterprise Library 2.0 -- Logging Application Block
- Enterprise Library 2.0 -- Cryptography Application Block
- Enterprise Library 2.0 -- Caching Application Block
- Enterprise Library 2.0学习之DAAB
- Enterprise Library 2.0 -- Cryptography Application Block
- Enterprise Library 2.0 -- Exception Handing Applcation Block
- Enterprise Library 2.0 -- Data Access Application Block
- Enterprise Library 2.0 -- Logging Application Block (上)
- Enterprise Library 2.0 -- Logging Application Block (下)
- Enterprise Library 2.0 -- Logging Application Block (补充)
- Enterprise Library 2.0 -- Security Application Block
- 对 Enterprise Library 2.0 进一步封装: DbHelperSQL2
- 多态原理
- parse_url解析 URL,返回其组成部分
- java中判断字符串是否为数字的三种方法
- 编辑GridView时如何控制列宽
- Ext 2.0布局实例
- Enterprise Library 2.0 - Hello IConfigurationSource
- table自动换行
- VS2005上使用ACE
- MiniGUI移植总结
- AJAX in Web Parts With MOSS
- 用Javascript 获取指定页面元素的位置
- My97DatePicker--YJControl-For-DONET 日历控件
- Mysql日期和时间函数大全
- C#-操作XML