最近在看PetShop4.0 ,暫且熟悉了一些數(shù)據(jù)庫層的設(shè)計(jì)。
看了看,其實(shí)也不是很復(fù)雜。主要就是使用了一個(gè)工廠 ,以及一個(gè)IOC以來注入。
我所畫的類圖如下(不是很標(biāo)準(zhǔn),自己的UML 水品一般。。。)
?
?
?
其中的web.config是我自己天上去的,主要就是為了說明一下IOC的問題。
?
其中的Model主要定義了一些實(shí)體類。
IDAL提供了數(shù)據(jù)庫訪問層的抽象,分別有SQLDAL 和OracleDAL去實(shí)現(xiàn)。
DALFactory是一個(gè)反射工廠,通過讀取配置文件中的配置,判斷使用的那個(gè)DAL,然后利用反射生成相應(yīng)的IDAL實(shí)例。
DALFactory代碼如下
public
sealed
class
DataAccess {
//
Look up the DAL implementation we should be using
private
static
readonly
string
path = ConfigurationManager.AppSettings[
"
WebDAL
"
];
private
static
readonly
string
orderPath = ConfigurationManager.AppSettings[
"
OrdersDAL
"
];
private
DataAccess() { }
public
static
PetShop.IDAL.ICategory CreateCategory() {
string
className = path +
"
.Category
"
;
return
(PetShop.IDAL.ICategory)Assembly.Load(path).CreateInstance(className);
}
public
static
PetShop.IDAL.IInventory CreateInventory() {
string
className = path +
"
.Inventory
"
;
return
(PetShop.IDAL.IInventory)Assembly.Load(path).CreateInstance(className);
}
public
static
PetShop.IDAL.IItem CreateItem() {
string
className = path +
"
.Item
"
;
return
(PetShop.IDAL.IItem)Assembly.Load(path).CreateInstance(className);
}
public
static
PetShop.IDAL.IOrder CreateOrder() {
string
className = orderPath +
"
.Order
"
;
return
(PetShop.IDAL.IOrder)Assembly.Load(orderPath).CreateInstance(className);
}
public
static
PetShop.IDAL.IProduct CreateProduct() {
string
className = path +
"
.Product
"
;
return
(PetShop.IDAL.IProduct)Assembly.Load(path).CreateInstance(className);
}
}
?
只不過這里將DAL中各個(gè)實(shí)現(xiàn)都做了配置。
如果我們需要更改為 Oracle數(shù)據(jù)庫的化,只要將相應(yīng)的DAL服務(wù)對(duì)象的配置更改即可。因?yàn)閮蓚€(gè)DAL層都是實(shí)現(xiàn)了IDAL的。如果我們?cè)诤笃谝M(jìn)行Access的擴(kuò)充,也只要實(shí)現(xiàn)該IDAL,編譯生成dll,然后更改配置文件即可。
這個(gè)依賴注入體現(xiàn)的一個(gè)設(shè)計(jì)思想也就是 高層組件應(yīng)該依賴于抽像,而不是某一個(gè)具體的功能。
這里的高層組件在PetShop里邊就是只業(yè)務(wù)邏輯層(BLL)。
依賴注入 不僅可以注入實(shí)例對(duì)象,也可以注入方法,屬性等等。(想想,其實(shí)也就是利用反射能夠動(dòng)態(tài)生成的服務(wù))
依賴注入有三種類型,第一種是 基于接口的。第二種是 基于屬性setter的,第三種是基于構(gòu)造函數(shù)的。PetShop這里就是基于接口的一個(gè)實(shí)現(xiàn)。至于后兩者,我暫時(shí)還不太清楚。
再說BLL,BLL里邊封裝的是業(yè)務(wù)邏輯。其實(shí)也就是利用DALFactory生成 實(shí)現(xiàn)了IDAL 的DAL實(shí)例,然后通過其調(diào)用數(shù)據(jù)庫訪問層的服務(wù),整合,將服務(wù)概念抽象為服務(wù)層的概念。操作的實(shí)體來自與Model。
典型的BLL代碼如下
public
class
Product {
//
Get an instance of the Product DAL using the DALFactory
//
Making this static will cache the DAL instance after the initial load
private
static
readonly
IProduct dal = PetShop.DALFactory.DataAccess.CreateProduct();
///
<summary>
///
A method to retrieve products by category name
///
</summary>
///
<param name="category">
The category name to search by
</param>
///
<returns>
A Generic List of ProductInfo
</returns>
public
IList<ProductInfo> GetProductsByCategory(
string
category) {
//
Return new if the string is empty
if
(
string
.IsNullOrEmpty(category))
return
new
List<ProductInfo>();
//
Run a search against the data store
return
dal.GetProductsByCategory(category);
}
///
<summary>
///
A method to search products by keywords
///
</summary>
///
<param name="text">
A list keywords delimited by a space
</param>
///
<returns>
An interface to an arraylist of the search results
</returns>
public
IList<ProductInfo> GetProductsBySearch(
string
text) {
//
Return new if the string is empty
if
(
string
.IsNullOrEmpty(text.Trim()))
return
new
List<ProductInfo>();
//
Split the input text into individual words
string
[] keywords = text.Split();
//
Run a search against the data store
return
dal.GetProductsBySearch(keywords);
}
///
<summary>
///
Query for a product
///
</summary>
///
<param name="productId">
Product Id
</param>
///
<returns>
ProductInfo object for requested product
</returns>
public
ProductInfo GetProduct(
string
productId) {
//
Return empty product if the string is empty
if
(
string
.IsNullOrEmpty(productId))
return
new
ProductInfo();
//
Get the product from the data store
return
dal.GetProduct(productId);
}
public
bool
UpdateProduct(ProductInfo productInfo)
{
if
(productInfo ==
null
)
return
false
;
return
dal.UpdateProduct(productInfo);
}
}
?
這是BLL中Product類,首先使用DataAccess.CreateProduct(); 調(diào)用DALFactory的服務(wù),生成IDAL對(duì)應(yīng)的實(shí)例。然后 在該類中的服務(wù)中使用,調(diào)用數(shù)據(jù)庫訪問層服務(wù)。提供業(yè)務(wù)邏輯層的服務(wù)。
UpdateProduct是我自己加的方法。
這樣在UI層,就調(diào)用該業(yè)務(wù)邏輯層的服務(wù),完成用戶的請(qǐng)求。
Ok,對(duì)與數(shù)據(jù)庫訪問層的介紹就先到這里吧。但是關(guān)于IOC的話題還是需要深入研究一下的。
PetShop4.0 學(xué)習(xí)總結(jié)----數(shù)據(jù)庫訪問層結(jié)構(gòu)分析
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元

