@ -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
}
}
p rivate static async Task HardDelete WithUnitOfWork Async< TEntity > (
I Repository< TEntity > repository ,
Expression< Func < TEntity , bool > > predicate ,
bool autoSave ,
CancellationToken cancellationToken , IUnitOfWork currentUow
p ublic static async Task HardDelete Async< TEntity > (
this I Basic Repository< TEntity > repository ,
IEnumerable< TEntity > entities ,
bool autoSave = false ,
CancellationToken cancellationToken = default
)
where TEntity : class , IEntity , ISoftDelete
{
var hardDeleteEntities = ( HashSet < IEntity > ) currentUow . Items . GetOrAdd (
UnitOfWorkItemNames . HardDeletedEntities ,
( ) = > new HashSet < IEntity > ( )
) ;
var uowManager = repository . GetUnitOfWorkManager ( ) ;
List < TEntity > entities ;
using ( currentUow . ServiceProvider . GetRequiredService < IDataFilter < ISoftDelete > > ( ) . 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 < TEntity > (
this IBasicRepository < TEntity > 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 < TEntity > (
IRepository < TEntity > repository ,
Expression < Func < TEntity , bool > > predicate ,
bool autoSave ,
CancellationToken cancellationToken ,
IUnitOfWork currentUow
)
where TEntity : class , IEntity , ISoftDelete
{
using ( currentUow . ServiceProvider . GetRequiredService < IDataFilter < ISoftDelete > > ( ) . 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 < TEntity > (
IBasicRepository < TEntity > repository ,
IEnumerable < TEntity > entities ,
bool autoSave ,
CancellationToken cancellationToken ,
IUnitOfWork currentUow
)
where TEntity : class , IEntity , ISoftDelete
{
var hardDeleteEntities = ( HashSet < IEntity > ) currentUow . Items . GetOrAdd (
UnitOfWorkItemNames . HardDeletedEntities ,
( ) = > new HashSet < IEntity > ( )
) ;
hardDeleteEntities . UnionWith ( entities ) ;
await repository . DeleteManyAsync ( entities , autoSave , cancellationToken ) ;
}
private static async Task HardDeleteWithUnitOfWorkAsync < TEntity > (
IBasicRepository < TEntity > repository ,
TEntity entity ,
bool autoSave ,
CancellationToken cancellationToken , IUnitOfWork currentUow
CancellationToken cancellationToken ,
IUnitOfWork currentUow
)
where TEntity : class , IEntity , ISoftDelete
{