最近在看PetShop4.0 ,暫且熟悉了一些數(shù)據(jù)庫層的設(shè)計。
看了看,其實也不是很復雜。主要就是使用了一個工廠 ,以及一個IOC以來注入。
我所畫的類圖如下(不是很標準,自己的UML 水品一般。。。)
?
?
?
其中的web.config是我自己天上去的,主要就是為了說明一下IOC的問題。
?
其中的Model主要定義了一些實體類。
IDAL提供了數(shù)據(jù)庫訪問層的抽象,分別有SQLDAL 和OracleDAL去實現(xiàn)。
DALFactory是一個反射工廠,通過讀取配置文件中的配置,判斷使用的那個DAL,然后利用反射生成相應(yīng)的IDAL實例。
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中各個實現(xiàn)都做了配置。
如果我們需要更改為 Oracle數(shù)據(jù)庫的化,只要將相應(yīng)的DAL服務(wù)對象的配置更改即可。因為兩個DAL層都是實現(xiàn)了IDAL的。如果我們在后期要進行Access的擴充,也只要實現(xiàn)該IDAL,編譯生成dll,然后更改配置文件即可。
這個依賴注入體現(xiàn)的一個設(shè)計思想也就是 高層組件應(yīng)該依賴于抽像,而不是某一個具體的功能。
這里的高層組件在PetShop里邊就是只業(yè)務(wù)邏輯層(BLL)。
依賴注入 不僅可以注入實例對象,也可以注入方法,屬性等等。(想想,其實也就是利用反射能夠動態(tài)生成的服務(wù))
依賴注入有三種類型,第一種是 基于接口的。第二種是 基于屬性setter的,第三種是基于構(gòu)造函數(shù)的。PetShop這里就是基于接口的一個實現(xiàn)。至于后兩者,我暫時還不太清楚。
再說BLL,BLL里邊封裝的是業(yè)務(wù)邏輯。其實也就是利用DALFactory生成 實現(xiàn)了IDAL 的DAL實例,然后通過其調(diào)用數(shù)據(jù)庫訪問層的服務(wù),整合,將服務(wù)概念抽象為服務(wù)層的概念。操作的實體來自與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對應(yīng)的實例。然后 在該類中的服務(wù)中使用,調(diào)用數(shù)據(jù)庫訪問層服務(wù)。提供業(yè)務(wù)邏輯層的服務(wù)。
UpdateProduct是我自己加的方法。
這樣在UI層,就調(diào)用該業(yè)務(wù)邏輯層的服務(wù),完成用戶的請求。
Ok,對與數(shù)據(jù)庫訪問層的介紹就先到這里吧。但是關(guān)于IOC的話題還是需要深入研究一下的。
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號聯(lián)系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元

