标签:
1 一、接口设计 2 1.1. IBaseRepository.cs 3 public interface IBaseRepository<T> 4 { 5 T Add(T entity); 6 bool Update(T entity); 7 bool Delete(T entity); 8 IQueryable<T> LoadEntities(Expression<Func<T, bool>> whereLambda); 9 IQueryable<T> LoadPageEntities<S>(int pageSize, int pageIndex, out int total, Expression<Func<T, bool>> whereLambda, Expression<Func<T, S>> orderbyLambda, bool isAsc); 10 11 } 12 1.2. ICompanyRepository.cs 13 public interface ICompanyRepository:IBaseRepository<Company> 14 { 15 16 } 17 二、接口实现 18 2.1 BaseRepository.cs 19 public class BaseRepository<T> where T:class,new() 20 { 21 private ObjectContext dbContext 22 { 23 get 24 { 25 return EFDbContextFactory.GetCurrentDbContext(); 26 } 27 } 28 public virtual T Add(T entity) 29 { 30 dbContext.CreateObjectSet<T>().AddObject(entity); 31 return entity; 32 } 33 34 public virtual bool Update(T entity) 35 { 36 37 dbContext.ObjectStateManager.ChangeObjectState(entity, System.Data.EntityState.Modified); 38 return true; 39 } 40 41 public virtual bool Delete(T entity) 42 { 43 dbContext.ObjectStateManager.ChangeObjectState(entity,System.Data.EntityState.Deleted); 44 return true; 45 } 46 47 public IQueryable<T> LoadEntities(Expression<Func<T, bool>> whereLambda) 48 { 49 return dbContext.CreateObjectSet<T>().Where(whereLambda).AsQueryable(); 50 } 51 public IQueryable<T> LoadPageEntities<S>(int pageSize, int pageIndex, out int total, Expression<Func<T, bool>> whereLambda, Expression<Func<T,S>> orderbyLambda, bool isAsc) 52 { 53 total = dbContext.CreateObjectSet<T>().Where(whereLambda).Count(); 54 if (isAsc) 55 { 56 return dbContext.CreateObjectSet<T>() 57 .Where(whereLambda) 58 .OrderBy(orderbyLambda) 59 .Skip(pageSize * (pageIndex - 1)) 60 .AsQueryable(); 61 } 62 else 63 { 64 return dbContext.CreateObjectSet<T>() 65 .Where(whereLambda) 66 .OrderByDescending(orderbyLambda) 67 .Skip(pageSize * (pageIndex - 1)) 68 .AsQueryable(); 69 } 70 } 71 } 72 2.2 CompanyRepository.cs 73 public class CompanyRepository : BaseRepository<Company>,ICompanyRepository 74 { 75 76 } 77 三、IDbSession 78 3.1 IDbSession.cs 79 public interface IDbSession 80 { 81 IMemberRepository MemberRepository { get; } 82 IServiceTypeRepository ServiceTypeRepository { get; } 83 IServiceRepository ServiceRepository { get; } 84 ICompanyRepository CompanyRepository { get; } 85 IProductGroupRepository ProductGroupRepository { get; } 86 int SaveChanges(); 87 } 88 3.2 DbSession.cs 89 public class DbSession : IDbSession 90 { 91 private IMemberRepository _MemberRepository; 92 private IServiceTypeRepository _ServiceTypeRepository; 93 private IServiceRepository _ServiceRepository; 94 private ICompanyRepository _CompanyRepository; 95 private IProductGroupRepository _ProductGroupRepository; 96 97 public IMemberRepository MemberRepository 98 { 99 get 100 { 101 if (_MemberRepository == null) 102 { 103 _MemberRepository = new MemberRepository(); 104 } 105 return _MemberRepository; 106 } 107 } 108 109 public IServiceTypeRepository ServiceTypeRepository 110 { 111 get 112 { 113 if (_ServiceTypeRepository == null) 114 { 115 _ServiceTypeRepository = new ServiceTypeRepository(); 116 } 117 return _ServiceTypeRepository; 118 } 119 } 120 121 public IServiceRepository ServiceRepository 122 { 123 get 124 { 125 if (_ServiceRepository == null) 126 { 127 _ServiceRepository = new ServiceRepository(); 128 } 129 return _ServiceRepository; 130 } 131 } 132 133 public ICompanyRepository CompanyRepository 134 { 135 get 136 { 137 if (_CompanyRepository == null) 138 { 139 _CompanyRepository = new CompanyRepository(); 140 } 141 return _CompanyRepository; 142 } 143 } 144 145 public IProductGroupRepository ProductGroupRepository 146 { 147 get 148 { 149 if (_ProductGroupRepository == null) 150 { 151 _ProductGroupRepository = new ProductGroupRepository(); 152 } 153 return _ProductGroupRepository; 154 } 155 } 156 157 public int SaveChanges() 158 { 159 return EFDbContextFactory.GetCurrentDbContext().SaveChanges(); 160 } 161 162 163 164 } 165 3.3 EFDbContextFactory.cs 166 public class EFDbContextFactory 167 { 168 public static ObjectContext GetCurrentDbContext() 169 { 170 171 //线程内唯一 172 ObjectContext dbContext = (ObjectContext)CallContext.GetData("dbContext"); 173 if (dbContext == null) 174 { 175 dbContext = new SimpleNewsContext(); 176 CallContext.SetData("dbContext", dbContext); 177 } 178 return dbContext; 179 } 180 181 }
标签:
原文地址:http://www.cnblogs.com/weishuanbao/p/5564032.html