IRepository.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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<bool> AnyAsync(Expression<Func<T, bool>> predicate = null);
  22. Task<T> AddOrUpdateAsync(T entity);
  23. Task AddOrUpdateAsync(IEnumerable<T> entitis);
  24. Task<int> DeleteAsync(T entity);
  25. Task<int> DeleteAsync(Expression<Func<T, bool>> predicate = null);
  26. }
  27. public class Repository<T> : IRepository<T> where T : BaseEntity
  28. {
  29. protected readonly OracleContext ctx;
  30. protected readonly DbSet<T> dbSet;
  31. public Repository(OracleContext ctx)
  32. {
  33. this.ctx = ctx;
  34. this.dbSet = ctx.Set<T>();
  35. }
  36. public IQueryable<T> AsQueryable()
  37. {
  38. return dbSet.AsQueryable();
  39. }
  40. public async Task<T> GetByIdAsync(object id)
  41. {
  42. return await dbSet.FindAsync(id);
  43. }
  44. public async Task<IEnumerable<T>> GetAllAsync()
  45. {
  46. return await dbSet.ToListAsync();
  47. }
  48. public async Task<IEnumerable<T>> GetAllAsync<TKey>(Expression<Func<T, TKey>> keySelector = null, bool asc = true)
  49. {
  50. if (keySelector == null)
  51. {
  52. return await dbSet.ToListAsync();
  53. }
  54. else
  55. {
  56. if (asc)
  57. return await dbSet.OrderBy(keySelector).ToListAsync();
  58. else
  59. return await dbSet.OrderByDescending(keySelector).ToListAsync();
  60. }
  61. }
  62. public async Task<IEnumerable<T>> FindAsync(Expression<Func<T, bool>> predicate)
  63. {
  64. return await dbSet.Where(predicate).ToListAsync();
  65. }
  66. public async Task<IEnumerable<T>> FindAsync<TKey>(Expression<Func<T, bool>> predicate, Expression<Func<T, TKey>> keySelector = null, bool asc = true)
  67. {
  68. if (keySelector == null)
  69. {
  70. return await dbSet.Where(predicate).ToListAsync();
  71. }
  72. else
  73. {
  74. if (asc)
  75. return await dbSet.Where(predicate).OrderBy(keySelector).ToListAsync();
  76. else
  77. return await dbSet.Where(predicate).OrderByDescending(keySelector).ToListAsync();
  78. }
  79. }
  80. public async Task<T> FirstOrDefaultAsync(Expression<Func<T, bool>> predicate = null)
  81. {
  82. if (predicate != null)
  83. return await dbSet.Where(predicate).FirstOrDefaultAsync();
  84. return await dbSet.FirstOrDefaultAsync();
  85. }
  86. public async Task<IEnumerable<Res>> GetDistinctAsync<Res>(Expression<Func<T, Res>> selector)
  87. {
  88. var res = await dbSet.Select(selector).Distinct().ToListAsync();
  89. return res;
  90. }
  91. public async Task AddOrUpdateAsync(IEnumerable<T> entity)
  92. {
  93. if (entity.First().Id == 0)
  94. {
  95. await dbSet.AddRangeAsync(entity);
  96. }
  97. else
  98. {
  99. dbSet.UpdateRange(entity);
  100. }
  101. }
  102. public async Task<T> AddOrUpdateAsync(T entity)
  103. {
  104. if (entity.Id==0)
  105. {
  106. var res = await dbSet.AddAsync(entity);
  107. return res.Entity;
  108. }
  109. else
  110. {
  111. var res = dbSet.Update(entity);
  112. return res.Entity;
  113. }
  114. }
  115. public async Task<int> DeleteAsync(T entity)
  116. {
  117. return await dbSet.Where(p => p.Id == entity.Id).ExecuteDeleteAsync();
  118. }
  119. public async Task<int> DeleteAsync(Expression<Func<T, bool>> predicate = null)
  120. {
  121. return await dbSet.Where(predicate).ExecuteDeleteAsync();
  122. }
  123. public async Task<bool> AnyAsync(Expression<Func<T, bool>> predicate = null)
  124. {
  125. return await dbSet.AnyAsync(predicate);
  126. }
  127. }
  128. }