IRepository.cs 6.1 KB

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