三层架构泛型应用

本文探讨了三层架构(UI、DAL、BLL)的组成,并指出Model在架构中的重要性。通过引入IDAL接口,增强了DAL层的灵活性。然后,文章详细介绍了如何使用泛型优化数据访问层和业务逻辑层,以减少重复代码并提高代码复用性。通过对比未使用泛型和使用泛型的代码实现,展示了泛型如何简化接口和类的设计,如在UserBLL中实现或直接使用BaseBLL泛型类。

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

1.三层架构一般包含:UI层、DAL层、BLL层,其中每层由Model实体类来传递,所以Model也算是三层架构之一了,例外为了数据库的迁移或者更OO点,DAL层就衍生出了IDAL接口。Model就是简单的对应数据库里面的类,DAL层就是主要操作数据库的方法了,BLL这个就看业务了。而DAL层大部分的方法都是差不多,无非就是几个Insert,Update,Delete,Select

2.泛型:泛型一个类型的模板,只要你定义了一个泛型类,就相当于定义了N个类,每个类的类型不一样而已

不使用泛型的设计:

 

  1. public interface IUserDAL    //数据访问层接口
  2.      {  
  3.         int Insert(User model);  
  4.         int Update(User model);  
  5.         int Delete(int id);  
  6.         User GetModel(int id);  
  7.         DataTable GetList();  
  8.     } 

 

  1. public class UserBLL    //业务逻辑层
  2.      {  
  3.          private IUserDAL dal = new UserDAL();  
  4.    
  5.          public int Insert(User model)  
  6.          {  
  7.              return dal.Insert(model);  
  8.          }  
  9.    
  10.          public int Update(User model)  
  11.          {  
  12.              return dal.Update(model);  
  13.          }  
  14.          public int Delete(int id)  
  15.          {  
  16.              return dal.Delete(id);  
  17.          }  
  18.          public T GetModel(int id)  
  19.          {  
  20.              return dal.GetModel(id);  
  21.          }  
  22.          public DataTable GetList()  
  23.          {  
  24.              return dal.GetList();  
  25.          }  
  26.      } 

使用泛型改写上面的设计:

//数据访问层泛型改写:定义一个数据访问层接口,可以用于不同对象去继承,比如:User,Order,Product...

  1. public interface IDAL<T> where T : class  
  2.     {  
  3.         int Insert(T model);  
  4.         int Update(T model);  
  5.         int Delete(int id);  
  6.         T GetModel(int id);  
  7.         IList<T> GetList();  
  8.    } 
  1. public class UserDAL : IDAL<User> 
  2.     {  
  3.         #region IDAL<User> 成员  
  4.         public int Insert(User model)  
  5.         {  
  6.             //coding  
  7.         }  
  8.         public int Update(User model)  
  9.         {  
  10.             //coding  
  11.         }  
  12.         public int Delete(object id)  
  13.         {  
  14.             //coding  
  15.         }  
  16.         public User GetModel(object id)  
  17.         {  
  18.             //coding  
  19.         }  
  20.         public IList<User> GetList()  
  21.         {  
  22.             //coding  
  23.         }  
  24.         #endregion  
  25.     } 

//业务逻辑层泛型改写:

  1. public class BaseBLL<T, D> 
  2.         where T : class  
  3.         where D : IDAL<T>,new ()  
  4.     {  
  5.         private D dal = new D();  
  6.         public virtual int Insert(T model)  
  7.         {  
  8.             return dal.Insert(model);  
  9.         }  
  10.         public virtual int Update(T model)  
  11.         {  
  12.             return dal.Update(model);  
  13.         }  
  14.         public virtual int Delete(object id)  
  15.         {  
  16.             return dal.Delete(id);  
  17.         }  
  18.         public virtual T GetModel(object id)  
  19.         {  
  20.             return dal.GetModel(id);  
  21.         }  
  22.         public virtual IList<T> GetList()  
  23.         {  
  24.             return dal.GetList(model);  
  25.         }  
  26.     } 
  1. public class UserBLL : BaseBLL<User, UserDAL> 
  2.     {  
  3.          
  4.     } 

 

如果UserBLL没有任何业务的话,那就不要继承了,在UI直接用BaseBLL这个泛型类就可以,调用也很简单

BaseBLL<User> dal=new BaseBLL<User>()

 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值