From a9aef8cc65f1dd4848f9799eead7730e83a0089f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Halil=20=C4=B0brahim=20Kalkan?= Date: Thu, 21 Jan 2021 18:06:21 +0300 Subject: [PATCH] Update best practice db operation documents for async queryable. --- .../Entity-Framework-Core-Integration.md | 9 +++++---- docs/en/Best-Practices/MongoDB-Integration.md | 6 +++--- docs/en/Best-Practices/Repositories.md | 18 ------------------ 3 files changed, 8 insertions(+), 25 deletions(-) diff --git a/docs/en/Best-Practices/Entity-Framework-Core-Integration.md b/docs/en/Best-Practices/Entity-Framework-Core-Integration.md index f487f9d61d..04ddeaeba4 100644 --- a/docs/en/Best-Practices/Entity-Framework-Core-Integration.md +++ b/docs/en/Best-Practices/Entity-Framework-Core-Integration.md @@ -144,7 +144,7 @@ public virtual async Task 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 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 WithDetails() +public override async Task> WithDetailsAsync() { - return GetQueryable().IncludeDetails(); // Uses the extension method defined above + // Uses the extension method defined above + return (await GetQueryableAsync()).IncludeDetails(); } ```` diff --git a/docs/en/Best-Practices/MongoDB-Integration.md b/docs/en/Best-Practices/MongoDB-Integration.md index 74a0e429f1..90f7871a70 100644 --- a/docs/en/Best-Practices/MongoDB-Integration.md +++ b/docs/en/Best-Practices/MongoDB-Integration.md @@ -128,7 +128,7 @@ public async Task 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 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` 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` 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` 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. diff --git a/docs/en/Best-Practices/Repositories.md b/docs/en/Best-Practices/Repositories.md index c0058df433..4f646a7d94 100644 --- a/docs/en/Best-Practices/Repositories.md +++ b/docs/en/Best-Practices/Repositories.md @@ -42,24 +42,6 @@ Task 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#