纽威
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.

185 lines
8.0 KiB

3 years ago
  1. using NFine.Code;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Data;
  5. using System.Data.Common;
  6. using System.Data.Entity;
  7. using System.Data.Entity.Infrastructure;
  8. using System.Linq;
  9. using System.Linq.Expressions;
  10. using System.Reflection;
  11. using System.Text.RegularExpressions;
  12. namespace NFine.Data
  13. {
  14. /// <summary>
  15. /// 仓储实现
  16. /// </summary>
  17. public class RepositoryBase : IRepositoryBase, IDisposable
  18. {
  19. private NFineDbContext dbcontext = new NFineDbContext();
  20. private DbTransaction dbTransaction { get; set; }
  21. public IRepositoryBase BeginTrans()
  22. {
  23. DbConnection dbConnection = ((IObjectContextAdapter)dbcontext).ObjectContext.Connection;
  24. if (dbConnection.State == ConnectionState.Closed)
  25. {
  26. dbConnection.Open();
  27. }
  28. dbTransaction = dbConnection.BeginTransaction();
  29. return this;
  30. }
  31. public int Commit()
  32. {
  33. try
  34. {
  35. var returnValue = dbcontext.SaveChanges();
  36. if (dbTransaction != null)
  37. {
  38. dbTransaction.Commit();
  39. }
  40. return returnValue;
  41. }
  42. catch (Exception)
  43. {
  44. if (dbTransaction != null)
  45. {
  46. this.dbTransaction.Rollback();
  47. }
  48. throw;
  49. }
  50. finally
  51. {
  52. this.Dispose();
  53. }
  54. }
  55. public void Dispose()
  56. {
  57. if (dbTransaction != null)
  58. {
  59. this.dbTransaction.Dispose();
  60. }
  61. this.dbcontext.Dispose();
  62. }
  63. public int Insert<TEntity>(TEntity entity) where TEntity : class
  64. {
  65. dbcontext.Entry<TEntity>(entity).State = EntityState.Added;
  66. return dbTransaction == null ? this.Commit() : 0;
  67. }
  68. public int Insert<TEntity>(List<TEntity> entitys) where TEntity : class
  69. {
  70. foreach (var entity in entitys)
  71. {
  72. dbcontext.Entry<TEntity>(entity).State = EntityState.Added;
  73. }
  74. return dbTransaction == null ? this.Commit() : 0;
  75. }
  76. public int Update<TEntity>(TEntity entity) where TEntity : class
  77. {
  78. dbcontext.Set<TEntity>().Attach(entity);
  79. PropertyInfo[] props = entity.GetType().GetProperties();
  80. foreach (PropertyInfo prop in props)
  81. {
  82. if (prop.GetValue(entity, null) != null)
  83. {
  84. if (prop.GetValue(entity, null).ToString() == "&nbsp;")
  85. dbcontext.Entry(entity).Property(prop.Name).CurrentValue = null;
  86. dbcontext.Entry(entity).Property(prop.Name).IsModified = true;
  87. }
  88. }
  89. return dbTransaction == null ? this.Commit() : 0;
  90. }
  91. public int Delete<TEntity>(TEntity entity) where TEntity : class
  92. {
  93. dbcontext.Set<TEntity>().Attach(entity);
  94. dbcontext.Entry<TEntity>(entity).State = EntityState.Deleted;
  95. return dbTransaction == null ? this.Commit() : 0;
  96. }
  97. public int Delete<TEntity>(Expression<Func<TEntity, bool>> predicate) where TEntity : class
  98. {
  99. var entitys = dbcontext.Set<TEntity>().Where(predicate).ToList();
  100. entitys.ForEach(m => dbcontext.Entry<TEntity>(m).State = EntityState.Deleted);
  101. return dbTransaction == null ? this.Commit() : 0;
  102. }
  103. public TEntity FindEntity<TEntity>(object keyValue) where TEntity : class
  104. {
  105. return dbcontext.Set<TEntity>().Find(keyValue);
  106. }
  107. public TEntity FindEntity<TEntity>(Expression<Func<TEntity, bool>> predicate) where TEntity : class
  108. {
  109. return dbcontext.Set<TEntity>().FirstOrDefault(predicate);
  110. }
  111. public IQueryable<TEntity> IQueryable<TEntity>() where TEntity : class
  112. {
  113. return dbcontext.Set<TEntity>();
  114. }
  115. public IQueryable<TEntity> IQueryable<TEntity>(Expression<Func<TEntity, bool>> predicate) where TEntity : class
  116. {
  117. return dbcontext.Set<TEntity>().Where(predicate);
  118. }
  119. public List<TEntity> FindList<TEntity>(string strSql) where TEntity : class
  120. {
  121. return dbcontext.Database.SqlQuery<TEntity>(strSql).ToList<TEntity>();
  122. }
  123. public List<TEntity> FindList<TEntity>(string strSql, DbParameter[] dbParameter) where TEntity : class
  124. {
  125. return dbcontext.Database.SqlQuery<TEntity>(strSql, dbParameter).ToList<TEntity>();
  126. }
  127. public List<TEntity> FindList<TEntity>(Pagination pagination) where TEntity : class,new()
  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>().AsQueryable();
  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. public List<TEntity> FindList<TEntity>(Expression<Func<TEntity, bool>> predicate, Pagination pagination) where TEntity : class,new()
  156. {
  157. bool isAsc = pagination.sord.ToLower() == "asc" ? true : false;
  158. string[] _order = pagination.sidx.Split(',');
  159. MethodCallExpression resultExp = null;
  160. var tempData = dbcontext.Set<TEntity>().Where(predicate);
  161. foreach (string item in _order)
  162. {
  163. string _orderPart = item;
  164. _orderPart = Regex.Replace(_orderPart, @"\s+", " ");
  165. string[] _orderArry = _orderPart.Split(' ');
  166. string _orderField = _orderArry[0];
  167. bool sort = isAsc;
  168. if (_orderArry.Length == 2)
  169. {
  170. isAsc = _orderArry[1].ToUpper() == "ASC" ? true : false;
  171. }
  172. var parameter = Expression.Parameter(typeof(TEntity), "t");
  173. var property = typeof(TEntity).GetProperty(_orderField);
  174. var propertyAccess = Expression.MakeMemberAccess(parameter, property);
  175. var orderByExp = Expression.Lambda(propertyAccess, parameter);
  176. resultExp = Expression.Call(typeof(Queryable), isAsc ? "OrderBy" : "OrderByDescending", new Type[] { typeof(TEntity), property.PropertyType }, tempData.Expression, Expression.Quote(orderByExp));
  177. }
  178. tempData = tempData.Provider.CreateQuery<TEntity>(resultExp);
  179. pagination.records = tempData.Count();
  180. tempData = tempData.Skip<TEntity>(pagination.rows * (pagination.page - 1)).Take<TEntity>(pagination.rows).AsQueryable();
  181. return tempData.ToList();
  182. }
  183. }
  184. }