| | 1 | | using Microsoft.EntityFrameworkCore.Query; |
| | 2 | | using System.Diagnostics; |
| | 3 | | using System.Reflection; |
| | 4 | |
|
| | 5 | | namespace Pozitron.QuerySpecification; |
| | 6 | |
|
| | 7 | | public class IncludeEvaluator : IEvaluator |
| | 8 | | { |
| 1 | 9 | | private static readonly MethodInfo _includeMethodInfo = typeof(EntityFrameworkQueryableExtensions) |
| 1 | 10 | | .GetTypeInfo().GetDeclaredMethods(nameof(EntityFrameworkQueryableExtensions.Include)) |
| 3 | 11 | | .Single(mi => mi.IsPublic && mi.GetGenericArguments().Length == 2 |
| 3 | 12 | | && mi.GetParameters()[0].ParameterType.GetGenericTypeDefinition() == typeof(IQueryable<>) |
| 3 | 13 | | && mi.GetParameters()[1].ParameterType.GetGenericTypeDefinition() == typeof(Expression<>)); |
| | 14 | |
|
| 1 | 15 | | private static readonly MethodInfo _thenIncludeAfterReferenceMethodInfo |
| 1 | 16 | | = typeof(EntityFrameworkQueryableExtensions) |
| 1 | 17 | | .GetTypeInfo().GetDeclaredMethods(nameof(EntityFrameworkQueryableExtensions.ThenInclude)) |
| 3 | 18 | | .Single(mi => mi.IsPublic && mi.GetGenericArguments().Length == 3 |
| 3 | 19 | | && mi.GetParameters()[0].ParameterType.GenericTypeArguments[1].IsGenericParameter |
| 3 | 20 | | && mi.GetParameters()[0].ParameterType.GetGenericTypeDefinition() == typeof(IIncludableQueryable<,>) |
| 3 | 21 | | && mi.GetParameters()[1].ParameterType.GetGenericTypeDefinition() == typeof(Expression<>)); |
| | 22 | |
|
| 1 | 23 | | private static readonly MethodInfo _thenIncludeAfterEnumerableMethodInfo |
| 1 | 24 | | = typeof(EntityFrameworkQueryableExtensions) |
| 1 | 25 | | .GetTypeInfo().GetDeclaredMethods(nameof(EntityFrameworkQueryableExtensions.ThenInclude)) |
| 3 | 26 | | .Single(mi => mi.IsPublic && mi.GetGenericArguments().Length == 3 |
| 3 | 27 | | && !mi.GetParameters()[0].ParameterType.GenericTypeArguments[1].IsGenericParameter |
| 3 | 28 | | && mi.GetParameters()[0].ParameterType.GetGenericTypeDefinition() == typeof(IIncludableQueryable<,>) |
| 3 | 29 | | && mi.GetParameters()[1].ParameterType.GetGenericTypeDefinition() == typeof(Expression<>)); |
| | 30 | |
|
| 2 | 31 | | private IncludeEvaluator() { } |
| 1 | 32 | | public static IncludeEvaluator Instance = new(); |
| | 33 | |
|
| | 34 | | public IQueryable<T> Evaluate<T>(IQueryable<T> source, Specification<T> specification) where T : class |
| | 35 | | { |
| 120 | 36 | | foreach (var includeString in specification.IncludeStrings) |
| | 37 | | { |
| 9 | 38 | | source = source.Include(includeString); |
| | 39 | | } |
| | 40 | |
|
| 220 | 41 | | foreach (var includeExpression in specification.IncludeExpressions) |
| | 42 | | { |
| 59 | 43 | | if (includeExpression.Type == IncludeTypeEnum.Include) |
| | 44 | | { |
| 27 | 45 | | source = BuildInclude<T>(source, includeExpression); |
| | 46 | | } |
| 32 | 47 | | else if (includeExpression.Type == IncludeTypeEnum.ThenInclude) |
| | 48 | | { |
| 32 | 49 | | source = BuildThenInclude<T>(source, includeExpression); |
| | 50 | | } |
| | 51 | | } |
| | 52 | |
|
| 51 | 53 | | return source; |
| | 54 | | } |
| | 55 | |
|
| | 56 | | private static IQueryable<T> BuildInclude<T>(IQueryable source, IncludeExpression includeExpression) |
| | 57 | | { |
| | 58 | | Debug.Assert(includeExpression is not null); |
| | 59 | |
|
| 27 | 60 | | var result = _includeMethodInfo |
| 27 | 61 | | .MakeGenericMethod(includeExpression.EntityType, includeExpression.PropertyType) |
| 27 | 62 | | .Invoke(null, [source, includeExpression.LambdaExpression]); |
| | 63 | |
|
| | 64 | | Debug.Assert(result is not null); |
| | 65 | |
|
| 27 | 66 | | return (IQueryable<T>)result; |
| | 67 | | } |
| | 68 | |
|
| | 69 | | private static IQueryable<T> BuildThenInclude<T>(IQueryable source, IncludeExpression includeExpression) |
| | 70 | | { |
| | 71 | | Debug.Assert(includeExpression is not null); |
| | 72 | | Debug.Assert(includeExpression.PreviousPropertyType is not null); |
| | 73 | |
|
| 32 | 74 | | var result = (IsGenericEnumerable(includeExpression.PreviousPropertyType, out var previousPropertyType) |
| 32 | 75 | | ? _thenIncludeAfterEnumerableMethodInfo |
| 32 | 76 | | : _thenIncludeAfterReferenceMethodInfo) |
| 32 | 77 | | .MakeGenericMethod(includeExpression.EntityType, previousPropertyType, includeExpression.PropertyType) |
| 32 | 78 | | .Invoke(null, [source, includeExpression.LambdaExpression]); |
| | 79 | |
|
| | 80 | | Debug.Assert(result is not null); |
| | 81 | |
|
| 32 | 82 | | return (IQueryable<T>)result; |
| | 83 | | } |
| | 84 | |
|
| | 85 | | private static bool IsGenericEnumerable(Type type, out Type propertyType) |
| | 86 | | { |
| 32 | 87 | | if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(IEnumerable<>)) |
| | 88 | | { |
| 17 | 89 | | propertyType = type.GenericTypeArguments[0]; |
| 17 | 90 | | return true; |
| | 91 | | } |
| | 92 | |
|
| 15 | 93 | | propertyType = type; |
| 15 | 94 | | return false; |
| | 95 | | } |
| | 96 | | } |