1.三层架构一般包含:UI层、DAL层、BLL层,其中每层由Model实体类来传递,所以Model也算是三层架构之一了,例外为了数据库的迁移或者更OO点,DAL层就衍生出了IDAL接口。Model就是简单的对应数据库里面的类,DAL层就是主要操作数据库的方法了,BLL这个就看业务了。而DAL层大部分的方法都是差不多,无非就是几个Insert,Update,Delete,Select
2.泛型:泛型一个类型的模板,只要你定义了一个泛型类,就相当于定义了N个类,每个类的类型不一样而已
不使用泛型的设计:
- public interface IUserDAL //数据访问层接口
- {
- int Insert(User model);
- int Update(User model);
- int Delete(int id);
- User GetModel(int id);
- DataTable GetList();
- }
- public class UserBLL //业务逻辑层
- {
- private IUserDAL dal = new UserDAL();
- public int Insert(User model)
- {
- return dal.Insert(model);
- }
- public int Update(User model)
- {
- return dal.Update(model);
- }
- public int Delete(int id)
- {
- return dal.Delete(id);
- }
- public T GetModel(int id)
- {
- return dal.GetModel(id);
- }
- public DataTable GetList()
- {
- return dal.GetList();
- }
- }
使用泛型改写上面的设计:
//数据访问层泛型改写:定义一个数据访问层接口,可以用于不同对象去继承,比如:User,Order,Product...
- public interface IDAL<T> where T : class
- {
- int Insert(T model);
- int Update(T model);
- int Delete(int id);
- T GetModel(int id);
- IList<T> GetList();
- }
- public class UserDAL : IDAL<User>
- {
- #region IDAL<User> 成员
- public int Insert(User model)
- {
- //coding
- }
- public int Update(User model)
- {
- //coding
- }
- public int Delete(object id)
- {
- //coding
- }
- public User GetModel(object id)
- {
- //coding
- }
- public IList<User> GetList()
- {
- //coding
- }
- #endregion
- }
//业务逻辑层泛型改写:
- public class BaseBLL<T, D>
- where T : class
- where D : IDAL<T>,new ()
- {
- private D dal = new D();
- public virtual int Insert(T model)
- {
- return dal.Insert(model);
- }
- public virtual int Update(T model)
- {
- return dal.Update(model);
- }
- public virtual int Delete(object id)
- {
- return dal.Delete(id);
- }
- public virtual T GetModel(object id)
- {
- return dal.GetModel(id);
- }
- public virtual IList<T> GetList()
- {
- return dal.GetList(model);
- }
- }
- public class UserBLL : BaseBLL<User, UserDAL>
- {
- }
如果UserBLL没有任何业务的话,那就不要继承了,在UI直接用BaseBLL这个泛型类就可以,调用也很简单
BaseBLL<User> dal=new BaseBLL<User>()