企业库自定义config文件
大家都知道如何将企业库的配置信息写在Web.Config和App.Config,下面介绍如何将配置信息独立管理,并可以任意命名和任意放置这个config文件。
一般情况我们在config文件中这样写:
<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>
<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>
创建实例的代码:
Database northwind = DatabaseFactory.CreateDatabase();
Database northwind = DatabaseFactory.CreateDatabase("Northwind");
Database northwind = DatabaseFactory.CreateDatabase("Northwind");
其实,也可以这样去实现:
还是上面的config内容,但可以任意命名这个config文件,并保存到其他路径下,创建数据库实例时这样写:
IConfigurationSource source =
new FileConfigurationSource("My.config");
DatabaseProviderFactory factory =
new DatabaseProviderFactory(source);
Database northwind = factory.Create("Northwind");
new FileConfigurationSource("My.config");
DatabaseProviderFactory factory =
new DatabaseProviderFactory(source);
Database northwind = factory.Create("Northwind");
另外,我们还可以使用原来的代码块,基于一个自定义的config文件,去创建数据库实例。
App.config和web.config中要这样写
<configuration>
<configSections>
<section
name="enterpriseLibrary.ConfigurationSource"
type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ConfigurationSourceSection, Microsoft.Practices.EnterpriseLibrary.Common" />
</configSections>
<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>
</configuration>
<configSections>
<section
name="enterpriseLibrary.ConfigurationSource"
type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ConfigurationSourceSection, Microsoft.Practices.EnterpriseLibrary.Common" />
</configSections>
<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>
</configuration>
数据库连接配置仍然在My.config中,而代码中可以用原来的方法创建实例:
Database northwind = DatabaseFactory.CreateDatabase();
Database northwind = DatabaseFactory.CreateDatabase("Northwind");
Database northwind = DatabaseFactory.CreateDatabase("Northwind");