首页 > 代码库 > 不成功的MVC Repository模式,记录下来,后面看看原因在哪里(二) Repository类
不成功的MVC Repository模式,记录下来,后面看看原因在哪里(二) Repository类
1 public class Repository<T> : IRepository<T> where T : class 2 { 3 public WBIDbContext m_Context=WBIContextFactory.GetCurrentContext();//上下文数据库 4 public DbSet<T> dbSet; 5 public Repository(WBIDbContext dbContext) 6 { 7 this.m_Context = dbContext; 8 this.dbSet = dbContext.Set<T>(); 9 } 10 11 protected IRepository<T> m_currentRepository1 { get; set; } 12 protected IDevTypeRepository m_currentRepository { get; set; } 13 public Repository(IDevTypeRepository currentRepository) 14 { 15 m_currentRepository = currentRepository; 16 } 17 18 public T Add(T entity) 19 { 20 //设置实体的状态 add 21 m_Context.Entry<T>(entity).State = System.Data.Entity.EntityState.Added; 22 //将上下文中所做的更改保存到数据库中 23 m_Context.SaveChanges(); 24 //返回保存后的实体 25 return entity; 26 } 27 28 public bool Update(T entity) 29 { 30 m_Context.Set<T>().Attach(entity); 31 m_Context.Entry<T>(entity).State = System.Data.Entity.EntityState.Modified; 32 return m_Context.SaveChanges() > 0; 33 } 34 35 public bool Delete(T entity) 36 37 { 38 m_Context.Set<T>().Attach(entity); 39 m_Context.Entry<T>(entity).State = System.Data.Entity.EntityState.Deleted; 40 return m_Context.SaveChanges() > 0; 41 } 42 43 //名字是否为空 44 public bool CheckNameIsEmpty(Expression<Func<T, bool>> whereLambda) 45 { 46 return m_Context.Set<T>().Any(whereLambda); 47 } 48 49 //名字是否存在 50 public bool CheckNameExist(Expression<Func<T, bool>> whereLambda) 51 { 52 return m_Context.Set<T>().Any(whereLambda); 53 } 54 //检查其他项 55 public bool Check(Expression<Func<T, bool>> whereLambda) 56 { 57 return m_Context.Set<T>().Any(whereLambda); 58 } 59 //保存 60 //public void Save() 61 //{ 62 // m_Context.SaveChanges(); 63 64 //} 65 66 public int Count(Expression<Func<T, bool>> predicate) 67 { 68 return m_Context.Set<T>().Count(predicate); 69 } 70 71 public List<T> ToList() 72 { 73 return m_Context.Set<T>().ToList(); 74 } 75 76 public T Find(Expression<Func<T, bool>> whereLambda) 77 { 78 T entity = m_Context.Set<T>().FirstOrDefault<T>(whereLambda); 79 return entity; 80 } 81 82 public IQueryable<T> FindList(Expression<Func<T, bool>> whereLamba, string orderName, bool isAsc) 83 { 84 var list = m_Context.Set<T>().Where(whereLamba); 85 list = OrderBy(list, orderName, isAsc); 86 return list; 87 } 88 89 public IQueryable<T> FindList<S>(Expression<Func<T, bool>> whereLambda, bool isAsc, Expression<Func<T, S>> orderLamba) 90 { 91 var list = m_Context.Set<T>().Where<T>(whereLambda); 92 if (isAsc) 93 { 94 list = list.OrderBy<T, S>(orderLamba); 95 } 96 else 97 { 98 list = list.OrderByDescending<T, S>(orderLamba);//降序排列 99 }100 return list;101 }102 103 104 105 106 public IQueryable<T> FindPageList<S>(int pageIndex, int pageSize, out int totalRecordCnt,107 Expression<Func<T, bool>> whereLamdba, bool isAsc, Expression<Func<T, S>> orderLambda)108 {109 var list = m_Context.Set<T>().Where<T>(whereLamdba);110 totalRecordCnt = list.Count();111 112 if (isAsc)113 {114 list = list.OrderBy<T, S>(orderLambda)115 .Skip<T>((pageIndex - 1) * pageSize).Take<T>(pageSize);116 }117 else118 {119 list = list.OrderByDescending<T, S>(orderLambda)120 .Skip<T>((pageIndex - 1) * pageSize).Take<T>(pageSize);121 }122 123 return list;124 }125 126 127 128 #region Private Methods129 /// <summary>130 /// 排序131 /// </summary>132 /// <typeparam name="T">类型</typeparam>133 /// <param name="source">原IQueryable</param>134 /// <param name="propertyName">排序属性名</param>135 /// <param name="isAsc">是否升序</param>136 /// <returns>排序后的IQueryable</returns>137 private IQueryable<T> OrderBy(IQueryable<T> source, string propertyName, bool isAsc)138 {139 if (source == null)140 {141 throw new ArgumentNullException("source", "不能为空");142 }143 if (string.IsNullOrEmpty(propertyName))144 {145 return source;146 }147 var parameter = Expression.Parameter(source.ElementType);148 var property = Expression.Property(parameter, propertyName);149 if (property == null)150 {151 throw new ArgumentNullException("propertyName", "属性不存在");152 }153 var lambda = Expression.Lambda(property, parameter);154 var methodName = isAsc ? "OrderBy" : "OrderByDescending";155 var resultExpression = Expression.Call(typeof(Queryable), methodName,156 new Type[] { source.ElementType, property.Type }, source.Expression, Expression.Quote(lambda));157 return source.Provider.CreateQuery<T>(resultExpression);158 }159 #endregion160 }
不成功的MVC Repository模式,记录下来,后面看看原因在哪里(二) Repository类
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。