This repository has been archived by the owner on May 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added possibility to chain includes.
- Loading branch information
Showing
6 changed files
with
131 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq.Expressions; | ||
|
||
namespace Microsoft.eShopWeb.ApplicationCore.Helpers.Query | ||
{ | ||
public class IncludeAggregator<TEntity> | ||
{ | ||
public IncludeQuery<TEntity, TProperty> Include<TProperty>(Expression<Func<TEntity, TProperty>> selector) | ||
{ | ||
var visitor = new IncludeVisitor(); | ||
visitor.Visit(selector); | ||
|
||
var id = Guid.NewGuid(); | ||
var pathMap = new Dictionary<Guid, string>() { { id, visitor.Path } }; | ||
|
||
return new IncludeQuery<TEntity, TProperty>(id, pathMap); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
using Microsoft.eShopWeb.ApplicationCore.Interfaces; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Linq.Expressions; | ||
|
||
namespace Microsoft.eShopWeb.ApplicationCore.Helpers.Query | ||
{ | ||
public class IncludeQuery<TEntity> : IIncludeQuery<TEntity> | ||
{ | ||
public Guid Id { get; } | ||
public Dictionary<Guid, string> PathMap { get; } = new Dictionary<Guid, string>(); | ||
public IncludeVisitor Visitor { get; } = new IncludeVisitor(); | ||
|
||
public IncludeQuery(Guid id, Dictionary<Guid, string> pathMap) | ||
{ | ||
Id = id; | ||
PathMap = pathMap; | ||
} | ||
|
||
public IEnumerable<string> Paths => PathMap.Select(x => x.Value).Distinct().ToList(); | ||
} | ||
|
||
public class IncludeQuery<TEntity, TPreviousProperty> : IncludeQuery<TEntity>, IIncludeQuery<TEntity, TPreviousProperty> | ||
{ | ||
public IncludeQuery(Guid id, Dictionary<Guid, string> pathMap) : base(id, pathMap) { } | ||
} | ||
|
||
public static class IncludeableQueryExtensions | ||
{ | ||
public static IIncludeQuery<TEntity, TNewProperty> Include<TEntity, TPreviousProperty, TNewProperty>( | ||
this IIncludeQuery<TEntity, TPreviousProperty> query, | ||
Expression<Func<TEntity, TNewProperty>> selector) | ||
{ | ||
query.Visitor.Visit(selector); | ||
|
||
var id = Guid.NewGuid(); | ||
query.PathMap[id] = query.Visitor.Path; | ||
|
||
return new IncludeQuery<TEntity, TNewProperty>(id, query.PathMap); | ||
} | ||
|
||
public static IIncludeQuery<TEntity, TNewProperty> ThenInclude<TEntity, TPreviousProperty, TNewProperty>( | ||
this IIncludeQuery<TEntity, TPreviousProperty> query, | ||
Expression<Func<TPreviousProperty, TNewProperty>> selector) | ||
{ | ||
query.Visitor.Visit(selector); | ||
|
||
var existingPath = query.PathMap[query.Id]; | ||
query.PathMap[query.Id] = $"{existingPath}.{query.Visitor.Path}"; | ||
|
||
return new IncludeQuery<TEntity, TNewProperty>(query.Id, query.PathMap); | ||
} | ||
|
||
public static IIncludeQuery<TEntity, TNewProperty> ThenInclude<TEntity, TPreviousProperty, TNewProperty>( | ||
this IIncludeQuery<TEntity, IEnumerable<TPreviousProperty>> query, | ||
Expression<Func<TPreviousProperty, TNewProperty>> selector) | ||
{ | ||
query.Visitor.Visit(selector); | ||
|
||
var existingPath = query.PathMap[query.Id]; | ||
query.PathMap[query.Id] = $"{existingPath}.{query.Visitor.Path}"; | ||
|
||
return new IncludeQuery<TEntity, TNewProperty>(query.Id, query.PathMap); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
using System.Linq.Expressions; | ||
|
||
namespace Microsoft.eShopWeb.ApplicationCore.Helpers.Query | ||
{ | ||
public class IncludeVisitor : ExpressionVisitor | ||
{ | ||
public string Path { get; private set; } = string.Empty; | ||
|
||
protected override Expression VisitMember(MemberExpression node) | ||
{ | ||
Path = string.IsNullOrEmpty(Path) ? node.Member.Name : $"{Path}.{node.Member.Name}"; | ||
|
||
return base.VisitMember(node); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
using Microsoft.eShopWeb.ApplicationCore.Helpers.Query; | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace Microsoft.eShopWeb.ApplicationCore.Interfaces | ||
{ | ||
public interface IIncludeQuery<TEntity> | ||
{ | ||
Guid Id { get; } | ||
Dictionary<Guid, string> PathMap { get; } | ||
IncludeVisitor Visitor { get; } | ||
IEnumerable<string> Paths { get; } | ||
} | ||
|
||
public interface IIncludeQuery<TEntity, out TPreviousProperty> : IIncludeQuery<TEntity> | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters