·您现在的位置: 云翼网络 >> 文章中心 >> 网站建设 >> 网站建设开发 >> ASP.NET网站开发 >> EF结合三层:三层中数据层父类和业务层父类的使用

EF结合三层:三层中数据层父类和业务层父类的使用

作者:佚名      ASP.NET网站开发编辑:admin      更新时间:2022-07-23

EF结合三层:三层中数据层父类和业务层父类的使用

今天我们主要讨论下数据层父类和业务层父类的使用。众所周知,数据层无非就是实现增删改查的方法。无论是哪个实体类,无非就是为了实现增删改查方法,所有我们在三层的DAL层封装了一个BaseDAL类,来做增删改查。在BLL层封装了一个BaseBLL类,来做增删改查。如果在Model层,通过ModelFirst的方式,又添加了新的实体(YYY),那么在DAL层只需要定义一个类YYYDAL,并让YYYDAL继承自BaseDAL即可。同理,在BLL层,只需要定义一个类YYYBLL,并让其继承自BaseBLL即可。下面我们来完成这个描述。

由于该文章的侧重点数据层父类和业务层父类的使用。所以我们就不再领着大家搭建三层结构。默认大家已经创建好的程序的架构。

并且在edmx模型中我们已经创建了两个实体。这里我们使用Customer和Ticket(由于最近在讲解影院售票系统)

