Browse Source

Skip bulk insert/update/delete if array is empty.

Resolves #17522
pull/17525/head
maliming 3 years ago
parent
commit
9b4a59b814
  1. 57
      framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/Domain/Repositories/EntityFrameworkCore/EfCoreRepository.cs
  2. 22
      framework/src/Volo.Abp.MongoDB/Volo/Abp/Domain/Repositories/MongoDB/MongoDbRepository.cs

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

@ -95,7 +95,7 @@ public class EfCoreRepository<TDbContext, TEntity> : RepositoryBase<TEntity>, IE
); );
} }
public override async Task<TEntity> InsertAsync(TEntity entity, bool autoSave = false, CancellationToken cancellationToken = default) public async override Task<TEntity> InsertAsync(TEntity entity, bool autoSave = false, CancellationToken cancellationToken = default)
{ {
CheckAndSetId(entity); CheckAndSetId(entity);
@ -111,9 +111,14 @@ public class EfCoreRepository<TDbContext, TEntity> : RepositoryBase<TEntity>, IE
return savedEntity; return savedEntity;
} }
public override async Task InsertManyAsync(IEnumerable<TEntity> entities, bool autoSave = false, CancellationToken cancellationToken = default) public async override Task InsertManyAsync(IEnumerable<TEntity> entities, bool autoSave = false, CancellationToken cancellationToken = default)
{ {
var entityArray = entities.ToArray(); var entityArray = entities.ToArray();
if (entityArray.IsNullOrEmpty())
{
return;
}
var dbContext = await GetDbContextAsync(); var dbContext = await GetDbContextAsync();
cancellationToken = GetCancellationToken(cancellationToken); cancellationToken = GetCancellationToken(cancellationToken);
@ -141,7 +146,7 @@ public class EfCoreRepository<TDbContext, TEntity> : RepositoryBase<TEntity>, IE
} }
} }
public override async Task<TEntity> UpdateAsync(TEntity entity, bool autoSave = false, CancellationToken cancellationToken = default) public async override Task<TEntity> UpdateAsync(TEntity entity, bool autoSave = false, CancellationToken cancellationToken = default)
{ {
var dbContext = await GetDbContextAsync(); var dbContext = await GetDbContextAsync();
@ -157,15 +162,21 @@ public class EfCoreRepository<TDbContext, TEntity> : RepositoryBase<TEntity>, IE
return updatedEntity; return updatedEntity;
} }
public override async Task UpdateManyAsync(IEnumerable<TEntity> entities, bool autoSave = false, CancellationToken cancellationToken = default) public async override Task UpdateManyAsync(IEnumerable<TEntity> entities, bool autoSave = false, CancellationToken cancellationToken = default)
{ {
var entityArray = entities.ToArray();
if (entityArray.IsNullOrEmpty())
{
return;
}
cancellationToken = GetCancellationToken(cancellationToken); cancellationToken = GetCancellationToken(cancellationToken);
if (BulkOperationProvider != null) if (BulkOperationProvider != null)
{ {
await BulkOperationProvider.UpdateManyAsync<TDbContext, TEntity>( await BulkOperationProvider.UpdateManyAsync<TDbContext, TEntity>(
this, this,
entities, entityArray,
autoSave, autoSave,
GetCancellationToken(cancellationToken) GetCancellationToken(cancellationToken)
); );
@ -175,7 +186,7 @@ public class EfCoreRepository<TDbContext, TEntity> : RepositoryBase<TEntity>, IE
var dbContext = await GetDbContextAsync(); var dbContext = await GetDbContextAsync();
dbContext.Set<TEntity>().UpdateRange(entities); dbContext.Set<TEntity>().UpdateRange(entityArray);
if (autoSave) if (autoSave)
{ {
@ -183,7 +194,7 @@ public class EfCoreRepository<TDbContext, TEntity> : RepositoryBase<TEntity>, IE
} }
} }
public override async Task DeleteAsync(TEntity entity, bool autoSave = false, CancellationToken cancellationToken = default) public async override Task DeleteAsync(TEntity entity, bool autoSave = false, CancellationToken cancellationToken = default)
{ {
var dbContext = await GetDbContextAsync(); var dbContext = await GetDbContextAsync();
@ -195,15 +206,21 @@ public class EfCoreRepository<TDbContext, TEntity> : RepositoryBase<TEntity>, IE
} }
} }
public override async Task DeleteManyAsync(IEnumerable<TEntity> entities, bool autoSave = false, CancellationToken cancellationToken = default) public async override Task DeleteManyAsync(IEnumerable<TEntity> entities, bool autoSave = false, CancellationToken cancellationToken = default)
{ {
var entityArray = entities.ToArray();
if (entityArray.IsNullOrEmpty())
{
return;
}
cancellationToken = GetCancellationToken(cancellationToken); cancellationToken = GetCancellationToken(cancellationToken);
if (BulkOperationProvider != null) if (BulkOperationProvider != null)
{ {
await BulkOperationProvider.DeleteManyAsync<TDbContext, TEntity>( await BulkOperationProvider.DeleteManyAsync<TDbContext, TEntity>(
this, this,
entities, entityArray,
autoSave, autoSave,
cancellationToken cancellationToken
); );
@ -213,7 +230,7 @@ public class EfCoreRepository<TDbContext, TEntity> : RepositoryBase<TEntity>, IE
var dbContext = await GetDbContextAsync(); var dbContext = await GetDbContextAsync();
dbContext.RemoveRange(entities); dbContext.RemoveRange(entityArray.Select(x => x));
if (autoSave) if (autoSave)
{ {
@ -221,26 +238,26 @@ public class EfCoreRepository<TDbContext, TEntity> : RepositoryBase<TEntity>, IE
} }
} }
public override async Task<List<TEntity>> GetListAsync(bool includeDetails = false, CancellationToken cancellationToken = default) public async override Task<List<TEntity>> GetListAsync(bool includeDetails = false, CancellationToken cancellationToken = default)
{ {
return includeDetails return includeDetails
? await (await WithDetailsAsync()).ToListAsync(GetCancellationToken(cancellationToken)) ? await (await WithDetailsAsync()).ToListAsync(GetCancellationToken(cancellationToken))
: await (await GetDbSetAsync()).ToListAsync(GetCancellationToken(cancellationToken)); : await (await GetDbSetAsync()).ToListAsync(GetCancellationToken(cancellationToken));
} }
public override async Task<List<TEntity>> GetListAsync(Expression<Func<TEntity, bool>> predicate, bool includeDetails = false, CancellationToken cancellationToken = default) public async override Task<List<TEntity>> GetListAsync(Expression<Func<TEntity, bool>> predicate, bool includeDetails = false, CancellationToken cancellationToken = default)
{ {
return includeDetails return includeDetails
? await (await WithDetailsAsync()).Where(predicate).ToListAsync(GetCancellationToken(cancellationToken)) ? await (await WithDetailsAsync()).Where(predicate).ToListAsync(GetCancellationToken(cancellationToken))
: await (await GetDbSetAsync()).Where(predicate).ToListAsync(GetCancellationToken(cancellationToken)); : await (await GetDbSetAsync()).Where(predicate).ToListAsync(GetCancellationToken(cancellationToken));
} }
public override async Task<long> GetCountAsync(CancellationToken cancellationToken = default) public async override Task<long> GetCountAsync(CancellationToken cancellationToken = default)
{ {
return await (await GetDbSetAsync()).LongCountAsync(GetCancellationToken(cancellationToken)); return await (await GetDbSetAsync()).LongCountAsync(GetCancellationToken(cancellationToken));
} }
public override async Task<List<TEntity>> GetPagedListAsync( public async override Task<List<TEntity>> GetPagedListAsync(
int skipCount, int skipCount,
int maxResultCount, int maxResultCount,
string sorting, string sorting,
@ -263,17 +280,17 @@ public class EfCoreRepository<TDbContext, TEntity> : RepositoryBase<TEntity>, IE
return DbSet.AsQueryable(); return DbSet.AsQueryable();
} }
public override async Task<IQueryable<TEntity>> GetQueryableAsync() public async override Task<IQueryable<TEntity>> GetQueryableAsync()
{ {
return (await GetDbSetAsync()).AsQueryable(); return (await GetDbSetAsync()).AsQueryable();
} }
protected override async Task SaveChangesAsync(CancellationToken cancellationToken) protected async override Task SaveChangesAsync(CancellationToken cancellationToken)
{ {
await (await GetDbContextAsync()).SaveChangesAsync(cancellationToken); await (await GetDbContextAsync()).SaveChangesAsync(cancellationToken);
} }
public override async Task<TEntity> FindAsync( public async override Task<TEntity> FindAsync(
Expression<Func<TEntity, bool>> predicate, Expression<Func<TEntity, bool>> predicate,
bool includeDetails = true, bool includeDetails = true,
CancellationToken cancellationToken = default) CancellationToken cancellationToken = default)
@ -287,7 +304,7 @@ public class EfCoreRepository<TDbContext, TEntity> : RepositoryBase<TEntity>, IE
.SingleOrDefaultAsync(GetCancellationToken(cancellationToken)); .SingleOrDefaultAsync(GetCancellationToken(cancellationToken));
} }
public override async Task DeleteAsync(Expression<Func<TEntity, bool>> predicate, bool autoSave = false, CancellationToken cancellationToken = default) public async override Task DeleteAsync(Expression<Func<TEntity, bool>> predicate, bool autoSave = false, CancellationToken cancellationToken = default)
{ {
var dbContext = await GetDbContextAsync(); var dbContext = await GetDbContextAsync();
var dbSet = dbContext.Set<TEntity>(); var dbSet = dbContext.Set<TEntity>();
@ -339,7 +356,7 @@ public class EfCoreRepository<TDbContext, TEntity> : RepositoryBase<TEntity>, IE
return AbpEntityOptions.DefaultWithDetailsFunc(GetQueryable()); return AbpEntityOptions.DefaultWithDetailsFunc(GetQueryable());
} }
public override async Task<IQueryable<TEntity>> WithDetailsAsync() public async override Task<IQueryable<TEntity>> WithDetailsAsync()
{ {
if (AbpEntityOptions.DefaultWithDetailsFunc == null) if (AbpEntityOptions.DefaultWithDetailsFunc == null)
{ {
@ -358,7 +375,7 @@ public class EfCoreRepository<TDbContext, TEntity> : RepositoryBase<TEntity>, IE
); );
} }
public override async Task<IQueryable<TEntity>> WithDetailsAsync(params Expression<Func<TEntity, object>>[] propertySelectors) public async override Task<IQueryable<TEntity>> WithDetailsAsync(params Expression<Func<TEntity, object>>[] propertySelectors)
{ {
return IncludeDetails( return IncludeDetails(
await GetQueryableAsync(), await GetQueryableAsync(),

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

@ -137,9 +137,13 @@ public class MongoDbRepository<TMongoDbContext, TEntity>
public async override Task InsertManyAsync(IEnumerable<TEntity> entities, bool autoSave = false, CancellationToken cancellationToken = default) public async override Task InsertManyAsync(IEnumerable<TEntity> entities, bool autoSave = false, CancellationToken cancellationToken = default)
{ {
cancellationToken = GetCancellationToken(cancellationToken);
var entityArray = entities.ToArray(); var entityArray = entities.ToArray();
if (entityArray.IsNullOrEmpty())
{
return;
}
cancellationToken = GetCancellationToken(cancellationToken);
foreach (var entity in entityArray) foreach (var entity in entityArray)
{ {
@ -226,6 +230,10 @@ public class MongoDbRepository<TMongoDbContext, TEntity>
public async override Task UpdateManyAsync(IEnumerable<TEntity> entities, bool autoSave = false, CancellationToken cancellationToken = default) public async override Task UpdateManyAsync(IEnumerable<TEntity> entities, bool autoSave = false, CancellationToken cancellationToken = default)
{ {
var entityArray = entities.ToArray(); var entityArray = entities.ToArray();
if (entityArray.IsNullOrEmpty())
{
return;
}
foreach (var entity in entityArray) foreach (var entity in entityArray)
{ {
@ -356,12 +364,18 @@ public class MongoDbRepository<TMongoDbContext, TEntity>
bool autoSave = false, bool autoSave = false,
CancellationToken cancellationToken = default) CancellationToken cancellationToken = default)
{ {
var entityArray = entities.ToArray();
if (entityArray.IsNullOrEmpty())
{
return;
}
cancellationToken = GetCancellationToken(cancellationToken); cancellationToken = GetCancellationToken(cancellationToken);
var softDeletedEntities = new Dictionary<TEntity, string>(); 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 entityArray)
{ {
if (typeof(ISoftDelete).IsAssignableFrom(typeof(TEntity)) && !IsHardDeleted(entity)) if (typeof(ISoftDelete).IsAssignableFrom(typeof(TEntity)) && !IsHardDeleted(entity))
{ {
@ -381,7 +395,7 @@ public class MongoDbRepository<TMongoDbContext, TEntity>
if (BulkOperationProvider != null) if (BulkOperationProvider != null)
{ {
await BulkOperationProvider.DeleteManyAsync(this, entities, dbContext.SessionHandle, autoSave, cancellationToken); await BulkOperationProvider.DeleteManyAsync(this, entityArray, dbContext.SessionHandle, autoSave, cancellationToken);
return; return;
} }

Loading…
Cancel
Save