C#数据仓储类

https://ninesky.codeplex.com/SourceControl/latest

  1 /*==============================
  2 版本:v0.1
  3 创建:2016.2.6
  4 作者:洞庭夕照
  5 博客:http://mzwhj.cnblogs.com
  6 -----------------------------
  7 修改:2016.3.4
  8 public int Delete(T entity, bool isSave)
  9 Remove 改为Attach
 10 -----------------------------
 11 修改:2016.3.17
 12 public IQueryable<T> FindPageList(int pageSize, int pageIndex, out int totalNumber, Expression<Func<T, bool>> where, OrderParam[] orderParams)
 13 排序参数Expression<Func<T, TKey>> order, bool asc 改为  OrderParam[] orderParams
 14 ==============================*/
 15 
 16 using System;
 17 using System.Collections.Generic;
 18 using System.Data.Entity;
 19 using System.Linq;
 20 using System.Linq.Expressions;
 21 using Ninesky.Auxiliary;
 22 
 23 namespace Ninesky.DataLibrary
 24 {
 25     /// <summary>
 26     /// 数据仓储类
 27     /// </summary>
 28     /// <typeparam name="T">实体模型</typeparam>
 29     public class Repository<T> where T : class
 30     {
 31         /// <summary>
 32         /// 数据上下文
 33         /// </summary>
 34         public DbContext DbContext { get; set; }
 35 
 36         public Repository()
 37         { }
 38 
 39         /// <summary>
 40         /// 构造函数
 41         /// </summary>
 42         /// <param name="dbContext">数据上下文</param>
 43         public Repository(DbContext dbContext)
 44         {
 45             DbContext = dbContext;
 46         }
 47 
 48         //查找实体
 49         #region Find
 50         /// <summary>
 51         /// 查找实体
 52         /// </summary>
 53         /// <param name="ID">实体主键值</param>
 54         /// <returns></returns>
 55         public T Find(int ID)
 56         {
 57             return DbContext.Set<T>().Find(ID);
 58         }
 59 
 60         /// <summary>
 61         /// 查找实体
 62         /// </summary>
 63         /// <param name="where">查询Lambda表达式</param>
 64         /// <returns></returns>
 65         public T Find(Expression<Func<T, bool>> where)
 66         {
 67             return DbContext.Set<T>().SingleOrDefault(where);
 68         }
 69         #endregion
 70 
 71         //查找实体列表
 72         #region FindList
 73         /// <summary>
 74         /// 查找实体列表
 75         /// </summary>
 76         /// <returns></returns>
 77         public IQueryable<T> FindList()
 78         {
 79             return DbContext.Set<T>();
 80         }
 81 
 82         /// <summary>
 83         /// 查找实体列表
 84         /// </summary>
 85         /// <param name="where">查询Lambda表达式</param>
 86         /// <returns></returns>
 87         public IQueryable<T> FindList(Expression<Func<T, bool>> where)
 88         {
 89             return DbContext.Set<T>().Where(where);
 90         }
 91 
 92         /// <summary>
 93         /// 查找实体列表
 94         /// </summary>
 95         /// <param name="where">查询Lambda表达式</param>
 96         /// <param name="number">获取的记录数量</param>
 97         /// <returns></returns>
 98         public IQueryable<T> FindList(Expression<Func<T, bool>> where, int number)
 99         {
100             return DbContext.Set<T>().Where(where).Take(number);
101         }
102 
103         /// <summary>
104         /// 查找实体列表
105         /// </summary>
106         /// <param name="where">查询Lambda表达式</param>
107         /// <param name="orderParam">排序参数</param>
108         /// <returns></returns>
109         public IQueryable<T> FindList(Expression<Func<T, bool>> where, OrderParam orderParam)
110         {
111             return FindList(where, orderParam, 0);
112         }
113 
114         /// <summary>
115         /// 查找实体列表
116         /// </summary>
117         /// <param name="where">查询Lambda表达式</param>
118         /// <param name="orderParam">排序参数</param>
119         /// <param name="number">获取的记录数量【0-不启用】</param>
120         public IQueryable<T> FindList(Expression<Func<T, bool>> where, OrderParam orderParam, int number)
121         {
122             OrderParam[] _orderParams = null;
123             if (orderParam != null) _orderParams = new OrderParam[] { orderParam };
124             return FindList(where, _orderParams, number);
125         }
126 
127         /// <summary>
128         /// 查找实体列表
129         /// </summary>
130         /// <param name="where">查询Lambda表达式</param>
131         /// <param name="orderParams">排序参数</param>
132         /// <param name="number">获取的记录数量【0-不启用】</param>
133         /// <returns></returns>
134         public IQueryable<T> FindList(Expression<Func<T, bool>> where, OrderParam[] orderParams, int number)
135         {
136             var _list = DbContext.Set<T>().Where(where);
137             var _orderParames = Expression.Parameter(typeof(T), "o");
138             if (orderParams != null && orderParams.Length > 0)
139             {
140                 bool _isFirstParam = true;
141                 for (int i = 0; i < orderParams.Length; i++)
142                 {
143                     //根据属性名获取属性
144                     var _property = typeof(T).GetProperty(orderParams[i].PropertyName);
145                     //创建一个访问属性的表达式
146                     var _propertyAccess = Expression.MakeMemberAccess(_orderParames, _property);
147                     var _orderByExp = Expression.Lambda(_propertyAccess, _orderParames);
148                     string _orderName;
149                     if (_isFirstParam)
150                     {
151                         _orderName = orderParams[i].Method == OrderMethod.ASC ? "OrderBy" : "OrderByDescending";
152                         _isFirstParam = false;
153                     }
154                     else _orderName = orderParams[i].Method == OrderMethod.ASC ? "ThenBy" : "ThenByDescending";
155                     MethodCallExpression resultExp = Expression.Call(typeof(Queryable), _orderName, new Type[] { typeof(T), _property.PropertyType }, _list.Expression, Expression.Quote(_orderByExp));
156                     _list = _list.Provider.CreateQuery<T>(resultExp);
157                 }
158             }
159             if (number > 0) _list = _list.Take(number);
160             return _list;
161         }
162         #endregion
163 
164         //查找实体分页列表
165         #region FindPageList
166 
167         /// <summary>
168         /// 查找分页列表
169         /// </summary>
170         /// <param name="pageSize">每页记录数。必须大于1</param>
171         /// <param name="pageIndex">页码。首页从1开始,页码必须大于1</param>
172         /// <param name="totalNumber">总记录数</param>
173         /// <returns></returns>
174         public IQueryable<T> FindPageList(int pageSize, int pageIndex, out int totalNumber)
175         {
176             OrderParam _orderParam = null;
177             return FindPageList(pageSize, pageIndex, out totalNumber, _orderParam);
178         }
179 
180         /// <summary>
181         /// 查找分页列表
182         /// </summary>
183         /// <param name="pageSize">每页记录数。必须大于1</param>
184         /// <param name="pageIndex">页码。首页从1开始,页码必须大于1</param>
185         /// <param name="totalNumber">总记录数</param>
186         /// <param name="order">排序键</param>
187         /// <param name="asc">是否正序</param>
188         /// <returns></returns>
189         public IQueryable<T> FindPageList(int pageSize, int pageIndex, out int totalNumber, OrderParam orderParam)
190         {
191             return FindPageList(pageSize, pageIndex, out totalNumber, (T) => true, orderParam);
192         }
193 
194         /// <summary>
195         /// 查找分页列表
196         /// </summary>
197         /// <param name="pageSize">每页记录数。必须大于1</param>
198         /// <param name="pageIndex">页码。首页从1开始,页码必须大于1</param>
199         /// <param name="totalNumber">总记录数</param>
200         /// <param name="where">查询表达式</param>
201         public IQueryable<T> FindPageList(int pageSize, int pageIndex, out int totalNumber, Expression<Func<T, bool>> where)
202         {
203             OrderParam _param = null;
204             return FindPageList(pageSize, pageIndex, out totalNumber, where, _param);
205         }
206 
207         /// <summary>
208         /// 查找分页列表
209         /// </summary>
210         /// <param name="pageSize">每页记录数。</param>
211         /// <param name="pageIndex">页码。首页从1开始</param>
212         /// <param name="totalNumber">总记录数</param>
213         /// <param name="where">查询表达式</param>
214         /// <param name="orderParam">排序【null-不设置】</param>
215         /// <returns></returns>
216         public IQueryable<T> FindPageList(int pageSize, int pageIndex, out int totalNumber, Expression<Func<T, bool>> where, OrderParam orderParam)
217         {
218             OrderParam[] _orderParams = null;
219             if (orderParam != null) _orderParams = new OrderParam[] { orderParam };
220             return FindPageList(pageSize, pageIndex, out totalNumber, where, _orderParams);
221         }
222 
223         /// <summary>
224         /// 查找分页列表
225         /// </summary>
226         /// <param name="pageSize">每页记录数。</param>
227         /// <param name="pageIndex">页码。首页从1开始</param>
228         /// <param name="totalNumber">总记录数</param>
229         /// <param name="where">查询表达式</param>
230         /// <param name="orderParams">排序【null-不设置】</param>
231         public IQueryable<T> FindPageList(int pageSize, int pageIndex, out int totalNumber, Expression<Func<T, bool>> where, OrderParam[] orderParams)
232         {
233             if (pageIndex < 1) pageIndex = 1;
234             if (pageSize < 1) pageSize = 10;
235             IQueryable<T> _list = DbContext.Set<T>().Where(where);
236             var _orderParames = Expression.Parameter(typeof(T), "o");
237             if (orderParams != null && orderParams.Length > 0)
238             {
239                 for (int i = 0; i < orderParams.Length; i++)
240                 {
241                     //根据属性名获取属性
242                     var _property = typeof(T).GetProperty(orderParams[i].PropertyName);
243                     //创建一个访问属性的表达式
244                     var _propertyAccess = Expression.MakeMemberAccess(_orderParames, _property);
245                     var _orderByExp = Expression.Lambda(_propertyAccess, _orderParames);
246                     string _orderName = orderParams[i].Method == OrderMethod.ASC ? "OrderBy" : "OrderByDescending";
247                     MethodCallExpression resultExp = Expression.Call(typeof(Queryable), _orderName, new Type[] { typeof(T), _property.PropertyType }, _list.Expression, Expression.Quote(_orderByExp));
248                     _list = _list.Provider.CreateQuery<T>(resultExp);
249                 }
250             }
251             totalNumber = _list.Count();
252             return _list.Skip((pageIndex - 1) * pageSize).Take(pageSize);
253         }
254 
255         #endregion
256 
257         //添加实体
258         #region Add
259 
260         /// <summary>
261         /// 添加实体【立即保存】
262         /// </summary>
263         /// <param name="entity">实体</param>
264         /// <returns>受影响的对象的数目</returns>
265         public int Add(T entity)
266         {
267             return Add(entity, true);
268         }
269 
270         /// <summary>
271         /// 添加实体
272         /// </summary>
273         /// <param name="entity">实体</param>
274         /// <param name="isSave">是否立即保存</param>
275         /// <returns>在“isSave”为True时返回受影响的对象的数目,为False时直接返回0</returns>
276         public int Add(T entity, bool isSave)
277         {
278             DbContext.Set<T>().Add(entity);
279             return isSave ? DbContext.SaveChanges() : 0;
280         }
281 
282         #endregion
283 
284         //更新实体
285         #region Update
286 
287         /// <summary>
288         /// 更新实体【立即保存】
289         /// </summary>
290         /// <param name="entity">实体</param>
291         /// <returns>受影响的对象的数目</returns>
292         public int Update(T entity)
293         {
294             return Update(entity, true);
295         }
296 
297         /// <summary>
298         /// 更新实体
299         /// </summary>
300         /// <param name="entity">实体</param>
301         /// <param name="isSave">是否立即保存</param>
302         /// <returns>在“isSave”为True时返回受影响的对象的数目,为False时直接返回0</returns>
303         public int Update(T entity, bool isSave)
304         {
305             DbContext.Set<T>().Attach(entity);
306             DbContext.Entry<T>(entity).State = EntityState.Modified;
307             return isSave ? DbContext.SaveChanges() : 0;
308         }
309         #endregion
310 
311         //删除
312         #region Delete
313 
314         /// <summary>
315         /// 删除实体【立即保存】
316         /// </summary>
317         /// <param name="entity">实体</param>
318         /// <returns>受影响的对象的数目</returns>
319         public int Delete(T entity)
320         {
321             return Delete(entity, true);
322         }
323 
324         /// <summary>
325         /// 删除实体
326         /// </summary>
327         /// <param name="entity">实体</param>
328         /// <param name="isSave">是否立即保存</param>
329         /// <returns>在“isSave”为True时返回受影响的对象的数目,为False时直接返回0</returns>
330         public int Delete(T entity, bool isSave)
331         {
332             DbContext.Set<T>().Attach(entity);
333             DbContext.Entry<T>(entity).State = EntityState.Deleted;
334             return isSave ? DbContext.SaveChanges() : 0;
335         }
336 
337         /// <summary>
338         /// 批量删除实体
339         /// </summary>
340         /// <param name="entities">实体集合</param>
341         /// <returns>受影响的对象的数目</returns>
342         public int Delete(IEnumerable<T> entities)
343         {
344             DbContext.Set<T>().RemoveRange(entities);
345             return DbContext.SaveChanges();
346         }
347         #endregion
348 
349         //记录数
350         #region Count
351 
352         /// <summary>
353         /// 记录数
354         /// </summary>
355         /// <returns></returns>
356         public int Count()
357         {
358             return DbContext.Set<T>().Count();
359         }
360 
361         /// <summary>
362         /// 记录数
363         /// </summary>
364         /// <param name="predicate">表达式</param>
365         /// <returns></returns>
366         public int Count(Expression<Func<T, bool>> predicate)
367         {
368             return DbContext.Set<T>().Count(predicate);
369         }
370         #endregion
371 
372         /// <summary>
373         /// 记录是否存在
374         /// </summary>
375         /// <param name="predicate">表达式</param>
376         /// <returns></returns>
377         public bool IsContains(Expression<Func<T, bool>> predicate)
378         {
379             return Count(predicate) > 0;
380         }
381 
382         /// <summary>
383         /// 保存数据【在Add、Upate、Delete未立即保存的情况下使用】
384         /// </summary>
385         /// <returns>受影响的记录数</returns>
386         public int Save()
387         {
388             return DbContext.SaveChanges();
389         }
390     }
391 }
原文地址:https://www.cnblogs.com/wanghaibin/p/6196003.html