IRepository.cs 5.3 KB

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