IRepository.cs 3.8 KB

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