Browse Source

Introduce DeleteDirectAsync method for IRepository.

Resolve #14725
pull/14726/head
maliming 4 years ago
parent
commit
dc0e4adbd4
  1. 14
      framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Repositories/IRepository.cs
  2. 2
      framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Repositories/RepositoryBase.cs
  3. 7
      framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/Domain/Repositories/EntityFrameworkCore/EfCoreRepository.cs
  4. 5
      framework/src/Volo.Abp.MemoryDb/Volo/Abp/Domain/Repositories/MemoryDb/MemoryDbRepository.cs
  5. 5
      framework/src/Volo.Abp.MongoDB/Volo/Abp/Domain/Repositories/MongoDB/MongoDbRepository.cs
  6. 5
      framework/test/Volo.Abp.Ddd.Tests/Volo/Abp/Domain/Repositories/RepositoryRegistration_Tests.cs
  7. 21
      framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/Repositories/Repository_Basic_Tests.cs

14
framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Repositories/IRepository.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>

2
framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Repositories/RepositoryBase.cs

@ -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>
{

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

@ -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,

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

@ -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,

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

@ -484,6 +484,11 @@ public class MongoDbRepository<TMongoDbContext, TEntity>
await DeleteManyAsync(entities, autoSave, cancellationToken);
}
public override async Task DeleteDirectAsync(Expression<Func<TEntity, bool>> predicate, CancellationToken cancellationToken = default)
{
await DeleteAsync(predicate, true, cancellationToken);
}
[Obsolete("Use GetQueryableAsync method.")]
protected override IQueryable<TEntity> GetQueryable()
{

5
framework/test/Volo.Abp.Ddd.Tests/Volo/Abp/Domain/Repositories/RepositoryRegistration_Tests.cs

@ -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();

21
framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/Repositories/Repository_Basic_Tests.cs

@ -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);
}
}
}

Loading…
Cancel
Save