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 }
然后我们在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 }