Browse Source

Merge pull request #7543 from olicooper/pr/repository-delete-updates

pull/7574/head
maliming 6 years ago
committed by GitHub
parent
commit
8cfc4d0762
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 17
      framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Repositories/IRepository.cs
  2. 5
      framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/Domain/Repositories/EntityFrameworkCore/EfCoreRepository.cs
  3. 5
      framework/src/Volo.Abp.MemoryDb/Volo/Abp/Domain/Repositories/MemoryDb/MemoryDbRepository.cs
  4. 114
      framework/src/Volo.Abp.MongoDB/Volo/Abp/Domain/Repositories/MongoDB/MongoDbRepository.cs
  5. 17
      framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Testing/SoftDelete_Tests.cs

17
framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Repositories/IRepository.cs

@ -20,8 +20,10 @@ namespace Volo.Abp.Domain.Repositories
{ {
/// <summary> /// <summary>
/// Get a single entity by the given <paramref name="predicate"/>. /// Get a single entity by the given <paramref name="predicate"/>.
/// It returns null if no entity with the given <paramref name="predicate"/>. /// <para>
/// It returns null if there is no entity with the given <paramref name="predicate"/>.
/// It throws <see cref="InvalidOperationException"/> if there are multiple entities with the given <paramref name="predicate"/>. /// It throws <see cref="InvalidOperationException"/> if there are multiple entities with the given <paramref name="predicate"/>.
/// </para>
/// </summary> /// </summary>
/// <param name="predicate">A condition to find the entity</param> /// <param name="predicate">A condition to find the entity</param>
/// <param name="includeDetails">Set true to include all children of this entity</param> /// <param name="includeDetails">Set true to include all children of this entity</param>
@ -34,8 +36,10 @@ namespace Volo.Abp.Domain.Repositories
/// <summary> /// <summary>
/// Get a single entity by the given <paramref name="predicate"/>. /// Get a single entity by the given <paramref name="predicate"/>.
/// <para>
/// It throws <see cref="EntityNotFoundException"/> if there is no entity with the given <paramref name="predicate"/>. /// It throws <see cref="EntityNotFoundException"/> if there is no entity with the given <paramref name="predicate"/>.
/// It throws <see cref="InvalidOperationException"/> if there are multiple entities with the given <paramref name="predicate"/>. /// It throws <see cref="InvalidOperationException"/> if there are multiple entities with the given <paramref name="predicate"/>.
/// </para>
/// </summary> /// </summary>
/// <param name="predicate">A condition to filter entities</param> /// <param name="predicate">A condition to filter entities</param>
/// <param name="includeDetails">Set true to include all children of this entity</param> /// <param name="includeDetails">Set true to include all children of this entity</param>
@ -47,10 +51,11 @@ namespace Volo.Abp.Domain.Repositories
); );
/// <summary> /// <summary>
/// Deletes many entities by function. /// Deletes many entities by the given <paramref name="predicate"/>.
/// Notice that: All entities fits to given predicate are retrieved and deleted. /// <para>
/// This may cause major performance problems if there are too many entities with /// Please note: This may cause major performance problems if there are too many entities returned for a
/// given predicate. /// given predicate and the database provider doesn't have a way to efficiently delete many entities.
/// </para>
/// </summary> /// </summary>
/// <param name="predicate">A condition to filter entities</param> /// <param name="predicate">A condition to filter entities</param>
/// <param name="autoSave"> /// <param name="autoSave">
@ -69,4 +74,4 @@ namespace Volo.Abp.Domain.Repositories
where TEntity : class, IEntity<TKey> where TEntity : class, IEntity<TKey>
{ {
} }
} }

5
framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/Domain/Repositories/EntityFrameworkCore/EfCoreRepository.cs

