爱思开
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

162 lines
7.0 KiB

2 years ago
  1. using NFine.Code;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Data.Common;
  5. using System.Data.Entity;
  6. using System.Data.Entity.Infrastructure;
  7. using System.Linq;
  8. using System.Linq.Expressions;
  9. using System.Reflection;
  10. using System.Text.RegularExpressions;
  11. namespace NFine.Data
  12. {
  13. /// <summary>
  14. /// 仓储实现
  15. /// </summary>
  16. /// <typeparam name="TEntity"></typeparam>
  17. public class RepositoryBase<TEntity> : IRepositoryBase<TEntity> where TEntity : class,new()
  18. {
  19. public NFineDbContext dbcontext = new NFineDbContext();
  20. public int Insert(TEntity entity)
  21. {
  22. dbcontext.Entry<TEntity>(entity).State = EntityState.Added;
  23. return dbcontext.SaveChanges();
  24. }
  25. public int Insert(List<TEntity> entitys)
  26. {
  27. foreach (var entity in entitys)
  28. {
  29. dbcontext.Entry<TEntity>(entity).State = EntityState.Added;
  30. }
  31. return dbcontext.SaveChanges();
  32. }
  33. public int Update(TEntity entity)
  34. {
  35. RemoveHoldingEntityInContext(entity);
  36. dbcontext.Set<TEntity>().Attach(entity);
  37. PropertyInfo[] props = entity.GetType().GetProperties();
  38. foreach (PropertyInfo prop in props)
  39. {
  40. if (prop.GetValue(entity, null) != null)
  41. {
  42. if (prop.GetValue(entity, null).ToString() == "&nbsp;")
  43. dbcontext.Entry(entity).Property(prop.Name).CurrentValue = null;
  44. dbcontext.Entry(entity).Property(prop.Name).IsModified = true;
  45. }
  46. }
  47. return dbcontext.SaveChanges();
  48. }
  49. //用于监测Context中的Entity是否存在,如果存在,将其Detach,防止出现问题。
  50. private Boolean RemoveHoldingEntityInContext(TEntity entity)
  51. {
  52. var objContext = ((IObjectContextAdapter)dbcontext).ObjectContext;
  53. var objSet = objContext.CreateObjectSet<TEntity>();
  54. var entityKey = objContext.CreateEntityKey(objSet.EntitySet.Name, entity);
  55. Object foundEntity;
  56. var exists = objContext.TryGetObjectByKey(entityKey, out foundEntity);
  57. if (exists)
  58. {
  59. objContext.Detach(foundEntity);
  60. }
  61. return (exists);
  62. }
  63. public int Delete(TEntity entity)
  64. {
  65. dbcontext.Set<TEntity>().Attach(entity);
  66. dbcontext.Entry<TEntity>(entity).State = EntityState.Deleted;
  67. return dbcontext.SaveChanges();
  68. }
  69. public int Delete(Expression<Func<TEntity, bool>> predicate)
  70. {
  71. var entitys = dbcontext.Set<TEntity>().Where(predicate).ToList();
  72. entitys.ForEach(m => dbcontext.Entry<TEntity>(m).State = EntityState.Deleted);
  73. return dbcontext.SaveChanges();
  74. }
  75. public TEntity FindEntity(object keyValue)
  76. {
  77. return dbcontext.Set<TEntity>().Find(keyValue);
  78. }
  79. public TEntity FindEntity(Expression<Func<TEntity, bool>> predicate)
  80. {
  81. return dbcontext.Set<TEntity>().FirstOrDefault(predicate);
  82. }
  83. public IQueryable<TEntity> IQueryable()
  84. {
  85. return dbcontext.Set<TEntity>();
  86. }
  87. public IQueryable<TEntity> IQueryable(Expression<Func<TEntity, bool>> predicate)
  88. {
  89. return dbcontext.Set<TEntity>().Where(predicate);
  90. }
  91. public List<TEntity> FindList(string strSql)
  92. {
  93. return dbcontext.Database.SqlQuery<TEntity>(strSql).ToList<TEntity>();
  94. }
  95. public List<TEntity> FindList(string strSql, DbParameter[] dbParameter)
  96. {
  97. return dbcontext.Database.SqlQuery<TEntity>(strSql, dbParameter).ToList<TEntity>();
  98. }
  99. public List<TEntity> FindList(Pagination pagination)
  100. {
  101. bool isAsc = pagination.sord.ToLower() == "asc" ? true : false;
  102. string[] _order = pagination.sidx.Split(',');
  103. MethodCallExpression resultExp = null;
  104. var tempData = dbcontext.Set<TEntity>().AsQueryable();
  105. foreach (string item in _order)
  106. {
  107. string _orderPart = item;
  108. _orderPart = Regex.Replace(_orderPart, @"\s+", " ");
  109. string[] _orderArry = _orderPart.Split(' ');
  110. string _orderField = _orderArry[0];
  111. bool sort = isAsc;
  112. if (_orderArry.Length == 2)
  113. {
  114. isAsc = _orderArry[1].ToUpper() == "ASC" ? true : false;
  115. }
  116. var parameter = Expression.Parameter(typeof(TEntity), "t");
  117. var property = typeof(TEntity).GetProperty(_orderField);
  118. var propertyAccess = Expression.MakeMemberAccess(parameter, property);
  119. var orderByExp = Expression.Lambda(propertyAccess, parameter);
  120. resultExp = Expression.Call(typeof(Queryable), isAsc ? "OrderBy" : "OrderByDescending", new Type[] { typeof(TEntity), property.PropertyType }, tempData.Expression, Expression.Quote(orderByExp));
  121. }
  122. tempData = tempData.Provider.CreateQuery<TEntity>(resultExp);
  123. pagination.records = tempData.Count();
  124. tempData = tempData.Skip<TEntity>(pagination.rows * (pagination.page - 1)).Take<TEntity>(pagination.rows).AsQueryable();
  125. return tempData.ToList();
  126. }
  127. public List<TEntity> FindList(Expression<Func<TEntity, bool>> predicate, Pagination pagination)
  128. {
  129. bool isAsc = pagination.sord.ToLower() == "asc" ? true : false;
  130. string[] _order = pagination.sidx.Split(',');
  131. MethodCallExpression resultExp = null;
  132. var tempData = dbcontext.Set<TEntity>().Where(predicate);
  133. foreach (string item in _order)
  134. {
  135. string _orderPart = item;
  136. _orderPart = Regex.Replace(_orderPart, @"\s+", " ");
  137. string[] _orderArry = _orderPart.Split(' ');
  138. string _orderField = _orderArry[0];
  139. bool sort = isAsc;
  140. if (_orderArry.Length == 2)
  141. {
  142. isAsc = _orderArry[1].ToUpper() == "ASC" ? true : false;
  143. }
  144. var parameter = Expression.Parameter(typeof(TEntity), "t");
  145. var property = typeof(TEntity).GetProperty(_orderField);
  146. var propertyAccess = Expression.MakeMemberAccess(parameter, property);
  147. var orderByExp = Expression.Lambda(propertyAccess, parameter);
  148. resultExp = Expression.Call(typeof(Queryable), isAsc ? "OrderBy" : "OrderByDescending", new Type[] { typeof(TEntity), property.PropertyType }, tempData.Expression, Expression.Quote(orderByExp));
  149. }
  150. tempData = tempData.Provider.CreateQuery<TEntity>(resultExp);
  151. pagination.records = tempData.Count();
  152. tempData = tempData.Skip<TEntity>(pagination.rows * (pagination.page - 1)).Take<TEntity>(pagination.rows).AsQueryable();
  153. return tempData.ToList();
  154. }
  155. }
  156. }