标签:des style blog http color os 数据 for
1,AdoNetDal和EfDal都实现IDal这个接口,公共的方法写到接口中,接口中的代码!
1 namespace PCITC.OA.IDal 2 { 3 public interface IUserInforDal 4 { 5 UserInfor Add(UserInfor userInfor); 6 7 bool Update(UserInfor userInfor); 8 9 bool Delete(UserInfor userInfor); 10 11 int Delete(params int[] ids); 12 13 /// <summary> 14 /// 基本过滤 15 /// </summary> 16 /// <param name="whereLambda">过滤的Lambda表达式</param> 17 /// <returns></returns> 18 IQueryable<UserInfor> LoadDatas(Func<UserInfor, bool> whereLambda); 19 20 /// <summary> 21 /// 分页 22 /// </summary> 23 /// <typeparam name="S">排序字段的类型</typeparam> 24 /// <param name="pagesize">每页多少条</param> 25 /// <param name="pageindex">当前第几页</param> 26 /// <param name="total">总条数</param> 27 /// <param name="whereLambda">过滤的Lambda表达式</param> 28 /// <param name="orderByLambda">排序的Lambda表达式</param> 29 /// <returns></returns> 30 IQueryable<UserInfor> LoadDatas<S>(int pagesize,int pageindex,out int total,Func<UserInfor,bool> whereLambda,Func<UserInfor,S> orderByLambda,bool isAsc); 31 } 32 }
2,EfDal层的代码
1 namespace PCITC.OA.Dal 2 { 3 public class UserInforDal:IUserInforDal 4 { 5 DataModelContainer dbc = new DataModelContainer(); 6 7 public UserInfor Add(UserInfor userInfor) 8 { 9 dbc.UserInfor.Add(userInfor); 10 dbc.SaveChanges(); 11 return userInfor; 12 } 13 14 public bool Update(UserInfor userInfor) 15 { 16 dbc.UserInfor.Attach(userInfor); 17 dbc.Entry(userInfor).State = EntityState.Modified; 18 return dbc.SaveChanges() > 0; 19 } 20 21 public bool Delete(UserInfor userInfor) 22 { 23 dbc.UserInfor.Attach(userInfor); 24 dbc.Entry(userInfor).State = EntityState.Deleted; 25 return dbc.SaveChanges() > 0; 26 } 27 28 public int Delete(params int[] ids) 29 { 30 foreach (int id in ids) 31 { 32 UserInfor user = new UserInfor(); 33 user.Id = id; 34 dbc.UserInfor.Attach(user); 35 dbc.Entry(user).State = EntityState.Deleted; 36 } 37 return dbc.SaveChanges(); 38 } 39 40 public IQueryable<UserInfor> LoadDatas(Func<UserInfor, bool> whereLambda) 41 { 42 return dbc.UserInfor.Where(whereLambda).AsQueryable(); 43 } 44 45 public IQueryable<UserInfor> LoadDatas<S>(int pagesize, int pageindex, out int total, Func<UserInfor, bool> whereLambda,Func<UserInfor,S> orderByLambda,bool isAsc) 46 { 47 total = dbc.UserInfor.Where(whereLambda).Count(); 48 if (isAsc) 49 { 50 return dbc.UserInfor.Where(whereLambda).OrderBy(orderByLambda).Skip(pagesize * (pageindex - 1)).Take(pagesize).AsQueryable(); 51 } 52 else 53 { 54 return dbc.UserInfor.Where(whereLambda).OrderByDescending(orderByLambda).Skip(pagesize * (pageindex - 1)).Take(pagesize).AsQueryable(); 55 } 56 57 } 58 59 60 61 } 62 }
3,这个时候当数据库中有第二张表Role表的时候,就需要同样有IRoleDal,然后在这个接口中跟IUserInfor一样写增删改查的方法。这个时候我们利用基接口和泛型来将接口中重复的代码提取到基接口中。
1 namespace PCITC.OA.IDal 2 { 3 public interface IBaseDal<T> where T:class,new() 4 { 5 T Add(T entity); 6 7 bool Update(T entity); 8 9 bool Delete(T entity); 10 11 int Delete(params int[] ids); 12 13 /// <summary> 14 /// 基本过滤 15 /// </summary> 16 /// <param name="whereLambda">过滤的Lambda表达式</param> 17 /// <returns></returns> 18 IQueryable<T> LoadDatas(Func<T, bool> whereLambda); 19 20 /// <summary> 21 /// 分页 22 /// </summary> 23 /// <typeparam name="S">排序字段的类型</typeparam> 24 /// <param name="pagesize">每页多少条</param> 25 /// <param name="pageindex">当前第几页</param> 26 /// <param name="total">总条数</param> 27 /// <param name="whereLambda">过滤的Lambda表达式</param> 28 /// <param name="orderByLambda">排序的Lambda表达式</param> 29 /// <returns></returns> 30 IQueryable<T> LoadDatas<S>(int pagesize, int pageindex, out int total, Func<T, bool> whereLambda, Func<T, S> orderByLambda, bool isAsc); 31 } 32 }
4,同样的RoleDal中具体的实现方法和UserInfor中具体的实现方法也是重复的,这个时候将重复的实现代码放到基类中。
1 namespace PCITC.OA.Dal 2 { 3 public class BaseDal<T> where T:class,new () 4 { 5 DataModelContainer dbc = new DataModelContainer(); 6 public T Add(T entity) 7 { 8 dbc.Set<T>().Add(entity); 9 dbc.SaveChanges(); 10 return entity; 11 } 12 13 public bool Update(T entity) 14 { 15 dbc.Set<T>().Attach(entity); 16 dbc.Entry(entity).State = EntityState.Modified; 17 return dbc.SaveChanges() > 0; 18 } 19 20 public bool Delete(T entity) 21 { 22 dbc.Set<T>().Attach(entity); 23 dbc.Entry(entity).State = EntityState.Deleted; 24 return dbc.SaveChanges() > 0; 25 } 26 27 public int Delete(params int[] ids) 28 { 29 foreach (int id in ids) 30 { 31 //首先可以泛型的基类约束给Id字段赋值,也可以通过反射! 32 T entity = dbc.Set<T>().Find(id);//Find方法会首先从内存中查找,内存中没有,采取查寻 33 dbc.Set<T>().Remove(entity); 34 } 35 return dbc.SaveChanges(); 36 } 37 38 public IQueryable<T> LoadDatas(Func<T, bool> whereLambda) 39 { 40 return dbc.Set<T>().Where(whereLambda).AsQueryable(); 41 } 42 43 public IQueryable<T> LoadDatas<S>(int pagesize, int pageindex, out int total, Func<T, bool> whereLambda, Func<T, S> orderByLambda, bool isAsc) 44 { 45 total = dbc.Set<T>().Where(whereLambda).Count(); 46 if (isAsc) 47 { 48 return dbc.Set<T>().Where(whereLambda).OrderBy(orderByLambda).Skip(pagesize * (pageindex - 1)).Take(pagesize).AsQueryable(); 49 } 50 else 51 { 52 return dbc.Set<T>().Where(whereLambda).OrderByDescending(orderByLambda).Skip(pagesize * (pageindex - 1)).Take(pagesize).AsQueryable(); 53 } 54 55 } 56 } 57 }
5,架构图
一步一步搭建开发框架(三)基类基接口,布布扣,bubuko.com
标签:des style blog http color os 数据 for
原文地址:http://www.cnblogs.com/wanren/p/3895645.html