IRepository.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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>
  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<bool> AnyAsync(Expression<Func<T, bool>> predicate = null);
  22. Task<T> AddAsync(T entity);
  23. Task AddRangeAsync(IEnumerable<T> entitis);
  24. void Update(T entity);
  25. void Delete(T entity);
  26. Task<int> DeleteAsync(Expression<Func<T, bool>> predicate = null);
  27. }
  28. public class Repository<T> : IRepository<T> where T : class
  29. {
  30. protected readonly OracleContext ctx;
  31. protected readonly DbSet<T> dbSet;
  32. public Repository(OracleContext ctx)
  33. {
  34. this.ctx = ctx;
  35. this.dbSet = ctx.Set<T>();
  36. }
  37. public IQueryable<T> AsQueryable()
  38. {
  39. return dbSet.AsQueryable();
  40. }
  41. public async Task<T> GetByIdAsync(object id)
  42. {
  43. return await dbSet.FindAsync(id);
  44. }
  45. public async Task<IEnumerable<T>> GetAllAsync()
  46. {
  47. return await dbSet.ToListAsync();
  48. }
  49. public async Task<IEnumerable<T>> GetAllAsync<TKey>(Expression<Func<T, TKey>> keySelector = null, bool asc = true)
  50. {
  51. if (keySelector == null)
  52. {
  53. return await dbSet.ToListAsync();
  54. }
  55. else
  56. {
  57. if (asc)
  58. return await dbSet.OrderBy(keySelector).ToListAsync();
  59. else
  60. return await dbSet.OrderByDescending(keySelector).ToListAsync();
  61. }
  62. }
  63. public async Task<IEnumerable<T>> FindAsync(Expression<Func<T, bool>> predicate)
  64. {
  65. return await dbSet.Where(predicate).ToListAsync();
  66. }
  67. public async Task<IEnumerable<T>> FindAsync<TKey>(Expression<Func<T, bool>> predicate,Expression<Func<T, TKey>> keySelector = null, bool asc = true)
  68. {
  69. if (keySelector == null)
  70. {
  71. return await dbSet.Where(predicate).ToListAsync();
  72. }
  73. else
  74. {
  75. if (asc)
  76. return await dbSet.Where(predicate).OrderBy(keySelector).ToListAsync();
  77. else
  78. return await dbSet.Where(predicate).OrderByDescending(keySelector).ToListAsync();
  79. }
  80. }
  81. public async Task<T> FirstOrDefaultAsync(Expression<Func<T, bool>> predicate = null)
  82. {
  83. if (predicate != null)
  84. return await dbSet.Where(predicate).FirstOrDefaultAsync();
  85. return await dbSet.FirstOrDefaultAsync();
  86. }
  87. public async Task<IEnumerable<Res>> GetDistinctAsync<Res>(Expression<Func<T, Res>> selector)
  88. {
  89. var res = await dbSet.Select(selector).Distinct().ToListAsync();
  90. return res;
  91. }
  92. public async Task<T> AddAsync(T entity)
  93. {
  94. var res = await dbSet.AddAsync(entity);
  95. return res.Entity;
  96. }
  97. public async Task AddRangeAsync(IEnumerable<T> entitis)
  98. {
  99. await dbSet.AddRangeAsync(entitis);
  100. }
  101. public void Update(T entity)
  102. {
  103. dbSet.Update(entity);
  104. }
  105. public void Delete(T entity)
  106. {
  107. dbSet.Remove(entity);
  108. }
  109. ///// <summary>
  110. ///// 高性能的删除
  111. ///// </summary>
  112. ///// <param name="id">ids可以是int、long、IEnumerable<int>、IEnumerable<long>等类型</param>
  113. //public async void DeleteById(object id)
  114. //{
  115. // if (id == null) return;
  116. // string table = typeof(T).Name;
  117. // if (id is IEnumerable<long> ids1)
  118. // {
  119. // //如果ID是连续的,这里可以使用大于小于等条件进一步优化
  120. // string sql = $"delete from {table} where id in ({string.Join(",", ids1)})";
  121. // await ctx.ExecuteSqlAsync(sql);
  122. // }
  123. // else if (id is IEnumerable<int> ids2)
  124. // {
  125. // string sql = $"delete from {table} where id in ({string.Join(",", ids2)})";
  126. // await ctx.ExecuteSqlAsync(sql);
  127. // }
  128. // else
  129. // {
  130. // string sql = $"delete from {table} where id={id}";
  131. // await ctx.ExecuteSqlAsync(sql);
  132. // }
  133. //}
  134. public async Task<int> DeleteAsync(Expression<Func<T, bool>> predicate = null)
  135. {
  136. return await dbSet.Where(predicate).ExecuteDeleteAsync();
  137. }
  138. public async Task<bool> AnyAsync(Expression<Func<T, bool>> predicate = null)
  139. {
  140. return await dbSet.AnyAsync(predicate);
  141. }
  142. }
  143. }