IRepository.cs 3.6 KB

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