zoukankan      html  css  js  c++  java
  • 网上购物系统(Task100)——业务逻辑层BLL(面向接口的编程模式)

    源代码:13033480群共享

    【操作步骤】

    一、新建类库IDAL,设置属性,添加引用→项目→Model

    二、添加类ICategory.csIItem.cs

    1ICategory.cs

    using System;
    using System.Collections.Generic;
    using WestGarden.Model;
    
    namespace WestGarden.IDAL
    {
        public interface ICategory
        {
            IList<CategoryInfo> GetCategories();
            CategoryInfo GetCategory(string categoryId);
        }
    }
    


     

    2IItem.cs

    using System;
    using System.Collections.Generic;
    
    using WestGarden.Model;
    
    namespace WestGarden.IDAL
    {
        public interface IItem
        {
            IList<ItemInfo> GetItemsByCategory(string CategoryId);
            IList<ItemDetails> GetItemDetailsByItemId(int ItemId);
            void UpdateItem(ItemDetails item);
            void InsertItem(ItemDetails item);
            void DeleteItem(ItemDetails item);
        }
    }
    


     

    三、新建类库BLL,设置属性,添加引用→项目→ModelDAL

    四、添加类Category.csItem.cs

    1Category.cs

    using System.Collections.Generic;
    
    using WestGarden.Model;
    using WestGarden.IDAL;
    
    using WestGarden.DAL;
    
    namespace WestGarden.BLL
    {
        public class Category
        {
            private static readonly ICategory dal = new WestGarden.DAL.Category();
     		
            public IList<CategoryInfo> GetCategories()
            {
                return dal.GetCategories();
            }
    
            public CategoryInfo GetCategory(string categoryId)
            {
                if (string.IsNullOrEmpty(categoryId))
                    return null;
    
                return dal.GetCategory(categoryId);
            }
        }
    }
    


     

    2Item.cs

    using System.Collections.Generic;
    
    using WestGarden.Model;
    using WestGarden.IDAL;
    
    using WestGarden.DAL;
    
    namespace WestGarden.BLL
    {
        public class Item
        {
            private static readonly IItem dal = new WestGarden.DAL.Item();
    
            public IList<ItemInfo> GetItemsByCategory(string CategoryId)
            {
                if (string.IsNullOrEmpty(CategoryId))
                    return new List<ItemInfo>();
                return dal.GetItemsByCategory(CategoryId);
            }
            public IList<ItemDetails> GetItemDetailsByItemId(int ItemId)
            {
                return dal.GetItemDetailsByItemId(ItemId);
            }
            public void UpdateItem(ItemDetails item)
            {
                dal.UpdateItem(item);
            }
            public void InsertItem(ItemDetails item)
            {
                dal.InsertItem(item);
            }
            public void DeleteItem(ItemDetails item)
            {
                dal.DeleteItem(item);
            }
        }
    }
    


     

    五、数据访问层DAL中,添加引用→项目→IDAL,修改两个类Category.csItem.cs,改为继承自ICategoryIItem,注意添加using WestGarden.IDAL;

     

    using WestGarden.IDAL;

    public class Category:ICategory

     

    using WestGarden.IDAL;

    public class Item:IItem

     

    六、应用层Web中,添加引用→项目→BLL,把所有原来引用DAL的地方,主要是几个用户控件,包括NavigationControl.ascx.csItemDetailsControl.ascx.csItemsControl.ascx.csItemManageControl.ascx.cs修改为引用BLL,即所using WestGarden.DAL;替换为using WestGarden.BLL;

    设计模式的师祖GoF,有句名言:Program to an interface, not an implementation,意思是说,要对接口编程而不要对实现编程。

            我们前面使用的两层结构,已经能比较好的解决数据库的查询、更新、插入和删除等基本操作,并以优美的界面呈现给客户。

    现在的问题是,如果数据库换成Access,你该怎么办?再换成Oracle、Excel......,头是不是很大??数据访问层肯定要换,用户界面层,也要做不小的改动。

            业务逻辑层的出现,就是为了解决这一问题的。

            三层结构后,应用层就不再直接面向数据访问层,而是面向业务逻辑层了。

            看下面的代码,业务逻辑层好象并没有做什么大的工作,只是把数据访问层中的类实例化一下,调用里面的函数,再转交给应用层,完全是多了一道手续嘛......

          的确,手续是多了一道,由原来的应用层直接找数据访问层变成了,应用层找业务逻辑层,再由业务逻辑层找数据访问层。而问题的关键也就在这里,你仔细一下业务逻辑层里,是怎么实例化数据访问层里的类的:

    private staticreadonlyICategory dal =new WestGarden.DAL.Category();

    private staticreadonlyIItem dal =new WestGarden.DAL.Item();

    注意,这两个实例化语句,实例化后的指针是ICategoryIItem,也就是说,通过业务层,应用层面向的不再是数据访问层了,而只是个接口。数据访问层以后如果有什么变化,就与应用层没有关系了。

            这里有个比方,比方说我们的计算机主机,从外部读取、写入数据,是通过USB接口的,而插在USB接口的是U盘,还是移动硬盘,甚至是MP3,都与计算机主机无关,不会因为外部设备的这些变化而去重新打造我们的主机。

            接口的作用,就在这里,采用这种面向接口的三层结构,如果数据库变成了Access、Excel甚至Oracle,你只需要重新做个数据访问层,继承接口中的类,或者说是实现接口规则,然后修改一下业务逻辑层的这个语句就可以了:

     

    private staticreadonlyICategory dal =new WestGarden.DAL.Category();

    private staticreadonlyIItem dal =new WestGarden.DAL.Item();

    版权所有©2012,西园电脑工作室.欢迎转载,转载请注明出处.更多文章请参阅博客http://blog.csdn.com/yousuosi

  • 相关阅读:
    css3多列
    伪元素
    text文本样式二
    透明登录框
    透明度设置opacity
    超链接
    meta标签
    奇偶选择器
    OC跟Swift混编
    Swift中as as! as?的区别
  • 原文地址:https://www.cnblogs.com/WestGarden/p/3138423.html
Copyright © 2011-2022 走看看