Browse Source

Merge pull request #7478 from abpframework/enisn/bulk-operations/hard-deletion-handle

Framework - Consider hard deleted entities in MongoDbRepository
pull/7487/head
maliming 6 years ago
committed by GitHub
parent
commit
a07b4e34b8
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 57
      framework/src/Volo.Abp.MongoDB/Volo/Abp/Domain/Repositories/MongoDB/MongoDbRepository.cs
  2. 37
      framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Testing/HardDelete_Tests.cs

57
framework/src/Volo.Abp.MongoDB/Volo/Abp/Domain/Repositories/MongoDB/MongoDbRepository.cs

@ -314,16 +314,26 @@ namespace Volo.Abp.Domain.Repositories.MongoDB
} }
public override async Task DeleteManyAsync( public override async Task DeleteManyAsync(
IEnumerable<TEntity> entities, IEnumerable<TEntity> entities,
bool autoSave = false, bool autoSave = false,
CancellationToken cancellationToken = default) CancellationToken cancellationToken = default)
{ {
var entityArray = entities.ToArray(); var softDeletedEntities = new List<TEntity>();
var hardDeletedEntities = new List<TEntity>();
foreach (var entity in entityArray) foreach (var entity in entities)
{ {
await ApplyAbpConceptsForDeletedEntityAsync(entity); await ApplyAbpConceptsForDeletedEntityAsync(entity);
SetNewConcurrencyStamp(entity); SetNewConcurrencyStamp(entity);
if (typeof(ISoftDelete).IsAssignableFrom(typeof(TEntity)) && !IsHardDeleted(entity))
{
softDeletedEntities.Add(entity);
}
else
{
hardDeletedEntities.Add(entity);
}
} }
var dbContext = await GetDbContextAsync(GetCancellationToken(cancellationToken)); var dbContext = await GetDbContextAsync(GetCancellationToken(cancellationToken));
@ -331,54 +341,57 @@ namespace Volo.Abp.Domain.Repositories.MongoDB
if (BulkOperationProvider != null) if (BulkOperationProvider != null)
{ {
await BulkOperationProvider.DeleteManyAsync(this, entityArray, dbContext.SessionHandle, autoSave, cancellationToken); await BulkOperationProvider.DeleteManyAsync(this, entities.ToArray(), dbContext.SessionHandle, autoSave, cancellationToken);
return; return;
} }
var entitiesCount = entityArray.Count(); if (softDeletedEntities.Count > 0)
if (typeof(ISoftDelete).IsAssignableFrom(typeof(TEntity)))
{ {
UpdateResult updateResult; UpdateResult updateResult;
var softDeleteEntitiesCount = softDeletedEntities.Count;
if (dbContext.SessionHandle != null) if (dbContext.SessionHandle != null)
{ {
updateResult = await collection.UpdateManyAsync( updateResult = await collection.UpdateManyAsync(
dbContext.SessionHandle, dbContext.SessionHandle,
CreateEntitiesFilter(entityArray), CreateEntitiesFilter(softDeletedEntities),
Builders<TEntity>.Update.Set(x => ((ISoftDelete)x).IsDeleted, true), Builders<TEntity>.Update.Set(x => ((ISoftDelete)x).IsDeleted, true)
cancellationToken: cancellationToken); );
} }
else else
{ {
updateResult = await collection.UpdateManyAsync( updateResult = await collection.UpdateManyAsync(
CreateEntitiesFilter(entityArray), CreateEntitiesFilter(softDeletedEntities),
Builders<TEntity>.Update.Set(x => ((ISoftDelete)x).IsDeleted, true), Builders<TEntity>.Update.Set(x => ((ISoftDelete)x).IsDeleted, true)
cancellationToken: cancellationToken); );
} }
if (updateResult.MatchedCount < entitiesCount) if (updateResult.MatchedCount < softDeleteEntitiesCount)
{ {
ThrowOptimisticConcurrencyException(); ThrowOptimisticConcurrencyException();
} }
} }
else
if (hardDeletedEntities.Count > 0)
{ {
DeleteResult deleteResult; DeleteResult deleteResult;
var hardDeletedEntitiesCount = hardDeletedEntities.Count;
if (dbContext.SessionHandle != null) if (dbContext.SessionHandle != null)
{ {
deleteResult = await collection.DeleteManyAsync( deleteResult = await collection.DeleteManyAsync(
dbContext.SessionHandle, dbContext.SessionHandle,
CreateEntitiesFilter(entityArray), CreateEntitiesFilter(hardDeletedEntities)
cancellationToken: cancellationToken); );
} }
else else
{ {
deleteResult = await collection.DeleteManyAsync( deleteResult = await collection.DeleteManyAsync(
CreateEntitiesFilter(entityArray), CreateEntitiesFilter(hardDeletedEntities)
cancellationToken); );
} }
if (deleteResult.DeletedCount < entitiesCount) if (deleteResult.DeletedCount < hardDeletedEntitiesCount)
{ {
ThrowOptimisticConcurrencyException(); ThrowOptimisticConcurrencyException();
} }

37
framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Testing/HardDelete_Tests.cs

@ -1,7 +1,9 @@
using Shouldly; using Shouldly;
using System; using System;
using System.Collections.Generic;
using System.Threading.Tasks; using System.Threading.Tasks;
using Volo.Abp.Data; using Volo.Abp.Data;
using Volo.Abp.Domain.Entities;
using Volo.Abp.Domain.Repositories; using Volo.Abp.Domain.Repositories;
using Volo.Abp.Modularity; using Volo.Abp.Modularity;
using Volo.Abp.TestApp.Domain; using Volo.Abp.TestApp.Domain;
@ -119,5 +121,40 @@ namespace Volo.Abp.TestApp.Testing
john.ShouldBeNull(); john.ShouldBeNull();
} }
} }
[Fact]
public async Task Should_HardDelete_WithDeleteMany()
{
var persons = await PersonRepository.GetListAsync();
await WithUnitOfWorkAsync(async () =>
{
var hardDeleteEntities = (HashSet<IEntity>)UnitOfWorkManager.Current.Items.GetOrAdd(
UnitOfWorkItemNames.HardDeletedEntities,
() => new HashSet<IEntity>()
);
hardDeleteEntities.UnionWith(persons);
await PersonRepository.DeleteManyAsync(persons);
});
var personsCount = await PersonRepository.GetCountAsync();
personsCount.ShouldBe(0);
}
[Fact]
public async Task Should_HardDelete_WithDeleteMany_WithPredicate()
{
await WithUnitOfWorkAsync(async () =>
{
await PersonRepository.HardDeleteAsync(x => x.Id == TestDataBuilder.UserDouglasId);
await PersonRepository.DeleteManyAsync(new[] { TestDataBuilder.UserDouglasId });
});
var douglas = await PersonRepository.FindAsync(TestDataBuilder.UserDouglasId);
douglas.ShouldBeNull();
}
} }
} }

Loading…
Cancel
Save