From de0836b612f3f57599637b4ed0fd1c2dfd7669bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Halil=20=C4=B0brahim=20Kalkan?= Date: Thu, 21 Jan 2021 18:50:59 +0300 Subject: [PATCH] Update the tutorial for async queryable change. --- docs/en/Tutorials/Part-10.md | 27 +++++++++++++++++---------- docs/en/Tutorials/Part-7.md | 13 ++++++++----- docs/en/Tutorials/Part-8.md | 13 ++++++------- 3 files changed, 31 insertions(+), 22 deletions(-) diff --git a/docs/en/Tutorials/Part-10.md b/docs/en/Tutorials/Part-10.md index a9de3a062e..84cce260c8 100644 --- a/docs/en/Tutorials/Part-10.md +++ b/docs/en/Tutorials/Part-10.md @@ -364,10 +364,11 @@ namespace Acme.BookStore.Books public override async Task GetAsync(Guid id) { - await CheckGetPolicyAsync(); + //Get the IQueryable from the repository + var queryable = await Repository.GetQueryableAsync(); //Prepare a query to join books and authors - var query = from book in Repository + var query = from book in queryable join author in _authorRepository on book.AuthorId equals author.Id where book.Id == id select new { book, author }; @@ -384,17 +385,18 @@ namespace Acme.BookStore.Books return bookDto; } - public override async Task> GetListAsync( - PagedAndSortedResultRequestDto input) + public override async Task> GetListAsync(PagedAndSortedResultRequestDto input) { - await CheckGetListPolicyAsync(); + //Get the IQueryable from the repository + var queryable = await Repository.GetQueryableAsync(); //Prepare a query to join books and authors - var query = from book in Repository + var query = from book in queryable join author in _authorRepository on book.AuthorId equals author.Id - orderby input.Sorting + orderby input.Sorting //TODO: Can not sort like that! select new {book, author}; + //Paging query = query .Skip(input.SkipCount) .Take(input.MaxResultCount); @@ -437,7 +439,7 @@ Let's see the changes we've done: * Injected `IAuthorRepository` to query from the authors. * Overrode the `GetAsync` method of the base `CrudAppService`, which returns a single `BookDto` object with the given `id`. * Used a simple LINQ expression to join books and authors and query them together for the given book id. - * Used `AsyncExecuter.FirstOrDefaultAsync(...)` to execute the query and get a result. `AsyncExecuter` was previously used in the `AuthorAppService`. Check the [repository documentation](../Repositories.md) to understand why we've used it. + * Used `AsyncExecuter.FirstOrDefaultAsync(...)` to execute the query and get a result. It is a way to use asynchronous LINQ extensions without depending on the database provider API. Check the [repository documentation](../Repositories.md) to understand why we've used it. * Throws an `EntityNotFoundException` which results an `HTTP 404` (not found) result if requested book was not present in the database. * Finally, created a `BookDto` object using the `ObjectMapper`, then assigning the `AuthorName` manually. * Overrode the `GetListAsync` method of the base `CrudAppService`, which returns a list of books. The logic is similar to the previous method, so you can easily understand the code. @@ -508,10 +510,13 @@ namespace Acme.BookStore.Books { input.Sorting = nameof(Book.Name); } + + //Get the IQueryable from the repository + var queryable = await Repository.GetQueryableAsync(); //Get the books var books = await AsyncExecuter.ToListAsync( - Repository + queryable .OrderBy(input.Sorting) .Skip(input.SkipCount) .Take(input.MaxResultCount) @@ -553,8 +558,10 @@ namespace Acme.BookStore.Books .Distinct() .ToArray(); + var queryable = await _authorRepository.GetQueryableAsync(); + var authors = await AsyncExecuter.ToListAsync( - _authorRepository.Where(a => authorIds.Contains(a.Id)) + queryable.Where(a => authorIds.Contains(a.Id)) ); return authors.ToDictionary(x => x.Id, x => x); diff --git a/docs/en/Tutorials/Part-7.md b/docs/en/Tutorials/Part-7.md index 97d28691ae..ec3ad9a8e0 100644 --- a/docs/en/Tutorials/Part-7.md +++ b/docs/en/Tutorials/Part-7.md @@ -127,7 +127,8 @@ namespace Acme.BookStore.Authors public async Task FindByNameAsync(string name) { - return await DbSet.FirstOrDefaultAsync(author => author.Name == name); + var dbSet = await GetDbSetAsync(); + return await dbSet.FirstOrDefaultAsync(author => author.Name == name); } public async Task> GetListAsync( @@ -136,7 +137,8 @@ namespace Acme.BookStore.Authors string sorting, string filter = null) { - return await DbSet + var dbSet = await GetDbSetAsync(); + return await dbSet .WhereIf( !filter.IsNullOrWhiteSpace(), author => author.Name.Contains(filter) @@ -186,8 +188,8 @@ namespace Acme.BookStore.Authors public async Task FindByNameAsync(string name) { - return await GetMongoQueryable() - .FirstOrDefaultAsync(author => author.Name == name); + var queryable = await GetMongoQueryableAsync(); + return await queryable.FirstOrDefaultAsync(author => author.Name == name); } public async Task> GetListAsync( @@ -196,7 +198,8 @@ namespace Acme.BookStore.Authors string sorting, string filter = null) { - return await GetMongoQueryable() + var queryable = await GetMongoQueryableAsync(); + return await queryable .WhereIf>( !filter.IsNullOrWhiteSpace(), author => author.Name.Contains(filter) diff --git a/docs/en/Tutorials/Part-8.md b/docs/en/Tutorials/Part-8.md index b7c805c17e..6ab14c714c 100644 --- a/docs/en/Tutorials/Part-8.md +++ b/docs/en/Tutorials/Part-8.md @@ -172,6 +172,7 @@ using System.Threading.Tasks; using Acme.BookStore.Permissions; using Microsoft.AspNetCore.Authorization; using Volo.Abp.Application.Dtos; +using Volo.Abp.Domain.Repositories; namespace Acme.BookStore.Authors { @@ -230,12 +231,10 @@ public async Task> GetListAsync(GetAuthorListDto input input.Filter ); - var totalCount = await AsyncExecuter.CountAsync( - _authorRepository.WhereIf( - !input.Filter.IsNullOrWhiteSpace(), - author => author.Name.Contains(input.Filter) - ) - ); + var totalCount = input.Filter == null + ? await _authorRepository.CountAsync() + : await _authorRepository.CountAsync( + author => author.Name.Contains(input.Filter)); return new PagedResultDto( totalCount, @@ -246,7 +245,7 @@ public async Task> GetListAsync(GetAuthorListDto input * Default sorting is "by author name" which is done in the beginning of the method in case of it wasn't sent by the client. * Used the `IAuthorRepository.GetListAsync` to get a paged, sorted and filtered list of authors from the database. We had implemented it in the previous part of this tutorial. Again, it actually was not needed to create such a method since we could directly query over the repository, but wanted to demonstrate how to create custom repository methods. -* Directly queried from the `AuthorRepository` while getting the count of the authors. We preferred to use the `AsyncExecuter` service which allows us to perform async queries without depending on the EF Core. However, you could depend on the EF Core package and directly use the `_authorRepository.WhereIf(...).ToListAsync()` method. See the [repository document](../Repositories.md) to read the alternative approaches and the discussion. +* Directly queried from the `AuthorRepository` while getting the count of the authors. If a filter is sent, then we are using it to filter entities while getting the count. * Finally, returning a paged result by mapping the list of `Author`s to a list of `AuthorDto`s. ### CreateAsync