使用設(shè)計模式構(gòu)建通用數(shù)據(jù)庫訪問類
2024-07-21 02:23:41
供稿:網(wǎng)友
使用設(shè)計模式構(gòu)建通用數(shù)據(jù)庫訪問類
在應(yīng)用程序的設(shè)計中,數(shù)據(jù)庫的訪問是非常重要的,我們通常需要將對數(shù)據(jù)庫的訪問集中起來,以保證良好的封裝性和可維護(hù)性。在.net中,數(shù)據(jù)庫的訪問,對于微軟自家的sqlserver和其他數(shù)據(jù)庫(支持oledb),采用不同的訪問方法,這些類分別分布于system.data.sqlclient和system.data.oledb名稱空間中。微軟后來又推出了專門用于訪問oracle數(shù)據(jù)庫的類庫。我們希望在編寫應(yīng)用系統(tǒng)的時候,不因這么多類的不同而受到影響,能夠盡量做到數(shù)據(jù)庫無關(guān),當(dāng)后臺數(shù)據(jù)庫發(fā)生變更的時候,不需要更改客戶端的代碼。
這就需要我們在實際開發(fā)過程中將這些數(shù)據(jù)庫訪問類再作一次封裝。經(jīng)過這樣的封裝,不僅可以達(dá)到上述的目標(biāo),還可以減少操作數(shù)據(jù)庫的步驟,減少代碼編寫量。在這個方面,微軟為我們提供了application block,但是,可惜的是目前只支持sql server。這里,介紹一種在實際應(yīng)用中得到了非常好的效果的實作策略——筆者編寫的websharp框架中的數(shù)據(jù)訪問結(jié)構(gòu)。factory設(shè)計模式是使用的主要方法。
我們先來看看factory的含義:定義一個用于創(chuàng)建對象的接口,讓子類決定實例化哪一個類。factory method使一個類的實例化延遲到其子類。我們這里可能會處理對多種數(shù)據(jù)庫的操作,因此,需要首先定義一個操縱數(shù)據(jù)庫的接口,然后,根據(jù)數(shù)據(jù)庫的不同,由類工廠決定實例化哪個類。
下面,我們首先來定義這個訪問接口。為了方便說明問題,我們在這里只列出了比較少的方法,其他的方法是很容易參照添加的。
public interface dataaccess
{
databasetype databasetype{get;} //數(shù)據(jù)庫類型
idbconnection dbconnection{get;} //得到數(shù)據(jù)庫連接
void open(); //打開數(shù)據(jù)庫連接
void close(); //關(guān)閉數(shù)據(jù)庫連接
idbtransaction begintransaction(); //開始一個事務(wù)
int executenonquery(string commandtext); //執(zhí)行sql語句
dataset executedataset(string commandtext);//執(zhí)行sql,返回dataset
}
因為,dataaccess的具體實現(xiàn)類有一些共同的方法,所以,先從dataaccess實現(xiàn)一個抽象的abstractdataaccess類,包含一些公用方法。然后,我們分別為sql server、oracle和oledb數(shù)據(jù)庫編寫三個數(shù)據(jù)訪問的具體實現(xiàn)類:
public sealed class mssqldataaccess : abstractdataaccess
{
……//具體實現(xiàn)代碼。
}
public class oledbdataaccess : abstractdataaccess
{
……//具體實現(xiàn)代碼。
}
public class oracledataaccess : abstractdataaccess
{
……//具體實現(xiàn)代碼。
}
現(xiàn)在我們已經(jīng)完成了所要的功能,下面,我們需要創(chuàng)建一個factory類,來實現(xiàn)自動數(shù)據(jù)庫切換的管理。這個類很簡單,主要的功能就是根據(jù)數(shù)據(jù)庫類型,返回適當(dāng)?shù)臄?shù)據(jù)庫操縱類。
public sealed class dataaccessfactory
{
private dataaccessfactory(){}
private static persistenceproperty defaultpersistenceproperty;
public static persistenceproperty defaultpersistenceproperty
{
get{return defaultpersistenceproperty;}
set{defaultpersistenceproperty=value;}
}
public static dataaccess createdataaccess(persistenceproperty pp)
{
dataaccess dataaccess;
switch(pp.databasetype)
{
case(databasetype.mssqlserver):
dataaccess = new mssqldataaccess(pp.connectionstring);
break;
case(databasetype.oracle):
dataaccess = new oracledataaccess(pp.connectionstring);
break;
case(databasetype.oledbsupported):
dataaccess = new oledbdataaccess(pp.connectionstring);
break;
default:
dataaccess=new mssqldataaccess(pp.connectionstring);
break;
}
return dataaccess;
}
public static dataaccess createdataaccess()
{
return createdataaccess(defaultpersistenceproperty);
}
}
好了,現(xiàn)在,一切都完成了,客戶端在代碼調(diào)用的時候,可能就是采用如下形式:
persistenceproperty pp = new persistenceproperty();
pp.connectionstring = "server=127.0.0.1;uid=sa;pwd=;database=northwind;";
pp.databasetype = databasetype. mssqlserver;
pp.userid = “sa”;
pp.password = “”;
dataaccess db= dataaccessfactory.createdataaccess(pp)
db.open();
……//db.需要的操作
db.close();
或者,如果事先設(shè)定了dataaccessfactory的defaultpersistenceproperty屬性,可以直接使用
dataaccess db= dataaccessfactory.createdataaccess()
方法創(chuàng)建dataaccess實例。
當(dāng)數(shù)據(jù)庫發(fā)生變化的時候,只需要修改persistenceproperty的值,客戶端不會感覺到變化,也不用去關(guān)心。這樣,實現(xiàn)了良好的封裝性。當(dāng)然,前提是,你在編寫程序的時候,沒有用到特定數(shù)據(jù)庫的特性,例如,sql server的專用函數(shù)。
以上,介紹了一種通用數(shù)據(jù)庫操作類的實現(xiàn)設(shè)計方法,希望能夠?qū)Υ蠹矣兴鶈l(fā)。全部的源代碼,可以從 www.websharp.org下載,或者到 http://www.uml.org.cn/dvbbs6.0.0/index.asp 進(jìn)行討論。除了數(shù)據(jù)訪問的源代碼,你還可以下載到全部websharp源代碼。