• 推荐
  • 评论
  • 收藏

PetShop4.0 学习总结数据库访问层结构分析

2023-01-27    2587次浏览

最近在看PetShop4.0 ,暂且熟悉了一些数据库层的设计。

看了看,其实也不是很复杂。主要就是使用了一个工厂 ,以及一个IOC以来注入。

我所画的类图如下(不是很标准,自己的UML 水品一般。。。)

其中的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的话题还是需要深入研究一下的。

原文地址:https://www.cnblogs.com/Leo_wl/p/2289446.html