自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

如何打造自己的數(shù)據(jù)訪問層三

數(shù)據(jù)庫
上一篇如何打造自己的數(shù)據(jù)訪問層二中,我們已具體實現(xiàn)了數(shù)據(jù)訪問層對應(yīng)的功能,該進(jìn)行收尾工作了。

上一篇如何打造自己的數(shù)據(jù)訪問層二中,我們已具體實現(xiàn)了數(shù)據(jù)訪問層對應(yīng)的功能,該進(jìn)行收尾工作了,先來看段代碼,試試上一篇實現(xiàn)的功能:

  1. string sqlText = "SELECT ID, NAME, VAL FROM TEST";  
  2. string columns = "ID, NAME, VAL";  
  3. DataSet ds = new DataSet();  
  4. DataExecutor execObj = new MSSqlExecutor();  
  5. DataMapping map = new DataMapping();  
  6. map.ExecuteObject = execObj;  
  7. map.TableName = "TEST";  
  8. map.KeyColumns = "ID";  
  9. map.Columns = "ID, NAME, VAL";  
  10. DataMapping map = new DataMapping(execObj.GetInstant(), "TEST""ID", columns);  
  11. map.Fill(sqlText, "TEST");  
  12. map.SetCommands(DataCommandType.Insert | DataCommandType.Update | DataCommandType.Delete, ds);  
  13. //DataTable方式進(jìn)行增、刪、改  
  14. bool isSuccess = execObj.Update(); 

果然已經(jīng)完成了對數(shù)據(jù)庫的讀寫操作了,至少不用再寫大段的參數(shù)傳遞代碼,功能都已經(jīng)實現(xiàn)了,是不是就完成了?

仔細(xì)看看上面的代碼,實際上還有問題尚未解決,看看這句:

  1. DataExecutor execObj = new MSSqlExecutor(); 

竟然在代碼里直接實例化一個MSSql的執(zhí)行對象,這樣一開始提出的數(shù)據(jù)庫之間的切換問題根本就沒有從本質(zhì)上解決。

再回過頭來看上一篇,有一個方法public IDbConnection GetConn(),用來獲取數(shù)據(jù)連接對像,之前并沒有說明其如何實現(xiàn)。

我們知道DBConnection有兩個關(guān)鍵信息:

1、與哪種類型的數(shù)據(jù)庫產(chǎn)生連接,這個前面已經(jīng)解決了。

2、傳遞與數(shù)據(jù)庫連接的帳號信息、訪問庫信息的ConnectionString,這個并沒有提及。

