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 ef44d6830d..168fe2b0c0 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 @@ -1,8 +1,11 @@ using System; using System.Collections.Generic; +using System.Linq; using System.Linq.Expressions; using System.Threading; using System.Threading.Tasks; +using Microsoft.Extensions.DependencyInjection; +using Volo.Abp.Data; using Volo.Abp.Domain.Entities; using Volo.Abp.DynamicProxy; using Volo.Abp.Uow; @@ -43,6 +46,65 @@ namespace Volo.Abp.Domain.Repositories } } + public static async Task HardDeleteAsync( + this IRepository repository, + Expression> predicate, + bool autoSave = false, + CancellationToken cancellationToken = default + ) + 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!"); + } + + if (uowManager.Current == null) + { + using (var uow = uowManager.Begin()) + { + await HardDeleteWithUnitOfWorkAsync(repository, predicate, autoSave, cancellationToken, uowManager.Current); + await uow.CompleteAsync(cancellationToken); + } + } + else + { + await HardDeleteWithUnitOfWorkAsync(repository, predicate, autoSave, cancellationToken, uowManager.Current); + } + } + + private static async Task HardDeleteWithUnitOfWorkAsync( + IRepository repository, + Expression> predicate, + bool autoSave, + CancellationToken cancellationToken, IUnitOfWork currentUow + ) + where TEntity : class, IEntity, ISoftDelete + { + var hardDeleteEntities = (HashSet) currentUow.Items.GetOrAdd( + UnitOfWorkItemNames.HardDeletedEntities, + () => new HashSet() + ); + + List entities; + using (currentUow.ServiceProvider.GetRequiredService>().Disable()) + { + entities = await repository.AsyncExecuter.ToListAsync((await repository.GetQueryableAsync()).Where(predicate), cancellationToken); + } + + foreach (var entity in entities) + { + hardDeleteEntities.Add(entity); + await repository.DeleteAsync(entity, autoSave, cancellationToken); + } + } + public static async Task HardDeleteAsync( this IBasicRepository repository, TEntity entity, @@ -77,8 +139,8 @@ namespace Volo.Abp.Domain.Repositories } private static async Task HardDeleteWithUnitOfWorkAsync( - IBasicRepository repository, - TEntity entity, + IBasicRepository repository, + TEntity entity, bool autoSave, CancellationToken cancellationToken, IUnitOfWork currentUow ) @@ -90,7 +152,6 @@ namespace Volo.Abp.Domain.Repositories ); hardDeleteEntities.Add(entity); - await repository.DeleteAsync(entity, autoSave, cancellationToken); } } 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 1eac79fd7c..3d9b324da8 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 @@ -30,8 +30,26 @@ namespace Volo.Abp.TestApp.Testing var douglas = await PersonRepository.GetAsync(TestDataBuilder.UserDouglasId); await PersonRepository.HardDeleteAsync(douglas); - douglas = await PersonRepository.FindAsync(TestDataBuilder.UserDouglasId); - douglas.ShouldBeNull(); + using (DataFilter.Disable()) + { + douglas = await PersonRepository.FindAsync(TestDataBuilder.UserDouglasId); + douglas.ShouldBeNull(); + } + } + + [Fact] + public async Task Should_HardDelete_Entities_With_Predicate() + { + await PersonRepository.HardDeleteAsync(x => x.Id == TestDataBuilder.UserDouglasId || x.Id == TestDataBuilder.UserJohnDeletedId); + + using (DataFilter.Disable()) + { + var douglas = await PersonRepository.FindAsync(TestDataBuilder.UserDouglasId); + douglas.ShouldBeNull(); + + var john = await PersonRepository.FindAsync(TestDataBuilder.UserJohnDeletedId); + john.ShouldBeNull(); + } } [Fact] @@ -62,8 +80,44 @@ namespace Volo.Abp.TestApp.Testing await uow.CompleteAsync(); } + using (DataFilter.Disable()) + { + douglas = await PersonRepository.FindAsync(TestDataBuilder.UserDouglasId); + douglas.ShouldBeNull(); + } + } + + [Fact] + public async Task Should_HardDelete_Soft_Deleted_Entities_With_Predicate() + { + var douglas = await PersonRepository.GetAsync(TestDataBuilder.UserDouglasId); + await PersonRepository.DeleteAsync(douglas); + douglas = await PersonRepository.FindAsync(TestDataBuilder.UserDouglasId); douglas.ShouldBeNull(); + + using (DataFilter.Disable()) + { + douglas = await PersonRepository.FindAsync(TestDataBuilder.UserDouglasId); + douglas.ShouldNotBeNull(); + douglas.IsDeleted.ShouldBeTrue(); + douglas.DeletionTime.ShouldNotBeNull(); + } + + using (var uow = UnitOfWorkManager.Begin()) + { + await PersonRepository.HardDeleteAsync(x => x.Id == TestDataBuilder.UserDouglasId || x.Id == TestDataBuilder.UserJohnDeletedId); + await uow.CompleteAsync(); + } + + using (DataFilter.Disable()) + { + douglas = await PersonRepository.FindAsync(TestDataBuilder.UserDouglasId); + douglas.ShouldBeNull(); + + var john = await PersonRepository.FindAsync(TestDataBuilder.UserJohnDeletedId); + john.ShouldBeNull(); + } } } }