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

本文介绍了PetShop4.0数据库访问层的设计,包括使用工厂模式、IOC依赖注入以及IDAL接口实现的SQLDAL和OracleDAL。DALFactory通过配置文件动态生成数据库访问实例,易于扩展到其他数据库。业务逻辑层(BLL)封装业务逻辑,调用DAL服务,提供服务给UI层。IOC的接口注入在PetShop中得以应用,但其完整理解仍有待深入。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近在看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对应的实例。然后在该类中的服务中使用,调用数据库访问层服务。提供业务逻辑层的服务。

这样在UI层,就调用该业务逻辑层的服务,完成用户的请求。

Ok,对与数据库访问层的介绍就先到这里吧。但是关于IOC的话题还是需要深入研究一下的。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值