From 7989bc333f1237216c7a5125ad96702e35f48a6b Mon Sep 17 00:00:00 2001 From: "hualin.zhu" Date: Fri, 24 Feb 2023 21:41:40 +0800 Subject: [PATCH] refactoring IncludableQueryable --- src/Application/Common/Extensions/QueryableExtensions.cs | 2 +- src/Application/Common/Interfaces/ISpecification.cs | 3 ++- src/Application/Common/Specification/Specification.cs | 5 +++-- src/Application/Features/Documents/DTOs/DocumentDto.cs | 2 +- .../PaginationQuery/DocumentsWithPaginationQuery.cs | 4 ++-- .../Features/Identity/Dto/ApplicationUserDto.cs | 7 +++++++ src/Blazor.Server.UI/Pages/Documents/Documents.razor | 4 ++-- 7 files changed, 18 insertions(+), 9 deletions(-) diff --git a/src/Application/Common/Extensions/QueryableExtensions.cs b/src/Application/Common/Extensions/QueryableExtensions.cs index 08657df16..ba060e037 100644 --- a/src/Application/Common/Extensions/QueryableExtensions.cs +++ b/src/Application/Common/Extensions/QueryableExtensions.cs @@ -11,7 +11,7 @@ public static IQueryable Specify(this IQueryable query, ISpecification< { var queryableResultWithIncludes = spec.Includes .Aggregate(query, - (current, include) => current.Include(include)); + (current, include) => include(current)); var secondaryResult = spec.IncludeStrings .Aggregate(queryableResultWithIncludes, (current, include) => current.Include(include)); diff --git a/src/Application/Common/Interfaces/ISpecification.cs b/src/Application/Common/Interfaces/ISpecification.cs index 3f535823a..cb1de4d29 100644 --- a/src/Application/Common/Interfaces/ISpecification.cs +++ b/src/Application/Common/Interfaces/ISpecification.cs @@ -3,13 +3,14 @@ using System.Linq.Expressions; using CleanArchitecture.Blazor.Domain.Common; +using Microsoft.EntityFrameworkCore.Query; namespace CleanArchitecture.Blazor.Application.Common.Interfaces; public interface ISpecification where T : class, IEntity { Expression> Criteria { get; } - List>> Includes { get; } + List, IIncludableQueryable>> Includes { get; } List IncludeStrings { get; } Expression> And(Expression> query); Expression> Or(Expression> query); diff --git a/src/Application/Common/Specification/Specification.cs b/src/Application/Common/Specification/Specification.cs index 07cb65c02..a34338409 100644 --- a/src/Application/Common/Specification/Specification.cs +++ b/src/Application/Common/Specification/Specification.cs @@ -3,16 +3,17 @@ using System.Linq.Expressions; using CleanArchitecture.Blazor.Domain.Common; +using Microsoft.EntityFrameworkCore.Query; namespace CleanArchitecture.Blazor.Application.Common.Specification; #nullable disable public abstract class Specification : ISpecification where T : class, IEntity { public Expression> Criteria { get; set; } - public List>> Includes { get; } = new(); + public List, IIncludableQueryable>> Includes { get; } = new(); public List IncludeStrings { get; } = new(); - protected virtual void AddInclude(Expression> includeExpression) + protected virtual void AddInclude(Func, IIncludableQueryable> includeExpression) { Includes.Add(includeExpression); } diff --git a/src/Application/Features/Documents/DTOs/DocumentDto.cs b/src/Application/Features/Documents/DTOs/DocumentDto.cs index c4bd36128..d2dc7f77d 100644 --- a/src/Application/Features/Documents/DTOs/DocumentDto.cs +++ b/src/Application/Features/Documents/DTOs/DocumentDto.cs @@ -25,5 +25,5 @@ public void Mapping(Profile profile) public JobStatus Status { get; set; } = JobStatus.NotStart; public string? Content { get; set; } - public ApplicationUserDto Owner { get; set; } + public ApplicationUserDto? Owner { get; set; } } diff --git a/src/Application/Features/Documents/Queries/PaginationQuery/DocumentsWithPaginationQuery.cs b/src/Application/Features/Documents/Queries/PaginationQuery/DocumentsWithPaginationQuery.cs index 0eaa71382..ad791ca2b 100644 --- a/src/Application/Features/Documents/Queries/PaginationQuery/DocumentsWithPaginationQuery.cs +++ b/src/Application/Features/Documents/Queries/PaginationQuery/DocumentsWithPaginationQuery.cs @@ -52,8 +52,8 @@ internal class DocumentsQuery : Specification { public DocumentsQuery(string userId,string tenantId,string? keyword) { - AddInclude(x => x.Owner); - AddInclude(x => x.Editor); + AddInclude(x=>x.Include(x=>x.Owner).ThenInclude(x=>x.Superior)); + AddInclude(x => x.Include(x=>x.Editor).ThenInclude(x=>x.Superior)); this.Criteria = p => (p.CreatedBy == userId && p.IsPublic == false) || p.IsPublic == true; And(x => x.TenantId == tenantId); if (!string.IsNullOrEmpty(keyword)) diff --git a/src/Application/Features/Identity/Dto/ApplicationUserDto.cs b/src/Application/Features/Identity/Dto/ApplicationUserDto.cs index 2cbf648a5..03c58a53d 100644 --- a/src/Application/Features/Identity/Dto/ApplicationUserDto.cs +++ b/src/Application/Features/Identity/Dto/ApplicationUserDto.cs @@ -3,11 +3,18 @@ using System.Linq; using System.Text; using System.Threading.Tasks; +using CleanArchitecture.Blazor.Application.Features.Documents.DTOs; using CleanArchitecture.Blazor.Domain.Identity; namespace CleanArchitecture.Blazor.Application.Features.Identity.Dto; public class ApplicationUserDto:IMapFrom { + public void Mapping(Profile profile) + { + profile.CreateMap(MemberList.None) + .ForMember(x => x.SuperiorName, s => s.MapFrom(y => y.Superior.UserName)); + + } public string Id { get; set; } = string.Empty; public string UserName { get; set; } = string.Empty; public string? DisplayName { get; set; } diff --git a/src/Blazor.Server.UI/Pages/Documents/Documents.razor b/src/Blazor.Server.UI/Pages/Documents/Documents.razor index fad95e13a..2e86c20c3 100644 --- a/src/Blazor.Server.UI/Pages/Documents/Documents.razor +++ b/src/Blazor.Server.UI/Pages/Documents/Documents.razor @@ -171,8 +171,8 @@
- @context.Owner.DisplayName - @context.Owner.Email + @context.Owner?.DisplayName + @context.Owner?.Email