Browse Source
Merge pull request #14726 from abpframework/DeleteDirectAsync
Introduce DeleteDirectAsync method for IRepository.
pull/14810/head
Halil İbrahim Kalkan
4 years ago
committed by
GitHub
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with
68 additions and
0 deletions
-
framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Repositories/IRepository.cs
-
framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Repositories/RepositoryBase.cs
-
framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/Domain/Repositories/EntityFrameworkCore/EfCoreRepository.cs
-
framework/src/Volo.Abp.MemoryDb/Volo/Abp/Domain/Repositories/MemoryDb/MemoryDbRepository.cs
-
framework/src/Volo.Abp.MongoDB/Volo/Abp/Domain/Repositories/MongoDB/MongoDbRepository.cs
-
framework/test/Volo.Abp.Ddd.Tests/Volo/Abp/Domain/Repositories/RepositoryRegistration_Tests.cs
-
framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/Repositories/Repository_Basic_Tests.cs
|
|
|
@ -68,6 +68,20 @@ public interface IRepository<TEntity> : IReadOnlyRepository<TEntity>, IBasicRepo |
|
|
|
bool autoSave = false, |
|
|
|
CancellationToken cancellationToken = default |
|
|
|
); |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Deletes all entities those fit to the given predicate.
|
|
|
|
/// It directly deletes entities from database, without fetching them.
|
|
|
|
/// Some features (like soft-delete, multi-tenancy and audit logging) won't work, so use this method carefully when you need it.
|
|
|
|
/// Use the DeleteAsync method if you need to these features.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="predicate">A condition to filter entities</param>
|
|
|
|
/// <param name="cancellationToken">A <see cref="T:System.Threading.CancellationToken" /> to observe while waiting for the task to complete.</param>
|
|
|
|
/// <returns></returns>
|
|
|
|
Task DeleteDirectAsync( |
|
|
|
[NotNull] Expression<Func<TEntity, bool>> predicate, |
|
|
|
CancellationToken cancellationToken = default |
|
|
|
); |
|
|
|
} |
|
|
|
|
|
|
|
public interface IRepository<TEntity, TKey> : IRepository<TEntity>, IReadOnlyRepository<TEntity, TKey>, IBasicRepository<TEntity, TKey> |
|
|
|
|
|
|
|
@ -63,6 +63,8 @@ public abstract class RepositoryBase<TEntity> : BasicRepositoryBase<TEntity>, IR |
|
|
|
|
|
|
|
public abstract Task DeleteAsync(Expression<Func<TEntity, bool>> predicate, bool autoSave = false, CancellationToken cancellationToken = default); |
|
|
|
|
|
|
|
public abstract Task DeleteDirectAsync(Expression<Func<TEntity, bool>> predicate, CancellationToken cancellationToken = default); |
|
|
|
|
|
|
|
protected virtual TQueryable ApplyDataFilters<TQueryable>(TQueryable query) |
|
|
|
where TQueryable : IQueryable<TEntity> |
|
|
|
{ |
|
|
|
|
|
|
|
@ -304,6 +304,13 @@ public class EfCoreRepository<TDbContext, TEntity> : RepositoryBase<TEntity>, IE |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public override async Task DeleteDirectAsync(Expression<Func<TEntity, bool>> predicate, CancellationToken cancellationToken = default) |
|
|
|
{ |
|
|
|
var dbContext = await GetDbContextAsync(); |
|
|
|
var dbSet = dbContext.Set<TEntity>(); |
|
|
|
await dbSet.Where(predicate).ExecuteDeleteAsync(GetCancellationToken(cancellationToken)); |
|
|
|
} |
|
|
|
|
|
|
|
public virtual async Task EnsureCollectionLoadedAsync<TProperty>( |
|
|
|
TEntity entity, |
|
|
|
Expression<Func<TEntity, IEnumerable<TProperty>>> propertyExpression, |
|
|
|
|
|
|
|
@ -205,6 +205,11 @@ public class MemoryDbRepository<TMemoryDbContext, TEntity> : RepositoryBase<TEnt |
|
|
|
await DeleteManyAsync(entities, autoSave, cancellationToken); |
|
|
|
} |
|
|
|
|
|
|
|
public override async Task DeleteDirectAsync(Expression<Func<TEntity, bool>> predicate, CancellationToken cancellationToken = default) |
|
|
|
{ |
|
|
|
await DeleteAsync(predicate, true, cancellationToken); |
|
|
|
} |
|
|
|
|
|
|
|
public override async Task<TEntity> InsertAsync( |
|
|
|
TEntity entity, |
|
|
|
bool autoSave = false, |
|
|
|
|
|
|
|
@ -484,6 +484,20 @@ public class MongoDbRepository<TMongoDbContext, TEntity> |
|
|
|
await DeleteManyAsync(entities, autoSave, cancellationToken); |
|
|
|
} |
|
|
|
|
|
|
|
public override async Task DeleteDirectAsync(Expression<Func<TEntity, bool>> predicate, CancellationToken cancellationToken = default) |
|
|
|
{ |
|
|
|
cancellationToken = GetCancellationToken(cancellationToken); |
|
|
|
|
|
|
|
var dbContext = await GetDbContextAsync(cancellationToken); |
|
|
|
var collection = dbContext.Collection<TEntity>(); |
|
|
|
|
|
|
|
await collection.DeleteManyAsync( |
|
|
|
dbContext.SessionHandle, |
|
|
|
Builders<TEntity>.Filter.Where(predicate), |
|
|
|
cancellationToken: cancellationToken |
|
|
|
); |
|
|
|
} |
|
|
|
|
|
|
|
[Obsolete("Use GetQueryableAsync method.")] |
|
|
|
protected override IQueryable<TEntity> GetQueryable() |
|
|
|
{ |
|
|
|
|
|
|
|
@ -317,6 +317,11 @@ public class RepositoryRegistration_Tests |
|
|
|
throw new NotImplementedException(); |
|
|
|
} |
|
|
|
|
|
|
|
public override Task DeleteDirectAsync(Expression<Func<TEntity, bool>> predicate, CancellationToken cancellationToken = default) |
|
|
|
{ |
|
|
|
throw new NotImplementedException(); |
|
|
|
} |
|
|
|
|
|
|
|
public override Task<TEntity> InsertAsync(TEntity entity, bool autoSave = false, CancellationToken cancellationToken = default) |
|
|
|
{ |
|
|
|
throw new NotImplementedException(); |
|
|
|
|
|
|
|
@ -1,8 +1,13 @@ |
|
|
|
using System.Threading.Tasks; |
|
|
|
using Microsoft.EntityFrameworkCore; |
|
|
|
using Microsoft.Extensions.DependencyInjection; |
|
|
|
using Shouldly; |
|
|
|
using Volo.Abp.Data; |
|
|
|
using Volo.Abp.Domain.Repositories; |
|
|
|
using Volo.Abp.TestApp; |
|
|
|
using Volo.Abp.TestApp.Domain; |
|
|
|
using Volo.Abp.TestApp.Testing; |
|
|
|
using Volo.Abp.Uow; |
|
|
|
using Xunit; |
|
|
|
|
|
|
|
namespace Volo.Abp.EntityFrameworkCore.Repositories; |
|
|
|
@ -28,4 +33,20 @@ public class Repository_Basic_Tests : Repository_Basic_Tests<AbpEntityFrameworkC |
|
|
|
count.ShouldBeGreaterThan(0); |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
[Fact] |
|
|
|
public async Task DeleteDirect_Test() |
|
|
|
{ |
|
|
|
using (ServiceProvider.GetRequiredService<IUnitOfWorkManager>().Begin()) |
|
|
|
{ |
|
|
|
await PersonRepository.DeleteAsync(x => x.Id == TestDataBuilder.UserDouglasId); |
|
|
|
(await PersonRepository.GetDbContextAsync()).ChangeTracker.Entries<Person>().ShouldContain(x => x.Entity.Id == TestDataBuilder.UserDouglasId); |
|
|
|
} |
|
|
|
|
|
|
|
using (ServiceProvider.GetRequiredService<IUnitOfWorkManager>().Begin()) |
|
|
|
{ |
|
|
|
await PersonRepository.DeleteDirectAsync(x => x.Id == TestDataBuilder.UserDouglasId); |
|
|
|
(await PersonRepository.GetDbContextAsync()).ChangeTracker.Entries<Person>().ShouldNotContain(x => x.Entity.Id == TestDataBuilder.UserDouglasId); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|