diff --git a/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Repositories/RepositoryExtensions.cs b/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Repositories/RepositoryExtensions.cs index 168fe2b0c0..7a4a21f84f 100644 --- a/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Repositories/RepositoryExtensions.cs +++ b/framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Repositories/RepositoryExtensions.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; +using System.Runtime.CompilerServices; using System.Threading; using System.Threading.Tasks; using Microsoft.Extensions.DependencyInjection; @@ -54,16 +55,7 @@ namespace Volo.Abp.Domain.Repositories ) where TEntity : class, IEntity, ISoftDelete { - if (!(ProxyHelper.UnProxy(repository) is IUnitOfWorkManagerAccessor unitOfWorkManagerAccessor)) - { - throw new AbpException($"The given repository (of type {repository.GetType().AssemblyQualifiedName}) should implement the {typeof(IUnitOfWorkManagerAccessor).AssemblyQualifiedName} interface in order to invoke the {nameof(HardDeleteAsync)} method!"); - } - - var uowManager = unitOfWorkManagerAccessor.UnitOfWorkManager; - if (uowManager == null) - { - throw new AbpException($"{nameof(unitOfWorkManagerAccessor.UnitOfWorkManager)} property of the given {nameof(repository)} object is null!"); - } + var uowManager = repository.GetUnitOfWorkManager(); if (uowManager.Current == null) { @@ -79,29 +71,27 @@ namespace Volo.Abp.Domain.Repositories } } - private static async Task HardDeleteWithUnitOfWorkAsync( - IRepository repository, - Expression> predicate, - bool autoSave, - CancellationToken cancellationToken, IUnitOfWork currentUow + public static async Task HardDeleteAsync( + this IBasicRepository repository, + IEnumerable entities, + bool autoSave = false, + CancellationToken cancellationToken = default ) where TEntity : class, IEntity, ISoftDelete { - var hardDeleteEntities = (HashSet) currentUow.Items.GetOrAdd( - UnitOfWorkItemNames.HardDeletedEntities, - () => new HashSet() - ); + var uowManager = repository.GetUnitOfWorkManager(); - List entities; - using (currentUow.ServiceProvider.GetRequiredService>().Disable()) + if (uowManager.Current == null) { - entities = await repository.AsyncExecuter.ToListAsync((await repository.GetQueryableAsync()).Where(predicate), cancellationToken); + using (var uow = uowManager.Begin()) + { + await HardDeleteWithUnitOfWorkAsync(repository, entities, autoSave, cancellationToken, uowManager.Current); + await uow.CompleteAsync(cancellationToken); + } } - - foreach (var entity in entities) + else { - hardDeleteEntities.Add(entity); - await repository.DeleteAsync(entity, autoSave, cancellationToken); + await HardDeleteWithUnitOfWorkAsync(repository, entities, autoSave, cancellationToken, uowManager.Current); } } @@ -113,16 +103,7 @@ namespace Volo.Abp.Domain.Repositories ) where TEntity : class, IEntity, ISoftDelete { - if (!(ProxyHelper.UnProxy(repository) is IUnitOfWorkManagerAccessor unitOfWorkManagerAccessor)) - { - throw new AbpException($"The given repository (of type {repository.GetType().AssemblyQualifiedName}) should implement the {typeof(IUnitOfWorkManagerAccessor).AssemblyQualifiedName} interface in order to invoke the {nameof(HardDeleteAsync)} method!"); - } - - var uowManager = unitOfWorkManagerAccessor.UnitOfWorkManager; - if (uowManager == null) - { - throw new AbpException($"{nameof(unitOfWorkManagerAccessor.UnitOfWorkManager)} property of the given {nameof(repository)} object is null!"); - } + var uowManager = repository.GetUnitOfWorkManager(); if (uowManager.Current == null) { @@ -138,11 +119,66 @@ namespace Volo.Abp.Domain.Repositories } } + private static IUnitOfWorkManager GetUnitOfWorkManager( + this IBasicRepository repository, + [CallerMemberName] string callingMethodName = nameof(GetUnitOfWorkManager) + ) + where TEntity : class, IEntity + { + if (ProxyHelper.UnProxy(repository) is not IUnitOfWorkManagerAccessor unitOfWorkManagerAccessor) + { + throw new AbpException($"The given repository (of type {repository.GetType().AssemblyQualifiedName}) should implement the " + + $"{typeof(IUnitOfWorkManagerAccessor).AssemblyQualifiedName} interface in order to invoke the {callingMethodName} method!"); + } + + if (unitOfWorkManagerAccessor.UnitOfWorkManager == null) + { + throw new AbpException($"{nameof(unitOfWorkManagerAccessor.UnitOfWorkManager)} property of the given {nameof(repository)} object is null!"); + } + + return unitOfWorkManagerAccessor.UnitOfWorkManager; + } + + private static async Task HardDeleteWithUnitOfWorkAsync( + IRepository repository, + Expression> predicate, + bool autoSave, + CancellationToken cancellationToken, + IUnitOfWork currentUow + ) + where TEntity : class, IEntity, ISoftDelete + { + using (currentUow.ServiceProvider.GetRequiredService>().Disable()) + { + var entities = await repository.AsyncExecuter.ToListAsync((await repository.GetQueryableAsync()).Where(predicate), cancellationToken); + await HardDeleteWithUnitOfWorkAsync(repository, entities, autoSave, cancellationToken, currentUow); + } + } + + private static async Task HardDeleteWithUnitOfWorkAsync( + IBasicRepository repository, + IEnumerable entities, + bool autoSave, + CancellationToken cancellationToken, + IUnitOfWork currentUow + ) + where TEntity : class, IEntity, ISoftDelete + { + var hardDeleteEntities = (HashSet)currentUow.Items.GetOrAdd( + UnitOfWorkItemNames.HardDeletedEntities, + () => new HashSet() + ); + + hardDeleteEntities.UnionWith(entities); + await repository.DeleteManyAsync(entities, autoSave, cancellationToken); + } + private static async Task HardDeleteWithUnitOfWorkAsync( IBasicRepository repository, TEntity entity, bool autoSave, - CancellationToken cancellationToken, IUnitOfWork currentUow + CancellationToken cancellationToken, + IUnitOfWork currentUow ) where TEntity : class, IEntity, ISoftDelete { diff --git a/framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Testing/HardDelete_Tests.cs b/framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Testing/HardDelete_Tests.cs index e31b64f6e4..a2085a9a67 100644 --- a/framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Testing/HardDelete_Tests.cs +++ b/framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Testing/HardDelete_Tests.cs @@ -54,6 +54,19 @@ namespace Volo.Abp.TestApp.Testing } } + [Fact] + public async Task Should_HardDelete_Entities_With_IEnumerable() + { + using (DataFilter.Disable()) + { + var persons = await PersonRepository.GetListAsync(); + await PersonRepository.HardDeleteAsync(persons); + + var personCount = await PersonRepository.GetCountAsync(); + personCount.ShouldBe(0); + } + } + [Fact] public async Task Should_HardDelete_Soft_Deleted_Entities() {