看看第二點以前是怎么做的:

  1. public IDbConnection GetConn()  
  2. {  
  3.     if (conn != null)  
  4.     {  
  5.         return conn;  
  6.     }  
  7.     conn = new SqlConnection();  
  8.     conn.ConnectionString = 連接字串;  
  9.     return conn;  

上面出現(xiàn)了連接字串,這個字串從哪來?

總結(jié)下,要完成最終的數(shù)據(jù)訪問輸出,還需要解決兩個問題:

1、動態(tài)進(jìn)行不同數(shù)據(jù)庫之間的切換。

2、解決數(shù)據(jù)連接字串的來源問題。

接著就來解決這兩問題,先解決第二個問題,有個比較簡單的方法,將連接字串寫入配置文件中去,數(shù)據(jù)訪問層只需知道它傳遞過來KEY值:

  1. <appSettings>  
  2.     <add key="ConnStr" value="server=.;uid=sa;password=123456;database=DATA_BASE;max pool size=300"/>  
  3. </appSettings> 

***個問題解決了,只剩下***一個問題了,如何動態(tài)切換不同的數(shù)據(jù)庫,也就是說,在使用的時候不需要自己NEW一個對象,而是通過第三方來創(chuàng)建一個對象,實際上,設(shè)計模式里已提出了方案,創(chuàng)建型模式,有興趣的朋友可以自行研究,我們這里只需要用到簡單工廠模式:

  1. public sealed class ExecutorFactory  
  2. {  
  3.     public static DataExecutor Create()  
  4.     {  
  5.         return Create(DatabaseType.MSSql);  
  6.     }  
  7.     public static DataExecutor Create(DatabaseType dbType)  
  8.     {  
  9.         AbstractDataBase dataBase = null;  
  10.         Switch(dbType)  
  11.         {  
  12.             case DatabaseType.MSSql:  
  13.                 dataBase = new MSSqlDataBase();  
  14.                 break;  
  15.             case DatabaseType.Oracle:  
  16.                 dataBase = new OracleDataBase();  
  17.                 break;  
  18.         }  
  19.         return dataBase.Create();  
  20.     }  

現(xiàn)在可對這句代碼進(jìn)行替換了:DataExecutor execObj = new MSSqlExecutor();

替換為:DataExecutor execObj = ExecutorFactory.Create();

至此,問題都解決了,切換數(shù)據(jù)庫是只需更改DatabaseType為相應(yīng)的數(shù)庫類型。

接下來再考慮下,如果改變數(shù)據(jù)庫類型也不需要變動程序,能不能實現(xiàn)?

還是利用配置文件,只是此時提供的不是類型字串,而是實際的數(shù)據(jù)執(zhí)行者程序集信息,再利用.NET的天然優(yōu)勢反射可以實現(xiàn)了。
最終配置文件為:

  1. <appSettings>  
  2.     <add key="ConnStr" value="server=.;uid=sa;password=123456;database=DATA_BASE;max pool size=300"/>  
  3.     <add key="DBExecutor" value="FM.DataAccess, FM.DataAccess.MappingExcuter.MSSqlExecutor"></add>  
  4.   </appSettings> 

改造后的工廠:

  1. public sealed class ExecutorFactory  
  2.     {  
  3.         public static DataExecutor Create()  
  4.         {  
  5.             return Create(null);  
  6.         }  
  7.         public static DataExecutor Create(string dataBaseTypeKey)  
  8.         {  
  9.             return Create(dataBaseTypeKey, null);  
  10.         }  
  11.         public static DataExecutor Create(string dataBaseTypeKey, string connStrKey)  
  12.         {  
  13.             if (string.IsNullOrEmpty(dataBaseTypeKey))  
  14.             {  
  15.                 dataBaseTypeKey = "DBExecutor";  
  16.             }  
  17.             string[] sltDataBaseType = ConfigReader.Read(dataBaseTypeKey).Split(',');  
  18.             string asselblyName = sltDataBaseType[0];  
  19.             string nameSpace = sltDataBaseType[1].Trim();  
  20.             Assembly assembly = Assembly.Load(asselblyName);  
  21.             DataExecutor execObj = assembly.CreateInstance(nameSpace) as DataExecutor;  
  22.             execObj.SetConnectionString(connStrKey);  
  23.             return execObj;  
  24.         }  
  25.     } 

到此為止,數(shù)據(jù)訪問層最終完成,當(dāng)然這里還有很多問題有待解決,但其基本框架已形成了,以此為依據(jù),根據(jù)業(yè)務(wù)變化,實現(xiàn)自己的擴(kuò)展,不斷更新,最終才能真正形成完善的數(shù)據(jù)訪問層。

原文鏈接:http://www.cnblogs.com/FlySoul/archive/2011/05/08/2040292.html

【編輯推薦】

  1. 曬曬我的通用數(shù)據(jù)訪問層
  2. 幾步走,教你創(chuàng)建簡單訪問數(shù)據(jù)庫方法
  3. 一句代碼實現(xiàn)批量數(shù)據(jù)綁定 下
  4. 一步一步設(shè)計你的數(shù)據(jù)庫1
  5. 不重復(fù)隨機(jī)數(shù)列生成算法
責(zé)任編輯:艾婧 來源: 博客園
相關(guān)推薦

2011-05-05 14:33:34

數(shù)據(jù)訪問層

2011-05-07 12:56:39

數(shù)據(jù)訪問

2012-08-15 11:03:18

框架項目

2011-03-29 09:15:28

通用數(shù)據(jù)訪問層

2009-01-08 09:52:26

2025-01-26 17:00:46

2012-01-11 09:46:31

DAL

2009-08-13 14:59:00

C#數(shù)據(jù)訪問層

2023-07-27 08:16:51

數(shù)據(jù)訪問層項目

2013-11-26 09:47:47

ORM

2009-08-04 10:17:55

ASP.NET SqlASP.NET數(shù)據(jù)訪問

2009-08-19 10:54:42

ASP.NET數(shù)據(jù)訪問

2009-09-04 18:00:54

C#數(shù)據(jù)訪問層

2012-06-07 10:53:08

架構(gòu)設(shè)計數(shù)據(jù)訪問層設(shè)計原則

2016-02-15 14:13:39

Python編碼環(huán)境

2011-08-31 13:45:38

Demon CamipadiPhone

2020-12-14 08:09:03

弱口令工具掃描

2015-10-15 09:37:50

桌面環(huán)境發(fā)行版Linux

2009-07-24 13:25:43

創(chuàng)建數(shù)據(jù)訪問層

2011-04-27 11:12:07

點贊
收藏

51CTO技術(shù)棧公眾號