数据层以及数据接口设计如下图(以g_orga组织机构和g_role角色)为例,这几个类可以通过.tt模版生成
设计参考学习http://www.cnblogs.com/hanyinglong/archive/2013/04/08/3008896.html
1.IBaseRepository
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Data.Entity; 6 using System.Linq.Expressions; 7 8 namespace MISMODEL.DAL 9 { 10 public interface IBaseRepository<T> where T : class, new() 11 { 12 void SetLazyLoading(bool flag); 13 14 // 1实现对数据库的添加功能,添加实现EF框架的引用 15 bool AddEntity(T entity); 16 bool AddEntity(T entity,bool isSave); 17 // 2实现对数据库的修改功能 18 bool UpdateEntity(T entity); 19 // 3实现对数据库的删除功能 20 bool DeleteEntity(T entity); 21 bool DeleteEntity(List<T> entity, bool isSave); 22 // 4实现对数据库的查询 --主键查询 23 T FindByID(Expression<Func<T, bool>> whereLambda); 24 // 5实现对数据库的查询 --条件查询 25 IQueryable<T> LoadEntities(Expression<Func<T, bool>> whereLambda); 26 // 6实现对数据库的查询 --无条件查询 27 IQueryable<T> LoadEntities(); 28 // 7查询返回dbset 29 DbSet<T> LoadDbSetEntities(); 30 // 8实现对数据库的查询 --首行查询 31 T FirstOrDefaultEntities(Expression<Func<T, bool>> whereLambda); 32 // 9实现对数据库的查询 --动态条件查询 33 IQueryable<T> SqlQuery(string sqlstring, params object[] paramertes); 34 // 10实现对数据的分页查询 35 IQueryable<T> LoadPageEntities<S>(int pageIndex, int pageSize, out int total, Expression<Func<T, bool>> whereLambda, bool isAsc, Func<T, S> orderByLambda); 36 //10事物提交 37 bool Commit(); 38 } 39 }
2. BaseRepository<T>
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Data.Entity; 6 using System.Data.Entity.Infrastructure; 7 using System.Data; 8 using System.Linq.Expressions; 9 10 11 namespace MISMODEL.DAL 12 { 13 public class BaseRepository<T> : IBaseRepository<T> where T : class ,new() 14 { 15 public MISDBEntities db = ContextFactory.GetCurrentContext(); 16 public void SetLazyLoading(bool flag) 17 { 18 db.Configuration.LazyLoadingEnabled = flag; 19 } 20 //1实现对数据库的增加功能 21 public bool AddEntity(T entity) 22 { 23 db.Set<T>().Add(entity); 24 return db.SaveChanges() > 0; 25 } 26 public bool AddEntity(T entity,bool isSave) 27 { 28 db.Set<T>().Add(entity); 29 if (isSave) 30 { 31 return db.SaveChanges() > 0; 32 } 33 else { 34 return false; 35 } 36 // return isSave ? db.SaveChanges() > 0: false; 37 } 38 //2实现对数据库的编辑功能 39 public bool UpdateEntity(T entity) 40 { 41 db.Set<T>().Attach(entity); 42 db.Entry<T>(entity).State = EntityState.Modified; 43 44 return db.SaveChanges() > 0; 45 } 46 //3实现对数据库的删除功能 47 public bool DeleteEntity(T entity) 48 { 49 db.Set<T>().Attach(entity); 50 db.Entry<T>(entity).State = EntityState.Deleted; 51 return db.SaveChanges() > 0; 52 } 53 public bool DeleteEntity(List<T> entitys, bool isSave) 54 { 55 foreach (var T in entitys) { 56 db.Set<T>().Attach(T); 57 db.Entry<T>(T).State = EntityState.Deleted; 58 } 59 if (isSave) 60 { 61 return db.SaveChanges() > 0; 62 } 63 else 64 { 65 return false; 66 } 67 } 68 public bool Commit() { 69 return db.SaveChanges() > 0; 70 } 71 //4实现对数据库的查询 --主键查询 72 public T FindByID(Expression<Func<T, bool>> whereLambda) 73 { 74 return db.Set<T>().AsNoTracking().FirstOrDefault(whereLambda); 75 } 76 //5实现对数据库的查询 --条件查询 77 public IQueryable<T> LoadEntities(Expression<Func<T, bool>> whereLambda) 78 { 79 return db.Set<T>().AsNoTracking().Where<T>(whereLambda); 80 } 81 public DbSet<T> LoadDbSetEntities() 82 { 83 return db.Set<T>(); 84 } 85 //6实现对数据库的查询 --无条件查询 86 public IQueryable<T> LoadEntities() 87 { 88 return db.Set<T>().AsNoTracking().AsQueryable(); 89 } 90 //7实现对数据库的查询 --首行查询 91 public T FirstOrDefaultEntities(Expression<Func<T, bool>> whereLambda) 92 { 93 return db.Set<T>().FirstOrDefault<T>(whereLambda); 94 } 95 //8实现对数据库的查询 --动态条件查询 96 public IQueryable<T> SqlQuery(string sqlstring, params object[] paramertes) 97 { 98 return db.Database.SqlQuery<T>(sqlstring, paramertes).AsQueryable(); 99 } 100 //9实现对数据库的查询 --分页 101 public IQueryable<T> LoadPageEntities<S>(int pageIndex, int pageSize, out int total, Expression<Func<T, bool>> whereLambda, bool isAsc, Func<T, S> orderByLambda) 102 { 103 var temp = db.Set<T>().Where<T>(whereLambda); 104 total = temp.Count(); //得到总的条数 105 //排序,获取当前页的数据 106 if (isAsc) 107 { 108 temp = temp.OrderBy<T, S>(orderByLambda) 109 .Skip<T>(pageSize * (pageIndex - 1)) //越过多少条 110 .Take<T>(pageSize).AsQueryable(); //取出多少条 111 } 112 else 113 { 114 temp = temp.OrderByDescending<T, S>(orderByLambda) 115 .Skip<T>(pageSize * (pageIndex - 1)) //越过多少条 116 .Take<T>(pageSize).AsQueryable(); //取出多少条 117 } 118 return temp.AsQueryable(); 119 } 120 } 121 }
3.角色和组织机构数据接口
1 public interface IG_orgaRepository:IBaseRepository<G_orga> //生成接口 2 { 3 } 4 5 public interface IG_roleRepository:IBaseRepository<G_role> //生成接口 6 { 7 }
4.角色和组织机构数据层实现
public class G_orgaRepository:BaseRepository<G_orga>,IG_orgaRepository //生成实体对象 { } public class G_roleRepository:BaseRepository<G_role>,IG_roleRepository //生成实体对象
5.为实现数据线程唯一,新建类ContextFactory
BaseRepository<T>调用该类
public MISDBEntities db = ContextFactory.GetCurrentContext();
public class ContextFactory { /// <summary> /// 获取当前数据上下文 /// </summary> /// <returns></returns> public static MISDBEntities GetCurrentContext() { MISDBEntities _nContext = CallContext.GetData("MISDB") as MISDBEntities; if (_nContext == null) { _nContext = new MISDBEntities(); CallContext.SetData("MISDB", _nContext); } _nContext.Configuration.LazyLoadingEnabled = false; return _nContext; } }
6.为降低系统耦合建立工厂类RepositoryFactory
public static class RepositoryFactory { public static IG_orgaRepository G_orgaRepository { get { return new G_orgaRepository(); } } public static IG_rolemenuRepository G_rolemenuRepository { get { return new G_rolemenuRepository(); } } }
时间: 2024-10-27 12:55:08