From c1aa8d240e50d70b6baea441f62e192945435914 Mon Sep 17 00:00:00 2001 From: Oliver Cooper Date: Mon, 25 Jan 2021 21:58:49 +0000 Subject: [PATCH 1/3] Use IBasicRepository.DeleteManyAsync for RepositoryExtensions.HardDeleteAsync --- .../Repositories/RepositoryExtensions.cs | 63 +++++++++++-------- 1 file changed, 36 insertions(+), 27 deletions(-) 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..206204372f 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 @@ -79,32 +79,6 @@ namespace Volo.Abp.Domain.Repositories } } - 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, @@ -138,11 +112,46 @@ namespace Volo.Abp.Domain.Repositories } } + 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 { From 7399f59a9d7909fb7409787df1b81d406e3b3f87 Mon Sep 17 00:00:00 2001 From: Oliver Cooper Date: Thu, 28 Jan 2021 11:41:44 +0000 Subject: [PATCH 2/3] Add RepositoryExtensions.HardDeleteAsync for IEnumerables --- .../Repositories/RepositoryExtensions.cs | 33 +++++++++++++++++++ .../Abp/TestApp/Testing/HardDelete_Tests.cs | 13 ++++++++ 2 files changed, 46 insertions(+) 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 206204372f..264630c117 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 @@ -79,6 +79,39 @@ namespace Volo.Abp.Domain.Repositories } } + public static async Task HardDeleteAsync( + this IBasicRepository repository, + IEnumerable entities, + 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, entities, autoSave, cancellationToken, uowManager.Current); + await uow.CompleteAsync(cancellationToken); + } + } + else + { + await HardDeleteWithUnitOfWorkAsync(repository, entities, autoSave, cancellationToken, uowManager.Current); + } + } + public static async Task HardDeleteAsync( this IBasicRepository repository, TEntity entity, 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() { From 55309996d0c9f7c59c03c91a659905d45cc70671 Mon Sep 17 00:00:00 2001 From: Oliver Cooper Date: Thu, 28 Jan 2021 11:45:25 +0000 Subject: [PATCH 3/3] Reduce repeated code in RepositoryExtensions --- .../Repositories/RepositoryExtensions.cs | 54 +++++++++---------- 1 file changed, 24 insertions(+), 30 deletions(-) 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 264630c117..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) { @@ -87,16 +79,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) { @@ -120,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) { @@ -145,6 +119,26 @@ 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,