Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactoring IncludableQueryable #296

Merged
merged 1 commit into from
Feb 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Application/Common/Extensions/QueryableExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public static IQueryable<T> Specify<T>(this IQueryable<T> 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));
Expand Down
3 changes: 2 additions & 1 deletion src/Application/Common/Interfaces/ISpecification.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T> where T : class, IEntity
{
Expression<Func<T, bool>> Criteria { get; }
List<Expression<Func<T, object>>> Includes { get; }
List<Func<IQueryable<T>, IIncludableQueryable<T, object>>> Includes { get; }
List<string> IncludeStrings { get; }
Expression<Func<T, bool>> And(Expression<Func<T, bool>> query);
Expression<Func<T, bool>> Or(Expression<Func<T, bool>> query);
Expand Down
5 changes: 3 additions & 2 deletions src/Application/Common/Specification/Specification.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T> : ISpecification<T> where T : class, IEntity
{
public Expression<Func<T, bool>> Criteria { get; set; }
public List<Expression<Func<T, object>>> Includes { get; } = new();
public List<Func<IQueryable<T>, IIncludableQueryable<T, object>>> Includes { get; } = new();
public List<string> IncludeStrings { get; } = new();

protected virtual void AddInclude(Expression<Func<T, object>> includeExpression)
protected virtual void AddInclude(Func<IQueryable<T>, IIncludableQueryable<T, object>> includeExpression)
{
Includes.Add(includeExpression);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Application/Features/Documents/DTOs/DocumentDto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ internal class DocumentsQuery : Specification<Document>
{
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))
Expand Down
7 changes: 7 additions & 0 deletions src/Application/Features/Identity/Dto/ApplicationUserDto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ApplicationUser>
{
public void Mapping(Profile profile)
{
profile.CreateMap<ApplicationUser, ApplicationUserDto>(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; }
Expand Down
4 changes: 2 additions & 2 deletions src/Blazor.Server.UI/Pages/Documents/Documents.razor
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,8 @@
</MudTd>
<MudTd DataLabel="@L["Owner Name"]">
<div class="d-flex flex-column">
<MudText>@context.Owner.DisplayName</MudText>
<MudText Typo="Typo.body2">@context.Owner.Email</MudText>
<MudText>@context.Owner?.DisplayName</MudText>
<MudText Typo="Typo.body2">@context.Owner?.Email</MudText>
</div>
</MudTd>
<MudTd DataLabel="@L["Tenant Name"]">
Expand Down