步骤一:先在DAL层创建一个CustomerDAL类,并且书写对应的增删改查以及分页的方法。

 1 public class CustomerDAL 2     { 3        //数据库上下文对象 4        YYMMVCEntities db = new YYMMVCEntities(); 5        /// <summary> 6        /// 新增操作 7        /// </summary> 8        public int Add(Customer customer) 9        {10            db.Customers.AddObject(customer);11            //保存成功后会将自增的ID设置成customer的主键属性12           return db.SaveChanges();13        }       14        //删除15        public int DeleteBy(int cid)16        {17            Customer customer=new Customer(){CID = cid};18            db.Customers.Attach(customer);19            db.Customers.DeleteObject(customer);20            return db.SaveChanges();21        }22        //根据条件删除23        public int DeleteExPRession(System.Linq.Expressions.Expression<Func<Customer,bool>> deleWhere)24        {25            List<Customer> customers=db.Customers.Where(deleWhere).ToList();26           customers.ForEach(m=>db.Customers.DeleteObject(m));27            return db.SaveChanges();28        }29 30        //修改方法31        public  int Modify(Customer customer)32        {33            db.Attach(customer);34            db.ObjectStateManager.ChangeObjectState(customer, EntityState.Modified);35            return db.SaveChanges();36        }37        //查询38        publicList<Customer> GetListBy(System.Linq.Expressions.Expression<Func<Customer,bool>> seleWhere)39        {40            return db.Customers.Where(seleWhere).ToList();41        }42        //查询和排序43        public List<Customer> GetListBy<Tkey>(System.Linq.Expressions.Expression<Func<Customer,bool>> seleWhere,System.Linq.Expressions.Expression<Func<Customer,Tkey>> orderWhere)44        {45            return db.Customers.Where(seleWhere).OrderBy(orderWhere).ToList();46        }47        //分页查询48        public List<Customer> GetListPaged(int pageIndex,int pageSize,System.Linq.Expressions.Expression<Func<Customer,bool>> orderbyWhere)49        {50            return db.Customers.OrderBy(orderbyWhere).Skip((pageIndex - 1)*pageSize).Take(pageSize).ToList();51        } 52    }

第二步:书写BaseDAL类

然后我们在DAL层新建一个BaseDAL类,类中内容是通过CustomerDAL修改而来的,将出现Customer的地方替换成了T,出现Customers的地方改成了CreateObjectSet<T>()

,修正后的类如下

 1 public class BaseDAL<T> where T:class,new() 2     { 3         //数据库上下文对象 4         YYMMVCEntities db = new YYMMVCEntities(); 5         /// <summary> 6         /// 新增操作 7         /// </summary> 8         public int Add(T customer) 9         {10             //必须限定T的类型,只能为引用类型11             db.CreateObjectSet<T>().AddObject(customer);12             //保存成功后会将自增的ID设置成customer的主键属性13             return db.SaveChanges();14         }15 16         //删除17         public int DeleteBy(T model)18         {19             20             db.CreateObjectSet<T>().Attach(model);21             db.CreateObjectSet<T>().DeleteObject(model);22             return db.SaveChanges();23         }24         //根据条件删除25         public int DeleteExpression(System.Linq.Expressions.Expression<Func<T, bool>> deleWhere)26         {27             List<T> customers = db.CreateObjectSet<T>().Where(deleWhere).ToList();28             customers.ForEach(m => db.CreateObjectSet<T>().DeleteObject(m));29             return db.SaveChanges();30         }31 32         //修改方法33         public int Modify(T customer)34         {35             db.CreateObjectSet<T>().Attach(customer);36             db.ObjectStateManager.ChangeObjectState(customer, EntityState.Modified);37             return db.SaveChanges();38         }39 40         //查询41         public List<T> GetListBy(System.Linq.Expressions.Expression<Func<T, bool>> seleWhere)42         {43             return db.CreateObjectSet<T>().Where(seleWhere).ToList();44         }45         //查询和排序46         public List<T> GetListBy<Tkey>(System.Linq.Expressions.Expression<Func<T, bool>> seleWhere, System.Linq.Expressions.Expression<Func<T, Tkey>> orderWhere)47         {48             return db.CreateObjectSet<T>().Where(seleWhere).OrderBy(orderWhere).ToList();49         }50         //分页查询51         public List<T> GetListPaged(int pageIndex, int pageSize, System.Linq.Expressions.Expression<Func<T, bool>> orderbyWhere)52         {53             return db.CreateObjectSet<T>().OrderBy(orderbyWhere).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();54 55         } 56    }

其中publicclassBaseDAL<T>whereT:class,new()中whereT:class代表T的类型只能是引用类型,new()代表T类型必须有无参的构造。

到这里我们已经完成了对DAL层父类的书写。

第三步:书写BaseBLL类

接下来,我们就可以书写BaseBLL类了,当然,抽取BaseBLL类的方式和抽取BaseDAL的方式基本一致,在这里就直接写出BaseBLL的类的代码结构了。

 1 public class BaseBLL<T> where T:class,new () 2     { 3        protected  BaseDAL<T> dal = new BaseDAL<T>(); 4         /// <summary> 5         /// 新增操作 6         /// </summary> 7         public int Add(T customer) 8         { 9             return dal.Add(customer);10         }11 12         //删除13         public int DeleteBy(T cid)14         {15             return dal.DeleteBy(cid);16         }17         //根据条件删除18         public int DeleteExpression(System.Linq.Expressions.Expression<Func<T, bool>> deleWhere)19         {20             return dal.DeleteExpression(deleWhere);21         }22 23         //修改方法24         public int Modify(T customer)25         {26             return dal.Modify(customer);27         }28 29         //查询30         public List<T> GetListBy(System.Linq.Expressions.Expression<Func<T, bool>> seleWhere)31         {32             return dal.GetListBy(seleWhere);33         }34         //查询和排序35         public List<T> GetListBy<Tkey>(System.Linq.Expressions.Expression<Func<T, bool>> seleWhere, System.Linq.Expressions.Expression<Func<T, Tkey>> orderWhere)36         {37             return dal.GetListBy(seleWhere, orderWhere);38         }39         //分页查询40         public List<T> GetListPaged(int pageIndex, int pageSize, System.Linq.Expressions.Expression<Func<T, bool>> orderbyWhere)41         {42             return dal.GetListPaged(pageIndex, pageSize, orderbyWhere);43 44         } 45     }

其实,我们发现一个问题,就先现在我们的BLL层就是起到了一个数据隔离的作用。

然后我们就可以在对应的DAL层和BLL层创建对应的子类来继承自各自的父类。接下来我们通过ModelFirst的方式修改edmx模型,在其中添加一个新的实体Book,并且建立Customer和Book之间的关系(1对多)。如下图:

步骤四:在DAL层添加BookDAL类,并且继承自BaseDAL;在BLL层添加BookBLL类,并且继承自BaseBLL类。

在页面的Load事件中书写如下代码:

1      if (!IsPostBack)2             {3                 BookBLL bll=new BookBLL();4                 List<Book> list=bll.GetListBy(m => m.ID > 0);5                 Repeater1.DataSource = list;6                 Repeater1.DataBind();7             }

就可以验证子类对象是否成功继承父类的方法。

好的,我们今天的探讨到此结束!