欧美三区_成人在线免费观看视频_欧美极品少妇xxxxⅹ免费视频_a级毛片免费播放_鲁一鲁中文字幕久久_亚洲一级特黄

PetShop4.0 學習總結(jié)----數(shù)據(jù)庫訪問層結(jié)構(gòu)分析

系統(tǒng) 1746 0

最近在看PetShop4.0 ,暫且熟悉了一些數(shù)據(jù)庫層的設(shè)計。

看了看,其實也不是很復雜。主要就是使用了一個工廠 ,以及一個IOC以來注入。

我所畫的類圖如下(不是很標準,自己的UML 水品一般。。。)

?

?

PetShop4.0 學習總結(jié)----數(shù)據(jù)庫訪問層結(jié)構(gòu)分析

?

其中的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的話題還是需要深入研究一下的。

PetShop4.0 學習總結(jié)----數(shù)據(jù)庫訪問層結(jié)構(gòu)分析


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 奇米影视88888 | 精品久久一区二区 | 欧美日韩精品一区二区三区视频 | 日本高清不卡一区久久精品 | 欧美一区久久 | 91激情网| 久久婷婷av| 日韩精品久久 | 亚洲日本在线天堂无码 | av在线第一页 | 亚洲第一男人天堂 | 女同久久另类99精品国产 | 国产一区二区欧美 | av看片网站 | 精品久久久久久 | 天天碰夜夜 | 国产精品久久人妻无码蜜 | 国产大片线上免费看 | 欧美三级三级三级爽爽爽 | 欧美日韩免费观看视频 | 热久久国产 | 操美女在线 | 国产亚洲美女精品久久久久 | 交免费观看在线 | 一级尻逼视频 | 亚洲一区二区综合 | 欧美激情久久久久久久久 | 欧美性a视频 | 午色影院 | 我不卡在线 | 婷婷色在线观看 | av免费网站在线观看 | 一区二区三区日 | 日韩在线你懂的 | 欧美激情无码成人A片 | 蜜桃久久| 夜色伊人| 国产黄色2 | 久久伦理中文字幕 | 青娱乐精品视频在线观看 | 久久久久国产精品免费免费搜索 |