Add document and unit tests.

pull/10981/head
maliming 4 years ago
parent 1b113fa3ad
commit 9355e0673d
No known key found for this signature in database
GPG Key ID: 096224957E51C89E

@ -607,6 +607,18 @@ Whenever you access to a property/collection, EF Core automatically performs an
See also [lazy loading document](https://docs.microsoft.com/en-us/ef/core/querying/related-data/lazy) of the EF Core.
## Repository Extensions
There are some useful repository extension methods.
````csharp
EnsureCollectionLoadedAsync // **Explicitly load** a sub collection.
EnsurePropertyLoadedAsync // **Explicitly load** a navigation property.
EnsureExistsAsync // Make sure the entitys exists, It accepts the entity id or a query expression.
HardDeleteAsync // Hard delete the entitys, It accepts one or more entities or query expressions.
HardDeleteWithUnitOfWorkAsync // Hard delete the entitys in a uow(create a new uow if current is null), It accepts one or more entities or query expressions.
````
## Access to the EF Core API
In most cases, you want to hide EF Core APIs behind a repository (this is the main purpose of the repository pattern). However, if you want to access the `DbContext` instance over the repository, you can use `GetDbContext()` or `GetDbSet()` extension methods. Example:

@ -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,7 @@ public static class RepositoryExtensions
}
}
public static async Task EnsureExistsAsync<TEntity, TKey>(
public async static Task EnsureExistsAsync<TEntity, TKey>(
this IRepository<TEntity, TKey> repository,
TKey id,
CancellationToken cancellationToken = default
@ -60,7 +60,7 @@ public static class RepositoryExtensions
}
}
public static async Task EnsureExistsAsync<TEntity, TKey>(
public async static Task EnsureExistsAsync<TEntity, TKey>(
this IRepository<TEntity, TKey> repository,
Expression<Func<TEntity, bool>> expression,
CancellationToken cancellationToken = default
@ -73,7 +73,7 @@ public static class RepositoryExtensions
}
}
public static async Task HardDeleteAsync<TEntity>(
public async static Task HardDeleteAsync<TEntity>(
this IRepository<TEntity> repository,
Expression<Func<TEntity, bool>> predicate,
bool autoSave = false,
@ -97,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,
@ -121,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,
@ -165,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,
@ -181,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,
@ -199,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,

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

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

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

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