一区二区三区日韩精品-日韩经典一区二区三区-五月激情综合丁香婷婷-欧美精品中文字幕专区

分享

企業(yè)庫中 DatabaseFactory.CreateDatabase 方法創(chuàng)建數(shù)據(jù)庫實例的

 woshishenxiande 2011-07-02
 企業(yè)庫中 DatabaseFactory.CreateDatabase 方法創(chuàng)建數(shù)據(jù)庫實例的 邏輯過程

在 Enterprise Library 的 數(shù)據(jù)訪問應(yīng)用塊中:
Microsoft.Practices.EnterpriseLibrary.Data.Database 抽象類派生出了以下三個數(shù)據(jù)庫類

     Microsoft.Practices.EnterpriseLibrary.Data.GenericDatabase
     Microsoft.Practices.EnterpriseLibrary.Data.Oracle.OracleDatabase
     Microsoft.Practices.EnterpriseLibrary.Data.Sql.SqlDatabase

從這一點知識我們就可以知道

Database dbSvc = DatabaseFactory.CreateDatabase();

上述代碼創(chuàng)建的 dbSvc 并不是 Microsoft.Practices.EnterpriseLibrary.Data.Database 抽象類的實例,而應(yīng)該是下面這三個類的其中一個實例。

     Microsoft.Practices.EnterpriseLibrary.Data.GenericDatabase
     Microsoft.Practices.EnterpriseLibrary.Data.Oracle.OracleDatabase
     Microsoft.Practices.EnterpriseLibrary.Data.Sql.SqlDatabase
或者是你自己新寫的一個派生自 Database 抽象類的子類。

我們再通過查看企業(yè)庫的源代碼簡單看一下 DatabaseFactory.CreateDatabase 方法 是根據(jù)啥關(guān)系,來判斷應(yīng)該創(chuàng)建的是 Database 抽象類的那個子類。

分析代碼可知,這里的判斷分三步:
主要代碼看 DatabaseCustomFactory.cs 文件的
public object CreateObject(IBuilderContext context, string name, IConfigurationSource configurationSource, ConfigurationReflectionCache reflectionCache)方法。

第一步:
在鏈接字符串中,我們可以根據(jù)鏈接字符串的 name 獲得 鏈接字符串的 providerName 。

第二步:
如果 我們設(shè)置了 providerName 的任何 影射關(guān)系, 則自動獲得 這個映射的 DbProviderMapping ;
如果這個數(shù)據(jù)庫訪問者 沒有被設(shè)置任何影射關(guān)系, 則 系統(tǒng)自動在默認提供的 SqlDatabase、 OracleDatabase 中匹配。
如果上述都沒匹配出結(jié)果, 則 返回 GenericDatabase 。

上述邏輯在 DatabaseConfigurationView.cs 文件中可以看到代碼:


public DbProviderMapping GetProviderMapping(string name, string dbProviderName)
{
DatabaseSettings settings = this.DatabaseSettings;
if (settings != null)
{
   DbProviderMapping existingMapping = settings.ProviderMappings.Get(dbProviderName);
   if (existingMapping != null)
   {
    return existingMapping;
   }
}

DbProviderMapping defaultMapping = this.GetDefaultMapping(name, dbProviderName);
if (defaultMapping != null)
{
   return defaultMapping;
}

return this.GetGenericMapping();
}

第三步
根據(jù) DbProviderMapping 生成 Database 抽象類的實例。

 

根據(jù)上述邏輯描述,我們就可以理解如下一個數(shù)據(jù)庫的配置文件了。

<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
    <section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null" />
</configSections>
<dataConfiguration defaultDatabase="MyTestConnectionString">
    <providerMappings>
      <add databaseType="Microsoft.Practices.EnterpriseLibrary.Data.GenericDatabase, Microsoft.Practices.EnterpriseLibrary.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null"
        name="GenericDatabase" />
      <add databaseType="Microsoft.Practices.EnterpriseLibrary.Data.Sql.SqlDatabase, Microsoft.Practices.EnterpriseLibrary.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null"
        name="System.Data.SqlClient" />
    </providerMappings>
</dataConfiguration>
<connectionStrings>
    <add name="MyTestConnectionString" connectionString="server=(local)\SQLEXPRESS;database=EntLibQuickStarts;Integrated Security=true;"
      providerName="GenericDatabase" />
</connectionStrings>
<system.data>
    <DbProviderFactories>
      <add name="my Generic Database" invariant="GenericDatabase" description="An alias for the SqlProvider" type="System.Data.SqlClient.SqlClientFactory, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
    </DbProviderFactories>
</system.data>
</configuration>

這個數(shù)據(jù)庫配置文件生成的 Database dbSvc = DatabaseFactory.CreateDatabase();
dbSvc 必然是 Microsoft.Practices.EnterpriseLibrary.Data.GenericDatabase 類型的。

注意:
system.data 數(shù)據(jù)節(jié) 的 DbProviderFactories 配置節(jié) 是給配置文件中 connectionStrings 節(jié)的 providerName 對應(yīng)用的。
而 dataConfiguration 配置節(jié)的 providerMappings 是給 生成 的 是那一個 Database 抽象類的那一個實例用的。

參考資料:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnpag2/html/entlibjan2006_dataaccessappblock.asp
http://blogs./tomholl/archive/2005/09/14/466298.aspx
http://www./VB_NET/re-9165_DAAB_in_Enterprise_Library_for_NET_2_0.aspx

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多

    日本熟妇熟女久久综合| 日韩性生活片免费观看| 精品al亚洲麻豆一区| 亚洲超碰成人天堂涩涩| 国产日韩中文视频一区| 日韩精品一级一区二区| 欧美一本在线免费观看| 亚洲视频在线观看免费中文字幕| 小草少妇视频免费看视频| 亚洲高清亚洲欧美一区二区| 国产精品香蕉一级免费| 日韩欧美第一页在线观看| 中文精品人妻一区二区| 偷拍洗澡一区二区三区| 亚洲一区二区三区三区| 日韩欧美一区二区久久婷婷 | 国产欧美韩日一区二区三区| 亚洲中文字幕在线乱码av| 99精品国产一区二区青青 | 99秋霞在线观看视频| 婷婷开心五月亚洲综合| 久久青青草原中文字幕| 亚洲中文字幕一区三区| 夫妻性生活黄色录像视频| 欧美一区二区不卡专区| 狠狠干狠狠操在线播放| 成人免费高清在线一区二区| 色鬼综合久久鬼色88| 好吊妞视频这里有精品| 粉嫩一区二区三区粉嫩视频| av在线免费播放一区二区| 99精品国产自在现线观看| 亚洲欧美日本国产不卡| 国产中文字幕一二三区| 最好看的人妻中文字幕| 色婷婷日本视频在线观看| 亚洲中文字幕剧情在线播放| 69久久精品亚洲一区二区| 欧美激情区一区二区三区| 男女激情视频在线免费观看| 中文字幕中文字幕一区二区|