Browse Source

Update best practice db operation documents for async queryable.

pull/7385/head
Halil İbrahim Kalkan 5 years ago
parent
commit
a9aef8cc65
  1. 9
      docs/en/Best-Practices/Entity-Framework-Core-Integration.md
  2. 6
      docs/en/Best-Practices/MongoDB-Integration.md
  3. 18
      docs/en/Best-Practices/Repositories.md

9
docs/en/Best-Practices/Entity-Framework-Core-Integration.md

@ -144,7 +144,7 @@ public virtual async Task<IdentityUser> FindByNormalizedUserNameAsync(
bool includeDetails = true,
CancellationToken cancellationToken = default)
{
return await DbSet
return await (await GetDbSetAsync())
.IncludeDetails(includeDetails)
.FirstOrDefaultAsync(
u => u.NormalizedUserName == normalizedUserName,
@ -175,14 +175,15 @@ public static IQueryable<IdentityUser> IncludeDetails(
}
````
* **Do** use the `IncludeDetails` extension method in the repository methods just like used in the example code above (see FindByNormalizedUserNameAsync).
* **Do** use the `IncludeDetails` extension method in the repository methods just like used in the example code above (see `FindByNormalizedUserNameAsync`).
- **Do** override `WithDetails` method of the repository for aggregates root which have **sub collections**. Example:
````C#
public override IQueryable<IdentityUser> WithDetails()
public override async Task<IQueryable<IdentityUser>> WithDetailsAsync()
{
return GetQueryable().IncludeDetails(); // Uses the extension method defined above
// Uses the extension method defined above
return (await GetQueryableAsync()).IncludeDetails();
}
````

6
docs/en/Best-Practices/MongoDB-Integration.md

@ -128,7 +128,7 @@ public async Task<IdentityUser> FindByNormalizedUserNameAsync(
bool includeDetails = true,
CancellationToken cancellationToken = default)
{
return await GetMongoQueryable()
return await (await GetMongoQueryableAsync())
.FirstOrDefaultAsync(
u => u.NormalizedUserName == normalizedUserName,
GetCancellationToken(cancellationToken)
@ -139,8 +139,8 @@ public async Task<IdentityUser> FindByNormalizedUserNameAsync(
`GetCancellationToken` fallbacks to the `ICancellationTokenProvider.Token` to obtain the cancellation token if it is not provided by the caller code.
* **Do** ignore the `includeDetails` parameters for the repository implementation since MongoDB loads the aggregate root as a whole (including sub collections) by default.
* **Do** use the `GetMongoQueryable()` method to obtain an `IQueryable<TEntity>` to perform queries wherever possible. Because;
* `GetMongoQueryable()` method automatically uses the `ApplyDataFilters` method to filter the data based on the current data filters (like soft delete and multi-tenancy).
* **Do** use the `GetMongoQueryableAsync()` method to obtain an `IQueryable<TEntity>` to perform queries wherever possible. Because;
* `GetMongoQueryableAsync()` method automatically uses the `ApplyDataFilters` method to filter the data based on the current data filters (like soft delete and multi-tenancy).
* Using `IQueryable<TEntity>` makes the code as much as similar to the EF Core repository implementation and easy to write and read.
* **Do** implement data filtering if it is not possible to use the `GetMongoQueryable()` method.

18
docs/en/Best-Practices/Repositories.md

@ -42,24 +42,6 @@ Task<IdentityUser> FindByNormalizedUserNameAsync(
);
````
* **Do** create a **synchronous extension** method for each asynchronous repository method. Example:
````C#
public static class IdentityUserRepositoryExtensions
{
public static IdentityUser FindByNormalizedUserName(
this IIdentityUserRepository repository,
[NotNull] string normalizedUserName)
{
return AsyncHelper.RunSync(
() => repository.FindByNormalizedUserNameAsync(normalizedUserName)
);
}
}
````
This will allow synchronous code to use the repository methods easier.
* **Do** add an optional `bool includeDetails = true` parameter (default value is `true`) for every repository method which returns a **single entity**. Example:
````C#

Loading…
Cancel
Save