QlbrcTenantCodeTrackedRepository.cs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. using System.Diagnostics;
  2. using Quadarax.Application.QLiberace.Common.Domain;
  3. using Quadarax.Application.QLiberace.Common.Entities.Base;
  4. using Quadarax.Foundation.Core.Data;
  5. using Quadarax.Foundation.Core.Data.Domain;
  6. using Quadarax.Foundation.Core.Data.Extensions;
  7. using Quadarax.Foundation.Core.Data.Interface;
  8. using Quadarax.Foundation.Core.Data.Interface.Domain;
  9. namespace Quadarax.Application.QLiberace.Common.Repositories
  10. {
  11. public abstract class QlbrcTenantCodeTrackedRepository<TEntity> : QlbrcTrackedRepository<TEntity> where TEntity : QlbrcEntityTenantCode, new()
  12. {
  13. protected string? TenantCode => OperationContext.GetContext<QlbrcContext>().TenantCode;
  14. protected QlbrcTenantCodeTrackedRepository(IUnitOfWork<IDataDomain> unitOfWork, QlbrcContext operationContext) : base(unitOfWork, operationContext)
  15. {
  16. }
  17. public override IQueryable<TEntity> Query(IPaging paging,bool usingUncommited=false)
  18. {
  19. var result = base.Query(paging, usingUncommited).Where(x => Equals(x.TenantCode ,TenantCode));
  20. #if DEBUG
  21. Debug.WriteLine($"Repo '{GetType().Name}' Query returns {result.Count()} rows.");
  22. #endif
  23. return result;
  24. }
  25. public override TEntity Get(long id)
  26. {
  27. return Query(new Paging()).MultiInclude(DefaultIncludes.ToArray()).FirstOrDefault(x => Equals(x.Id, id) && Equals(x.TenantCode,TenantCode))!;
  28. }
  29. public override IQueryable<TEntity> Get(IEnumerable<long> ids)
  30. {
  31. return Query(new Paging()).Where(x => ids.Contains(x.Id) && Equals(x.TenantCode,TenantCode));
  32. }
  33. public override TEntity New()
  34. {
  35. var entity = base.New();
  36. entity.TenantCode = TenantCode!;
  37. return entity;
  38. }
  39. }
  40. }