Browse Source

Merge pull request #10981 from sh-erfan/dev

Added EnsureExistsAsync for optimal checking
pull/12504/head
maliming 4 years ago
committed by GitHub
parent
commit
c4b19c8154
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 4
      docs/en/Repositories.md
  2. 42
      framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Repositories/RepositoryExtensions.cs
  3. 8
      framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/Repositories/RepositoryExtensions_Tests.cs
  4. 8
      framework/test/Volo.Abp.MemoryDb.Tests/Volo/Abp/MemoryDb/Repositories/RepositoryExtensions_Tests.cs
  5. 10
      framework/test/Volo.Abp.MongoDB.Tests/Volo/Abp/MongoDB/Repositories/RepositoryExtensions_Tests.cs
  6. 38
      framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Testing/RepositoryExtensions_Tests.cs

4
docs/en/Repositories.md

@ -164,6 +164,10 @@ If your entity is a soft-delete entity, you can use the `HardDeleteAsync` method
> See the [Data Filtering](Data-Filtering.md) documentation for more about soft-delete.
### Ensure Entities Exists
The `EnsureExistsAsync` extension method accepts entity id or entities query expression to ensure entities exist, otherwise, it will throw `EntityNotFoundException`.
## Other Generic Repository Types
Standard `IRepository<TEntity, TKey>` interface exposes the standard `IQueryable<TEntity>` and you can freely query using the standard LINQ methods. This is fine for most of the applications. However, some ORM providers or database systems may not support standard `IQueryable` interface. If you want to use such providers, you can't rely on the `IQueryable`.

42
framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Repositories/RepositoryExtensions.cs

