亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

PetShop4.0 學習總結----數據庫訪問層結構分析

系統 1679 0

最近在看PetShop4.0 ,暫且熟悉了一些數據庫層的設計。

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

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

?

?

PetShop4.0 學習總結----數據庫訪問層結構分析

?

其中的web.config是我自己天上去的,主要就是為了說明一下IOC的問題。

?

其中的Model主要定義了一些實體類。

IDAL提供了數據庫訪問層的抽象,分別有SQLDAL 和OracleDAL去實現。

DALFactory是一個反射工廠,通過讀取配置文件中的配置,判斷使用的那個DAL,然后利用反射生成相應的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中各個實現都做了配置。

如果我們需要更改為 Oracle數據庫的化,只要將相應的DAL服務對象的配置更改即可。因為兩個DAL層都是實現了IDAL的。如果我們在后期要進行Access的擴充,也只要實現該IDAL,編譯生成dll,然后更改配置文件即可。

這個依賴注入體現的一個設計思想也就是 高層組件應該依賴于抽像,而不是某一個具體的功能。

這里的高層組件在PetShop里邊就是只業務邏輯層(BLL)。

依賴注入 不僅可以注入實例對象,也可以注入方法,屬性等等。(想想,其實也就是利用反射能夠動態生成的服務)

依賴注入有三種類型,第一種是 基于接口的。第二種是 基于屬性setter的,第三種是基于構造函數的。PetShop這里就是基于接口的一個實現。至于后兩者,我暫時還不太清楚。

再說BLL,BLL里邊封裝的是業務邏輯。其實也就是利用DALFactory生成 實現了IDAL 的DAL實例,然后通過其調用數據庫訪問層的服務,整合,將服務概念抽象為服務層的概念。操作的實體來自與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(); 調用DALFactory的服務,生成IDAL對應的實例。然后 在該類中的服務中使用,調用數據庫訪問層服務。提供業務邏輯層的服務。

UpdateProduct是我自己加的方法。

這樣在UI層,就調用該業務邏輯層的服務,完成用戶的請求。

Ok,對與數據庫訪問層的介紹就先到這里吧。但是關于IOC的話題還是需要深入研究一下的。

PetShop4.0 學習總結----數據庫訪問層結構分析


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

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

【本文對您有幫助就好】

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

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 精品国产一区二区三区2021 | 99精品久久久久中文字幕 | 精品久久中文字幕有码 | 国产成人亚洲综合 | 九九视频在线观看视频23 | 99视频精品全部国产盗摄视频 | 亚洲欧美日产综合一区二区三区 | 色婷婷色综合缴情在线 | 一类毛片| 国内久久精品 | 狠狠婷婷| 日韩a无吗一区二区三区 | 久久精品一区二区免费看 | 久久久久久尹人网香蕉 | 中日韩欧美中文字幕毛片 | 色综合天天综合网国产成人网 | 91成人免费福利网站在线 | 久久精品国产波多野结衣 | 亚洲免费久久 | 亚洲欧美色综合大色 | 波多野结衣中文字幕久久 | 美女视频国产 | 伊人久久99亚洲精品久久频 | 欧美日韩中文字幕在线观看 | 一级片在线免费观看 | 日本一本一区二区 | 99热国产在线| 久久久99精品久久久 | 日本二级毛片免费 | 色爱区综合激情五月综合色 | 综合欧美视频一区二区三区 | 四虎免费永久观看 | 五月天婷婷在线播放 | 久草青青 | 日韩成人午夜 | 国产亚洲精品美女久久久 | 国内精自品线一区91 | 一本一道波多野结衣一区二区 | 欧美69p| 亚洲精品第一国产综合高清 | 日产国产欧美视频一区精品 |