@ -289,10 +289,7 @@ namespace Volo.Abp.Domain.Repositories.EntityFrameworkCore
.Where(predicate) .Where(predicate)
.ToListAsync(GetCancellationToken(cancellationToken)); .ToListAsync(GetCancellationToken(cancellationToken));
foreach (var entity in entities) await DeleteManyAsync(entities, autoSave, cancellationToken);
{
dbSet.Remove(entity);
}
if (autoSave) if (autoSave)
{ {

5
framework/src/Volo.Abp.MemoryDb/Volo/Abp/Domain/Repositories/MemoryDb/MemoryDbRepository.cs

@ -191,10 +191,7 @@ namespace Volo.Abp.Domain.Repositories.MemoryDb
{ {
var entities = (await GetQueryableAsync()).Where(predicate).ToList(); var entities = (await GetQueryableAsync()).Where(predicate).ToList();
foreach (var entity in entities) await DeleteManyAsync(entities, autoSave, cancellationToken);
{
await DeleteAsync(entity, autoSave, cancellationToken);
}
} }
public override async Task<TEntity> InsertAsync( public override async Task<TEntity> InsertAsync(

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

@ -71,16 +71,18 @@ namespace Volo.Abp.Domain.Repositories.MongoDB
protected Task<TMongoDbContext> GetDbContextAsync(CancellationToken cancellationToken = default) protected Task<TMongoDbContext> GetDbContextAsync(CancellationToken cancellationToken = default)
{ {
cancellationToken = GetCancellationToken(cancellationToken);
// Multi-tenancy unaware entities should always use the host connection string // Multi-tenancy unaware entities should always use the host connection string
if (!EntityHelper.IsMultiTenant<TEntity>()) if (!EntityHelper.IsMultiTenant<TEntity>())
{ {
using (CurrentTenant.Change(null)) using (CurrentTenant.Change(null))
{ {
return DbContextProvider.GetDbContextAsync(GetCancellationToken(cancellationToken)); return DbContextProvider.GetDbContextAsync(cancellationToken);
} }
} }
return DbContextProvider.GetDbContextAsync(GetCancellationToken(cancellationToken)); return DbContextProvider.GetDbContextAsync(cancellationToken);
} }
protected IMongoDbContextProvider<TMongoDbContext> DbContextProvider { get; } protected IMongoDbContextProvider<TMongoDbContext> DbContextProvider { get; }
@ -107,9 +109,11 @@ namespace Volo.Abp.Domain.Repositories.MongoDB
bool autoSave = false, bool autoSave = false,
CancellationToken cancellationToken = default) CancellationToken cancellationToken = default)
{ {
cancellationToken = GetCancellationToken(cancellationToken);
await ApplyAbpConceptsForAddedEntityAsync(entity); await ApplyAbpConceptsForAddedEntityAsync(entity);
var dbContext = await GetDbContextAsync(GetCancellationToken(cancellationToken)); var dbContext = await GetDbContextAsync(cancellationToken);
var collection = dbContext.Collection<TEntity>(); var collection = dbContext.Collection<TEntity>();
if (dbContext.SessionHandle != null) if (dbContext.SessionHandle != null)
@ -117,14 +121,14 @@ namespace Volo.Abp.Domain.Repositories.MongoDB
await collection.InsertOneAsync( await collection.InsertOneAsync(
dbContext.SessionHandle, dbContext.SessionHandle,
entity, entity,
cancellationToken: GetCancellationToken(cancellationToken) cancellationToken: cancellationToken
); );
} }
else else
{ {
await collection.InsertOneAsync( await collection.InsertOneAsync(
entity, entity,
cancellationToken: GetCancellationToken(cancellationToken) cancellationToken: cancellationToken
); );
} }
@ -133,6 +137,8 @@ namespace Volo.Abp.Domain.Repositories.MongoDB
public override async Task InsertManyAsync(IEnumerable<TEntity> entities, bool autoSave = false, CancellationToken cancellationToken = default) public override async Task InsertManyAsync(IEnumerable<TEntity> entities, bool autoSave = false, CancellationToken cancellationToken = default)
{ {
cancellationToken = GetCancellationToken(cancellationToken);
var entityArray = entities.ToArray(); var entityArray = entities.ToArray();
foreach (var entity in entityArray) foreach (var entity in entityArray)
@ -140,7 +146,7 @@ namespace Volo.Abp.Domain.Repositories.MongoDB
await ApplyAbpConceptsForAddedEntityAsync(entity); await ApplyAbpConceptsForAddedEntityAsync(entity);
} }
var dbContext = await GetDbContextAsync(GetCancellationToken(cancellationToken)); var dbContext = await GetDbContextAsync(cancellationToken);
var collection = dbContext.Collection<TEntity>(); var collection = dbContext.Collection<TEntity>();
if (BulkOperationProvider != null) if (BulkOperationProvider != null)
@ -169,6 +175,8 @@ namespace Volo.Abp.Domain.Repositories.MongoDB
bool autoSave = false, bool autoSave = false,
CancellationToken cancellationToken = default) CancellationToken cancellationToken = default)
{ {
cancellationToken = GetCancellationToken(cancellationToken);
SetModificationAuditProperties(entity); SetModificationAuditProperties(entity);
if (entity is ISoftDelete softDeleteEntity && softDeleteEntity.IsDeleted) if (entity is ISoftDelete softDeleteEntity && softDeleteEntity.IsDeleted)
@ -186,7 +194,7 @@ namespace Volo.Abp.Domain.Repositories.MongoDB
var oldConcurrencyStamp = SetNewConcurrencyStamp(entity); var oldConcurrencyStamp = SetNewConcurrencyStamp(entity);
ReplaceOneResult result; ReplaceOneResult result;
var dbContext = await GetDbContextAsync(GetCancellationToken(cancellationToken)); var dbContext = await GetDbContextAsync(cancellationToken);
var collection = dbContext.Collection<TEntity>(); var collection = dbContext.Collection<TEntity>();
if (dbContext.SessionHandle != null) if (dbContext.SessionHandle != null)
@ -195,7 +203,7 @@ namespace Volo.Abp.Domain.Repositories.MongoDB
dbContext.SessionHandle, dbContext.SessionHandle,
CreateEntityFilter(entity, true, oldConcurrencyStamp), CreateEntityFilter(entity, true, oldConcurrencyStamp),
entity, entity,
cancellationToken: GetCancellationToken(cancellationToken) cancellationToken: cancellationToken
); );
} }
else else
@ -203,7 +211,7 @@ namespace Volo.Abp.Domain.Repositories.MongoDB
result = await collection.ReplaceOneAsync( result = await collection.ReplaceOneAsync(
CreateEntityFilter(entity, true, oldConcurrencyStamp), CreateEntityFilter(entity, true, oldConcurrencyStamp),
entity, entity,
cancellationToken: GetCancellationToken(cancellationToken) cancellationToken: cancellationToken
); );
} }
@ -277,15 +285,18 @@ namespace Volo.Abp.Domain.Repositories.MongoDB
bool autoSave = false, bool autoSave = false,
CancellationToken cancellationToken = default) CancellationToken cancellationToken = default)
{ {
await ApplyAbpConceptsForDeletedEntityAsync(entity); cancellationToken = GetCancellationToken(cancellationToken);
var oldConcurrencyStamp = SetNewConcurrencyStamp(entity);
var dbContext = await GetDbContextAsync(GetCancellationToken(cancellationToken)); var dbContext = await GetDbContextAsync(cancellationToken);
var collection = dbContext.Collection<TEntity>(); var collection = dbContext.Collection<TEntity>();
if (entity is ISoftDelete softDeleteEntity && !IsHardDeleted(entity)) var oldConcurrencyStamp = SetNewConcurrencyStamp(entity);
if (typeof(ISoftDelete).IsAssignableFrom(typeof(TEntity)) && !IsHardDeleted(entity))
{ {
softDeleteEntity.IsDeleted = true; ((ISoftDelete)entity).IsDeleted = true;
await ApplyAbpConceptsForDeletedEntityAsync(entity);
ReplaceOneResult result; ReplaceOneResult result;
if (dbContext.SessionHandle != null) if (dbContext.SessionHandle != null)
@ -294,7 +305,7 @@ namespace Volo.Abp.Domain.Repositories.MongoDB
dbContext.SessionHandle, dbContext.SessionHandle,
CreateEntityFilter(entity, true, oldConcurrencyStamp), CreateEntityFilter(entity, true, oldConcurrencyStamp),
entity, entity,
cancellationToken: GetCancellationToken(cancellationToken) cancellationToken: cancellationToken
); );
} }
else else
@ -302,7 +313,7 @@ namespace Volo.Abp.Domain.Repositories.MongoDB
result = await collection.ReplaceOneAsync( result = await collection.ReplaceOneAsync(
CreateEntityFilter(entity, true, oldConcurrencyStamp), CreateEntityFilter(entity, true, oldConcurrencyStamp),
entity, entity,
cancellationToken: GetCancellationToken(cancellationToken) cancellationToken: cancellationToken
); );
} }
@ -313,6 +324,8 @@ namespace Volo.Abp.Domain.Repositories.MongoDB
} }
else else
{ {
await ApplyAbpConceptsForDeletedEntityAsync(entity);
DeleteResult result; DeleteResult result;
if (dbContext.SessionHandle != null) if (dbContext.SessionHandle != null)
@ -320,14 +333,14 @@ namespace Volo.Abp.Domain.Repositories.MongoDB
result = await collection.DeleteOneAsync( result = await collection.DeleteOneAsync(
dbContext.SessionHandle, dbContext.SessionHandle,
CreateEntityFilter(entity, true, oldConcurrencyStamp), CreateEntityFilter(entity, true, oldConcurrencyStamp),
cancellationToken: GetCancellationToken(cancellationToken) cancellationToken: cancellationToken
); );
} }
else else
{ {
result = await collection.DeleteOneAsync( result = await collection.DeleteOneAsync(
CreateEntityFilter(entity, true, oldConcurrencyStamp), CreateEntityFilter(entity, true, oldConcurrencyStamp),
GetCancellationToken(cancellationToken) cancellationToken
); );
} }
@ -343,55 +356,55 @@ namespace Volo.Abp.Domain.Repositories.MongoDB
bool autoSave = false, bool autoSave = false,
CancellationToken cancellationToken = default) CancellationToken cancellationToken = default)
{ {
var softDeletedEntities = new List<TEntity>(); cancellationToken = GetCancellationToken(cancellationToken);
var softDeletedEntities = new Dictionary<TEntity, string>();
var hardDeletedEntities = new List<TEntity>(); var hardDeletedEntities = new List<TEntity>();
foreach (var entity in entities) foreach (var entity in entities)
{ {
await ApplyAbpConceptsForDeletedEntityAsync(entity);
SetNewConcurrencyStamp(entity);
if (typeof(ISoftDelete).IsAssignableFrom(typeof(TEntity)) && !IsHardDeleted(entity)) if (typeof(ISoftDelete).IsAssignableFrom(typeof(TEntity)) && !IsHardDeleted(entity))
{ {
softDeletedEntities.Add(entity); ((ISoftDelete)entity).IsDeleted = true;
softDeletedEntities.Add(entity, SetNewConcurrencyStamp(entity));
} }
else else
{ {
hardDeletedEntities.Add(entity); hardDeletedEntities.Add(entity);
} }
await ApplyAbpConceptsForDeletedEntityAsync(entity);
} }
var dbContext = await GetDbContextAsync(GetCancellationToken(cancellationToken)); var dbContext = await GetDbContextAsync(cancellationToken);
var collection = dbContext.Collection<TEntity>(); var collection = dbContext.Collection<TEntity>();
if (BulkOperationProvider != null) if (BulkOperationProvider != null)
{ {
await BulkOperationProvider.DeleteManyAsync(this, entities.ToArray(), dbContext.SessionHandle, autoSave, cancellationToken); await BulkOperationProvider.DeleteManyAsync(this, entities, dbContext.SessionHandle, autoSave, cancellationToken);
return; return;
} }
if (softDeletedEntities.Count > 0) if (softDeletedEntities.Count > 0)
{ {
UpdateResult updateResult; BulkWriteResult updateResult;
var softDeleteEntitiesCount = softDeletedEntities.Count;
var replaceRequests = new List<WriteModel<TEntity>>(
softDeletedEntities.Select(entity => new ReplaceOneModel<TEntity>(
CreateEntityFilter(entity.Key, true, entity.Value), entity.Key))
);
if (dbContext.SessionHandle != null) if (dbContext.SessionHandle != null)
{ {
updateResult = await collection.UpdateManyAsync( updateResult = await collection.BulkWriteAsync(dbContext.SessionHandle, replaceRequests, cancellationToken: cancellationToken);
dbContext.SessionHandle,
CreateEntitiesFilter(softDeletedEntities),
Builders<TEntity>.Update.Set(x => ((ISoftDelete)x).IsDeleted, true)
);
} }
else else
{ {
updateResult = await collection.UpdateManyAsync( updateResult = await collection.BulkWriteAsync(replaceRequests, cancellationToken: cancellationToken);
CreateEntitiesFilter(softDeletedEntities),
Builders<TEntity>.Update.Set(x => ((ISoftDelete)x).IsDeleted, true)
);
} }
if (updateResult.MatchedCount < softDeleteEntitiesCount) if (updateResult.MatchedCount < softDeletedEntities.Count)
{ {
ThrowOptimisticConcurrencyException(); ThrowOptimisticConcurrencyException();
} }
@ -406,14 +419,14 @@ namespace Volo.Abp.Domain.Repositories.MongoDB
{ {
deleteResult = await collection.DeleteManyAsync( deleteResult = await collection.DeleteManyAsync(
dbContext.SessionHandle, dbContext.SessionHandle,
CreateEntitiesFilter(hardDeletedEntities) CreateEntitiesFilter(hardDeletedEntities),
); cancellationToken: cancellationToken);
} }
else else
{ {
deleteResult = await collection.DeleteManyAsync( deleteResult = await collection.DeleteManyAsync(
CreateEntitiesFilter(hardDeletedEntities) CreateEntitiesFilter(hardDeletedEntities),
); cancellationToken: cancellationToken);
} }
if (deleteResult.DeletedCount < hardDeletedEntitiesCount) if (deleteResult.DeletedCount < hardDeletedEntitiesCount)
@ -462,10 +475,7 @@ namespace Volo.Abp.Domain.Repositories.MongoDB
.Where(predicate) .Where(predicate)
.ToListAsync(cancellationToken); .ToListAsync(cancellationToken);
foreach (var entity in entities) await DeleteManyAsync(entities, autoSave, cancellationToken);
{
await DeleteAsync(entity, autoSave, cancellationToken);
}
} }
[Obsolete("Use GetQueryableAsync method.")] [Obsolete("Use GetQueryableAsync method.")]
@ -484,9 +494,11 @@ namespace Volo.Abp.Domain.Repositories.MongoDB
bool includeDetails = true, bool includeDetails = true,
CancellationToken cancellationToken = default) CancellationToken cancellationToken = default)
{ {
cancellationToken = GetCancellationToken(cancellationToken);
return await (await GetMongoQueryableAsync(cancellationToken)) return await (await GetMongoQueryableAsync(cancellationToken))
.Where(predicate) .Where(predicate)
.SingleOrDefaultAsync(GetCancellationToken(cancellationToken)); .SingleOrDefaultAsync(cancellationToken);
} }
[Obsolete("Use GetMongoQueryableAsync method.")] [Obsolete("Use GetMongoQueryableAsync method.")]
@ -501,6 +513,8 @@ namespace Volo.Abp.Domain.Repositories.MongoDB
public async Task<IMongoQueryable<TEntity>> GetMongoQueryableAsync(CancellationToken cancellationToken = default) public async Task<IMongoQueryable<TEntity>> GetMongoQueryableAsync(CancellationToken cancellationToken = default)
{ {
cancellationToken = GetCancellationToken(cancellationToken);
var dbContext = await GetDbContextAsync(cancellationToken); var dbContext = await GetDbContextAsync(cancellationToken);
var collection = dbContext.Collection<TEntity>(); var collection = dbContext.Collection<TEntity>();
@ -513,6 +527,8 @@ namespace Volo.Abp.Domain.Repositories.MongoDB
public async Task<IAggregateFluent<TEntity>> GetAggregateAsync(CancellationToken cancellationToken = default) public async Task<IAggregateFluent<TEntity>> GetAggregateAsync(CancellationToken cancellationToken = default)
{ {
cancellationToken = GetCancellationToken(cancellationToken);
var dbContext = await GetDbContextAsync(cancellationToken); var dbContext = await GetDbContextAsync(cancellationToken);
var collection = await GetCollectionAsync(cancellationToken); var collection = await GetCollectionAsync(cancellationToken);
@ -695,13 +711,13 @@ namespace Volo.Abp.Domain.Repositories.MongoDB
[Obsolete("This method will be removed in future versions.")] [Obsolete("This method will be removed in future versions.")]
public IAsyncCursor<TEntity> ToCursor(CancellationToken cancellationToken = new CancellationToken()) public IAsyncCursor<TEntity> ToCursor(CancellationToken cancellationToken = new CancellationToken())
{ {
return GetMongoQueryable().ToCursor(cancellationToken); return GetMongoQueryable().ToCursor(GetCancellationToken(cancellationToken));
} }
[Obsolete("This method will be removed in future versions.")] [Obsolete("This method will be removed in future versions.")]
public Task<IAsyncCursor<TEntity>> ToCursorAsync(CancellationToken cancellationToken = new CancellationToken()) public Task<IAsyncCursor<TEntity>> ToCursorAsync(CancellationToken cancellationToken = new CancellationToken())
{ {
return GetMongoQueryable().ToCursorAsync(cancellationToken); return GetMongoQueryable().ToCursorAsync(GetCancellationToken(cancellationToken));
} }
} }
@ -724,7 +740,7 @@ namespace Volo.Abp.Domain.Repositories.MongoDB
bool includeDetails = true, bool includeDetails = true,
CancellationToken cancellationToken = default) CancellationToken cancellationToken = default)
{ {
var entity = await FindAsync(id, includeDetails, cancellationToken); var entity = await FindAsync(id, includeDetails, GetCancellationToken(cancellationToken));
if (entity == null) if (entity == null)
{ {

17
framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Testing/SoftDelete_Tests.cs

@ -56,6 +56,23 @@ namespace Volo.Abp.TestApp.Testing
douglas.DeletionTime.ShouldNotBeNull(); douglas.DeletionTime.ShouldNotBeNull();
} }
} }
[Fact]
public async Task Should_Cancel_Deletion_For_Soft_Delete_Many_Entities_ById()
{
await PersonRepository.DeleteManyAsync(new []{ TestDataBuilder.UserDouglasId });
var douglas = await PersonRepository.FindAsync(TestDataBuilder.UserDouglasId);
douglas.ShouldBeNull();
using (DataFilter.Disable<ISoftDelete>())
{
douglas = await PersonRepository.FindAsync(TestDataBuilder.UserDouglasId);
douglas.ShouldNotBeNull();
douglas.IsDeleted.ShouldBeTrue();
douglas.DeletionTime.ShouldNotBeNull();
}
}
[Fact] [Fact]
public async Task Should_Handle_Deletion_On_Update_For_Soft_Delete_Entities() public async Task Should_Handle_Deletion_On_Update_For_Soft_Delete_Entities()

Loading…
Cancel
Save