@ -15,7 +15,7 @@ namespace Volo.Abp.Domain.Repositories;
public static class RepositoryExtensions
{
public static async Task EnsureCollectionLoadedAsync<TEntity, TKey, TProperty>(
public async static Task EnsureCollectionLoadedAsync<TEntity, TKey, TProperty>(
this IBasicRepository<TEntity, TKey> repository,
TEntity entity,
Expression<Func<TEntity, IEnumerable<TProperty>>> propertyExpression,
@ -31,7 +31,7 @@ public static class RepositoryExtensions
}
}
public static async Task EnsurePropertyLoadedAsync<TEntity, TKey, TProperty>(
public async static Task EnsurePropertyLoadedAsync<TEntity, TKey, TProperty>(
this IBasicRepository<TEntity, TKey> repository,
TEntity entity,
Expression<Func<TEntity, TProperty>> propertyExpression,
@ -47,7 +47,33 @@ public static class RepositoryExtensions
}
}
public static async Task HardDeleteAsync<TEntity>(
public async static Task EnsureExistsAsync<TEntity, TKey>(
this IRepository<TEntity, TKey> repository,
TKey id,
CancellationToken cancellationToken = default
)
where TEntity : class, IEntity<TKey>
{
if (!await repository.AnyAsync(x => x.Id.Equals(id), cancellationToken))
{
throw new EntityNotFoundException(typeof(TEntity), id);
}
}
public async static Task EnsureExistsAsync<TEntity, TKey>(
this IRepository<TEntity, TKey> repository,
Expression<Func<TEntity, bool>> expression,
CancellationToken cancellationToken = default
)
where TEntity : class, IEntity<TKey>
{
if (!await repository.AnyAsync(expression, cancellationToken))
{
throw new EntityNotFoundException(typeof(TEntity));
}
}
public async static Task HardDeleteAsync<TEntity>(
this IRepository<TEntity> repository,
Expression<Func<TEntity, bool>> predicate,
bool autoSave = false,
@ -71,7 +97,7 @@ public static class RepositoryExtensions
}
}
public static async Task HardDeleteAsync<TEntity>(
public async static Task HardDeleteAsync<TEntity>(
this IBasicRepository<TEntity> repository,
IEnumerable<TEntity> entities,
bool autoSave = false,
@ -95,7 +121,7 @@ public static class RepositoryExtensions
}
}
public static async Task HardDeleteAsync<TEntity>(
public async static Task HardDeleteAsync<TEntity>(
this IBasicRepository<TEntity> repository,
TEntity entity,
bool autoSave = false,
@ -139,7 +165,7 @@ public static class RepositoryExtensions
return unitOfWorkManagerAccessor.UnitOfWorkManager;
}
private static async Task HardDeleteWithUnitOfWorkAsync<TEntity>(
private async static Task HardDeleteWithUnitOfWorkAsync<TEntity>(
IRepository<TEntity> repository,
Expression<Func<TEntity, bool>> predicate,
bool autoSave,
@ -155,7 +181,7 @@ public static class RepositoryExtensions
}
}
private static async Task HardDeleteWithUnitOfWorkAsync<TEntity>(
private async static Task HardDeleteWithUnitOfWorkAsync<TEntity>(
IBasicRepository<TEntity> repository,
IEnumerable<TEntity> entities,
bool autoSave,
@ -173,7 +199,7 @@ public static class RepositoryExtensions
await repository.DeleteManyAsync(entities, autoSave, cancellationToken);
}
private static async Task HardDeleteWithUnitOfWorkAsync<TEntity>(
private async static Task HardDeleteWithUnitOfWorkAsync<TEntity>(
IBasicRepository<TEntity> repository,
TEntity entity,
bool autoSave,

8
framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/Repositories/RepositoryExtensions_Tests.cs

@ -0,0 +1,8 @@
using Volo.Abp.TestApp.Testing;
namespace Volo.Abp.EntityFrameworkCore.Repositories;
public class RepositoryExtensions_Tests : RepositoryExtensions_Tests<AbpEntityFrameworkCoreTestModule>
{
}

8
framework/test/Volo.Abp.MemoryDb.Tests/Volo/Abp/MemoryDb/Repositories/RepositoryExtensions_Tests.cs

@ -0,0 +1,8 @@
using Volo.Abp.TestApp.Testing;
namespace Volo.Abp.MemoryDb.Repositories;
public class RepositoryExtensions_Tests : RepositoryExtensions_Tests<AbpMemoryDbTestModule>
{
}

10
framework/test/Volo.Abp.MongoDB.Tests/Volo/Abp/MongoDB/Repositories/RepositoryExtensions_Tests.cs

@ -0,0 +1,10 @@
using Volo.Abp.TestApp.Testing;
using Xunit;
namespace Volo.Abp.MongoDB.Repositories;
[Collection(MongoTestCollection.Name)]
public class RepositoryExtensions_Tests : RepositoryExtensions_Tests<AbpMongoDbTestModule>
{
}

38
framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Testing/RepositoryExtensions_Tests.cs

@ -0,0 +1,38 @@
using System;
using System.Threading.Tasks;
using Volo.Abp.Domain.Entities;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.Modularity;
using Volo.Abp.TestApp.Domain;
using Xunit;
namespace Volo.Abp.TestApp.Testing;
public abstract class RepositoryExtensions_Tests<TStartupModule> : TestAppTestBase<TStartupModule>
where TStartupModule : IAbpModule
{
protected readonly IRepository<Person, Guid> PersonRepository;
protected RepositoryExtensions_Tests()
{
PersonRepository = GetRequiredService<IRepository<Person, Guid>>();
}
[Fact]
public async Task EnsureExistsAsync_Test()
{
await WithUnitOfWorkAsync(async () =>
{
var id = Guid.NewGuid();
await Assert.ThrowsAsync<EntityNotFoundException>(async () =>
await PersonRepository.EnsureExistsAsync(Guid.NewGuid())
);
await Assert.ThrowsAsync<EntityNotFoundException>(async () =>
await PersonRepository.EnsureExistsAsync(x => x.Id == id)
);
await PersonRepository.EnsureExistsAsync(TestDataBuilder.UserDouglasId);
await PersonRepository.EnsureExistsAsync(x => x.Id == TestDataBuilder.UserDouglasId);
});
}
}
Loading…
Cancel
Save