IRepository.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. using DW5S.Entity;
  2. using Microsoft.EntityFrameworkCore;
  3. using Microsoft.EntityFrameworkCore.ChangeTracking;
  4. using Microsoft.EntityFrameworkCore.Query;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Linq.Expressions;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. namespace DW5S.Repostory
  12. {
  13. public interface IRepository<T, IDType> where T : BaseEntity<IDType>
  14. {
  15. IQueryable<T> AsQueryable();
  16. Task<T> GetByIdAsync(object id);
  17. Task<IEnumerable<T>> GetAllAsync();
  18. Task<IEnumerable<T>> GetAllAsync<TKey>(Expression<Func<T, TKey>> keySelector = null, bool asc = true);
  19. Task<IEnumerable<T>> FindAsync(Expression<Func<T, bool>> predicate);
  20. Task<IEnumerable<T>> FindAsync<TKey>(Expression<Func<T, bool>> predicate, Expression<Func<T, TKey>> keySelector = null, bool asc = true);
  21. Task<IEnumerable<Res>> GetDistinctAsync<Res>(Expression<Func<T, Res>> selector);
  22. Task<T> FirstOrDefaultAsync(Expression<Func<T, bool>> predicate = null);
  23. Task<T> AddOrUpdateAsync(T entity);
  24. Task AddOrUpdateAsync(IEnumerable<T> entitis);
  25. /// <summary>
  26. /// 修改某些字段
  27. /// </summary>
  28. /// <typeparam name="TProperty"></typeparam>
  29. /// <param name="predicate"></param>
  30. /// <param name="propertyExpression"></param>
  31. /// <param name="value"></param>
  32. /// <returns></returns>
  33. Task<int> UpdatePropAsync<TProperty>(Expression<Func<T, bool>> predicate, Func<T, TProperty> propertyExpression, TProperty value);
  34. Task<int> DeleteAsync(T entity);
  35. Task<int> DeleteAsync(IEnumerable<T> entitis);
  36. Task<int> DeleteAsync(Expression<Func<T, bool>> predicate = null);
  37. Task<bool> AnyAsync(Expression<Func<T, bool>> predicate = null);
  38. T MaxBy<TResult>(Expression<Func<T, TResult>> selector, Expression<Func<T, bool>> predicate = null);
  39. }
  40. public class Repository<T, IDType> : IRepository<T, IDType> where T : BaseEntity<IDType>
  41. {
  42. protected readonly OracleContext ctx;
  43. protected readonly DbSet<T> dbSet;
  44. public Repository(OracleContext ctx)
  45. {
  46. this.ctx = ctx;
  47. this.dbSet = ctx.Set<T>();
  48. }
  49. public IQueryable<T> AsQueryable()
  50. {
  51. return dbSet.AsQueryable();
  52. }
  53. public async Task<T> GetByIdAsync(object id)
  54. {
  55. return await dbSet.FindAsync(id);
  56. }
  57. public async Task<IEnumerable<T>> GetAllAsync()
  58. {
  59. return await dbSet.ToListAsync();
  60. }
  61. public async Task<IEnumerable<T>> GetAllAsync<TKey>(Expression<Func<T, TKey>> keySelector = null, bool asc = true)
  62. {
  63. if (keySelector == null)
  64. {
  65. return await dbSet.ToListAsync();
  66. }
  67. else
  68. {
  69. if (asc)
  70. return await dbSet.OrderBy(keySelector).ToListAsync();
  71. else
  72. return await dbSet.OrderByDescending(keySelector).ToListAsync();
  73. }
  74. }
  75. public async Task<IEnumerable<T>> FindAsync(Expression<Func<T, bool>> predicate)
  76. {
  77. return await dbSet.Where(predicate).ToListAsync();
  78. }
  79. public async Task<IEnumerable<T>> FindAsync<TKey>(Expression<Func<T, bool>> predicate, Expression<Func<T, TKey>> keySelector = null, bool asc = true)
  80. {
  81. if (keySelector == null)
  82. {
  83. return await dbSet.Where(predicate).ToListAsync();
  84. }
  85. else
  86. {
  87. if (asc)
  88. return await dbSet.Where(predicate).OrderBy(keySelector).ToListAsync();
  89. else
  90. return await dbSet.Where(predicate).OrderByDescending(keySelector).ToListAsync();
  91. }
  92. }
  93. public async Task<T> FirstOrDefaultAsync(Expression<Func<T, bool>> predicate = null)
  94. {
  95. if (predicate != null)
  96. return await dbSet.Where(predicate).FirstOrDefaultAsync();
  97. return await dbSet.FirstOrDefaultAsync();
  98. }
  99. public async Task<IEnumerable<Res>> GetDistinctAsync<Res>(Expression<Func<T, Res>> selector)
  100. {
  101. var res = await dbSet.Select(selector).Distinct().ToListAsync();
  102. return res;
  103. }
  104. public async Task AddOrUpdateAsync(IEnumerable<T> entity)
  105. {
  106. if (entity.First().Id.Equals(0))
  107. {
  108. foreach (var item in entity)
  109. {
  110. item.CreateTime = DateTime.Now;
  111. item.UpdateTime = DateTime.Now;
  112. }
  113. await dbSet.AddRangeAsync(entity);
  114. }
  115. else
  116. {
  117. foreach (var item in entity)
  118. {
  119. item.UpdateTime = DateTime.Now;
  120. }
  121. dbSet.UpdateRange(entity);
  122. }
  123. }
  124. public async Task<T> AddOrUpdateAsync(T entity)
  125. {
  126. if (entity.Id.Equals(0))
  127. {
  128. entity.CreateTime = DateTime.Now;
  129. entity.UpdateTime = DateTime.Now;
  130. var res = await dbSet.AddAsync(entity);
  131. return res.Entity;
  132. }
  133. else
  134. {
  135. entity.UpdateTime = DateTime.Now;
  136. var res = dbSet.Update(entity);
  137. return res.Entity;
  138. }
  139. }
  140. public async Task<int> DeleteAsync(T entity)
  141. {
  142. return await dbSet.Where(p => p.Id.Equals(entity.Id)).ExecuteDeleteAsync();
  143. }
  144. public async Task<int> DeleteAsync(IEnumerable<T> entities)
  145. {
  146. var ids = entities.Select(p => p.Id);
  147. return await DeleteAsync(p => ids.Contains(p.Id));
  148. }
  149. public async Task<int> DeleteAsync(Expression<Func<T, bool>> predicate = null)
  150. {
  151. return await dbSet.Where(predicate).ExecuteDeleteAsync();
  152. }
  153. public async Task<bool> AnyAsync(Expression<Func<T, bool>> predicate = null)
  154. {
  155. return await dbSet.AnyAsync(predicate);
  156. }
  157. public T MaxBy<TResult>(Expression<Func<T, TResult>> selector, Expression<Func<T, bool>> predicate = null)
  158. {
  159. if (predicate == null)
  160. return dbSet.MaxBy(selector);
  161. else
  162. return dbSet.Where(predicate).MaxBy(selector);
  163. }
  164. public async Task<int> UpdatePropAsync<TProperty>(Expression<Func<T, bool>> predicate, Func<T, TProperty> propertyExpression, TProperty value)
  165. {
  166. return await dbSet.Where(predicate)
  167. .ExecuteUpdateAsync(setters => setters.SetProperty(propertyExpression, value));
  168. }
  169. }
  170. }