RepositoryExtensions.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Linq.Expressions;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7. using Abp.Collections.Extensions;
  8. using Abp.Dependency;
  9. using Abp.Domain.Entities;
  10. using Abp.Domain.Uow;
  11. using Abp.Reflection;
  12. using Abp.Runtime.Session;
  13. using Abp.Threading;
  14. namespace Abp.Domain.Repositories
  15. {
  16. public static class RepositoryExtensions
  17. {
  18. public static async Task EnsureCollectionLoadedAsync<TEntity, TPrimaryKey, TProperty>(
  19. this IRepository<TEntity, TPrimaryKey> repository,
  20. TEntity entity,
  21. Expression<Func<TEntity, IEnumerable<TProperty>>> collectionExpression,
  22. CancellationToken cancellationToken = default(CancellationToken)
  23. )
  24. where TEntity : class, IEntity<TPrimaryKey>
  25. where TProperty : class
  26. {
  27. var repo = ProxyHelper.UnProxy(repository) as ISupportsExplicitLoading<TEntity, TPrimaryKey>;
  28. if (repo != null)
  29. {
  30. await repo.EnsureCollectionLoadedAsync(entity, collectionExpression, cancellationToken);
  31. }
  32. }
  33. public static void EnsureCollectionLoaded<TEntity, TPrimaryKey, TProperty>(
  34. this IRepository<TEntity, TPrimaryKey> repository,
  35. TEntity entity,
  36. Expression<Func<TEntity, IEnumerable<TProperty>>> collectionExpression
  37. )
  38. where TEntity : class, IEntity<TPrimaryKey>
  39. where TProperty : class
  40. {
  41. AsyncHelper.RunSync(() => repository.EnsureCollectionLoadedAsync(entity, collectionExpression));
  42. }
  43. public static async Task EnsurePropertyLoadedAsync<TEntity, TPrimaryKey, TProperty>(
  44. this IRepository<TEntity, TPrimaryKey> repository,
  45. TEntity entity,
  46. Expression<Func<TEntity, TProperty>> propertyExpression,
  47. CancellationToken cancellationToken = default(CancellationToken)
  48. )
  49. where TEntity : class, IEntity<TPrimaryKey>
  50. where TProperty : class
  51. {
  52. var repo = ProxyHelper.UnProxy(repository) as ISupportsExplicitLoading<TEntity, TPrimaryKey>;
  53. if (repo != null)
  54. {
  55. await repo.EnsurePropertyLoadedAsync(entity, propertyExpression, cancellationToken);
  56. }
  57. }
  58. public static void EnsurePropertyLoaded<TEntity, TPrimaryKey, TProperty>(
  59. this IRepository<TEntity, TPrimaryKey> repository,
  60. TEntity entity,
  61. Expression<Func<TEntity, TProperty>> propertyExpression
  62. )
  63. where TEntity : class, IEntity<TPrimaryKey>
  64. where TProperty : class
  65. {
  66. AsyncHelper.RunSync(() => repository.EnsurePropertyLoadedAsync(entity, propertyExpression));
  67. }
  68. public static IIocResolver GetIocResolver<TEntity, TPrimaryKey>(this IRepository<TEntity, TPrimaryKey> repository)
  69. where TEntity : class, IEntity<TPrimaryKey>
  70. {
  71. var repo = ProxyHelper.UnProxy(repository) as AbpRepositoryBase<TEntity, TPrimaryKey>;
  72. if (repo != null)
  73. {
  74. return repo.IocResolver;
  75. }
  76. throw new ArgumentException($"Given {nameof(repository)} is not inherited from {typeof(AbpRepositoryBase<TEntity, TPrimaryKey>).AssemblyQualifiedName}");
  77. }
  78. public static async Task HardDeleteAsync<TEntity, TPrimaryKey>(this IRepository<TEntity, TPrimaryKey> repository, TEntity entity)
  79. where TEntity : class, IEntity<TPrimaryKey>, ISoftDelete
  80. {
  81. var repo = ProxyHelper.UnProxy(repository) as IRepository<TEntity, TPrimaryKey>;
  82. if (repo == null)
  83. {
  84. throw new ArgumentException($"Given {nameof(repository)} is not inherited from {typeof(IRepository<TEntity, TPrimaryKey>).AssemblyQualifiedName}");
  85. }
  86. var items = ((IUnitOfWorkManagerAccessor)repo).UnitOfWorkManager.Current.Items;
  87. var hardDeleteEntities = items.GetOrAdd(UnitOfWorkExtensionDataTypes.HardDelete, () => new HashSet<string>()) as HashSet<string>;
  88. var tenantId = GetCurrentTenantIdOrNull(repo.GetIocResolver());
  89. var hardDeleteKey = EntityHelper.GetHardDeleteKey(entity, tenantId);
  90. hardDeleteEntities.Add(hardDeleteKey);
  91. await repo.DeleteAsync(entity);
  92. }
  93. public static void HardDelete<TEntity, TPrimaryKey>(this IRepository<TEntity, TPrimaryKey> repository, TEntity entity)
  94. where TEntity : class, IEntity<TPrimaryKey>, ISoftDelete
  95. {
  96. var repo = ProxyHelper.UnProxy(repository) as IRepository<TEntity, TPrimaryKey>;
  97. if (repo == null)
  98. {
  99. throw new ArgumentException($"Given {nameof(repository)} is not inherited from {typeof(IRepository<TEntity, TPrimaryKey>).AssemblyQualifiedName}");
  100. }
  101. var items = ((IUnitOfWorkManagerAccessor)repo).UnitOfWorkManager.Current.Items;
  102. var hardDeleteEntities = items.GetOrAdd(UnitOfWorkExtensionDataTypes.HardDelete, () => new HashSet<string>()) as HashSet<string>;
  103. var tenantId = GetCurrentTenantIdOrNull(repo.GetIocResolver());
  104. var hardDeleteKey = EntityHelper.GetHardDeleteKey(entity, tenantId);
  105. hardDeleteEntities.Add(hardDeleteKey);
  106. repo.Delete(entity);
  107. }
  108. public static async Task HardDeleteAsync<TEntity, TPrimaryKey>(this IRepository<TEntity, TPrimaryKey> repository, Expression<Func<TEntity, bool>> predicate)
  109. where TEntity : class, IEntity<TPrimaryKey>, ISoftDelete
  110. {
  111. foreach (var entity in repository.GetAll().Where(predicate).ToList())
  112. {
  113. await repository.HardDeleteAsync(entity);
  114. }
  115. }
  116. public static void HardDelete<TEntity, TPrimaryKey>(this IRepository<TEntity, TPrimaryKey> repository, Expression<Func<TEntity, bool>> predicate)
  117. where TEntity : class, IEntity<TPrimaryKey>, ISoftDelete
  118. {
  119. foreach (var entity in repository.GetAll().Where(predicate).ToList())
  120. {
  121. repository.HardDelete(entity);
  122. }
  123. }
  124. private static int? GetCurrentTenantIdOrNull(IIocResolver iocResolver)
  125. {
  126. using (var scope = iocResolver.CreateScope())
  127. {
  128. var currentUnitOfWorkProvider = scope.Resolve<ICurrentUnitOfWorkProvider>();
  129. if (currentUnitOfWorkProvider?.Current != null)
  130. {
  131. return currentUnitOfWorkProvider.Current.GetTenantId();
  132. }
  133. }
  134. return iocResolver.Resolve<IAbpSession>().TenantId;
  135. }
  136. }
  137. }