VEfRepositoryBase.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. using Microsoft.EntityFrameworkCore;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Linq.Expressions;
  6. using VCommon.VEntity;
  7. namespace VCommon.VEntityFrameworkCore
  8. {
  9. public abstract class VEfRepositoryBase<TDbContext, TEntity> : IDisposable where TEntity : VEntityBase where TDbContext : VEfDbContextBase
  10. {
  11. private TDbContext _dbContext;
  12. public TDbContext DbContext
  13. {
  14. get => _dbContext ??= ResolveDbContextInstance();
  15. set => _dbContext = value;
  16. }
  17. private readonly Dictionary<string, bool> _filterState = new()
  18. {
  19. { VEfDataFilters.SoftDelete, true },
  20. { VEfDataFilters.Passive, true },
  21. };
  22. protected virtual IQueryable<TEntity> ProcessFilters(IQueryable<TEntity> input)
  23. {
  24. if (_filterState[VEfDataFilters.SoftDelete] && typeof(TEntity).IsAssignableTo(typeof(ISoftDelete)))
  25. {
  26. input = input.Where(p => ((ISoftDelete)p).IsAbolish == false);
  27. }
  28. if (_filterState[VEfDataFilters.Passive] && typeof(TEntity).IsAssignableTo(typeof(IPassive)))
  29. {
  30. input = input.Where(p => ((IPassive)p).IsEnable == false);
  31. }
  32. return input;
  33. }
  34. public abstract VEfRepositoryBase<TDbContext, TForkEntity> ForkRepository<TForkEntity>() where TForkEntity : VEntityBase;
  35. protected abstract TDbContext ResolveDbContextInstance();
  36. protected DbSet<TEntity> DbSet => DbContext.Set<TEntity>();
  37. public virtual void DisableFilter(string filterName) => _filterState[filterName] = false;
  38. public virtual void EnableFilter(string filterName) => _filterState[filterName] = true;
  39. public virtual bool IsFilterEnabled(string filterName) => _filterState[filterName];
  40. public virtual IQueryable<TEntity> QueryNoTracking(Expression<Func<TEntity, bool>> queryExpression = null)
  41. {
  42. var source = ProcessFilters(DbSet.AsNoTracking());
  43. return queryExpression != null
  44. ? source.Where(queryExpression)
  45. : source;
  46. }
  47. public virtual IQueryable<TEntity> Query(Expression<Func<TEntity, bool>> queryExpression = null)
  48. {
  49. var source = ProcessFilters(DbSet);
  50. return null == queryExpression ? source : source.Where(queryExpression);
  51. }
  52. public virtual void Delete(TEntity entity) => DbSet.Remove(entity);
  53. public virtual TEntity Add(TEntity entity) => DbSet.Add(entity).Entity;
  54. public bool CheckExist(Expression<Func<TEntity, bool>> expression = null)
  55. {
  56. var source = ProcessFilters(DbSet);
  57. return null == expression ? source.Any() : source.Any(expression);
  58. }
  59. public TEntity GetEntityOrDefault(Guid id) => ProcessFilters(DbSet).FirstOrDefault(p => p.Id == id);
  60. public TEntity GetEntityOrDefault(Expression<Func<TEntity, bool>> expression = null)
  61. {
  62. var source = ProcessFilters(DbSet);
  63. return null == expression ? source.FirstOrDefault() : source.FirstOrDefault(expression);
  64. }
  65. public int SaveChanges() => DbContext.SaveChanges();
  66. public void Dispose() => DbContext?.Dispose();
  67. }
  68. }