| | 1 | | using Microsoft.EntityFrameworkCore.Query; |
| | 2 | | using System.Collections; |
| | 3 | | using System.Collections.Concurrent; |
| | 4 | | using System.Diagnostics; |
| | 5 | | using System.Reflection; |
| | 6 | |
|
| | 7 | | namespace Pozitron.QuerySpecification; |
| | 8 | |
|
| | 9 | | /// <summary> |
| | 10 | | /// Evaluates a specification to include navigation properties. |
| | 11 | | /// </summary> |
| | 12 | | public sealed class IncludeEvaluator : IEvaluator |
| | 13 | | { |
| 1 | 14 | | private static readonly MethodInfo _includeMethodInfo = typeof(EntityFrameworkQueryableExtensions) |
| 1 | 15 | | .GetTypeInfo().GetDeclaredMethods(nameof(EntityFrameworkQueryableExtensions.Include)) |
| 3 | 16 | | .Single(mi => mi.IsPublic && mi.GetGenericArguments().Length == 2 |
| 3 | 17 | | && mi.GetParameters()[0].ParameterType.GetGenericTypeDefinition() == typeof(IQueryable<>) |
| 3 | 18 | | && mi.GetParameters()[1].ParameterType.GetGenericTypeDefinition() == typeof(Expression<>)); |
| | 19 | |
|
| 1 | 20 | | private static readonly MethodInfo _thenIncludeAfterReferenceMethodInfo |
| 1 | 21 | | = typeof(EntityFrameworkQueryableExtensions) |
| 1 | 22 | | .GetTypeInfo().GetDeclaredMethods(nameof(EntityFrameworkQueryableExtensions.ThenInclude)) |
| 3 | 23 | | .Single(mi => mi.IsPublic && mi.GetGenericArguments().Length == 3 |
| 3 | 24 | | && mi.GetParameters()[0].ParameterType.GenericTypeArguments[1].IsGenericParameter |
| 3 | 25 | | && mi.GetParameters()[0].ParameterType.GetGenericTypeDefinition() == typeof(IIncludableQueryable<,>) |
| 3 | 26 | | && mi.GetParameters()[1].ParameterType.GetGenericTypeDefinition() == typeof(Expression<>)); |
| | 27 | |
|
| 1 | 28 | | private static readonly MethodInfo _thenIncludeAfterEnumerableMethodInfo |
| 1 | 29 | | = typeof(EntityFrameworkQueryableExtensions) |
| 1 | 30 | | .GetTypeInfo().GetDeclaredMethods(nameof(EntityFrameworkQueryableExtensions.ThenInclude)) |
| 3 | 31 | | .Single(mi => mi.IsPublic && mi.GetGenericArguments().Length == 3 |
| 3 | 32 | | && !mi.GetParameters()[0].ParameterType.GenericTypeArguments[1].IsGenericParameter |
| 3 | 33 | | && mi.GetParameters()[0].ParameterType.GetGenericTypeDefinition() == typeof(IIncludableQueryable<,>) |
| 3 | 34 | | && mi.GetParameters()[1].ParameterType.GetGenericTypeDefinition() == typeof(Expression<>)); |
| | 35 | |
|
| 131 | 36 | | private readonly record struct CacheKey(Type EntityType, Type PropertyType, Type? PreviousReturnType); |
| 1 | 37 | | private static readonly ConcurrentDictionary<CacheKey, Func<IQueryable, LambdaExpression, IQueryable>> _cache = new( |
| | 38 | |
|
| | 39 | |
|
| | 40 | | /// <summary> |
| | 41 | | /// Gets the singleton instance of the <see cref="IncludeEvaluator"/> class. |
| | 42 | | /// </summary> |
| 1 | 43 | | public static IncludeEvaluator Instance = new(); |
| 2 | 44 | | private IncludeEvaluator() { } |
| | 45 | |
|
| | 46 | | /// <inheritdoc/> |
| | 47 | | public IQueryable<T> Evaluate<T>(IQueryable<T> source, Specification<T> specification) where T : class |
| | 48 | | { |
| 52 | 49 | | Type? previousReturnType = null; |
| 608 | 50 | | foreach (var item in specification.Items) |
| | 51 | | { |
| 252 | 52 | | if (item.Type == ItemType.Include && item.Reference is LambdaExpression expr) |
| | 53 | | { |
| 59 | 54 | | if (item.Bag == (int)IncludeType.Include) |
| | 55 | | { |
| 27 | 56 | | var key = new CacheKey(typeof(T), expr.ReturnType, null); |
| 27 | 57 | | previousReturnType = expr.ReturnType; |
| 27 | 58 | | var include = _cache.GetOrAdd(key, CreateIncludeDelegate); |
| 27 | 59 | | source = (IQueryable<T>)include(source, expr); |
| | 60 | | } |
| 32 | 61 | | else if (item.Bag == (int)IncludeType.ThenInclude) |
| | 62 | | { |
| 32 | 63 | | var key = new CacheKey(typeof(T), expr.ReturnType, previousReturnType); |
| 32 | 64 | | previousReturnType = expr.ReturnType; |
| 32 | 65 | | var include = _cache.GetOrAdd(key, CreateThenIncludeDelegate); |
| 32 | 66 | | source = (IQueryable<T>)include(source, expr); |
| | 67 | | } |
| | 68 | | } |
| | 69 | | } |
| | 70 | |
|
| 52 | 71 | | return source; |
| | 72 | | } |
| | 73 | |
|
| | 74 | | private static Func<IQueryable, LambdaExpression, IQueryable> CreateIncludeDelegate(CacheKey cacheKey) |
| | 75 | | { |
| 7 | 76 | | var includeMethod = _includeMethodInfo.MakeGenericMethod(cacheKey.EntityType, cacheKey.PropertyType); |
| 7 | 77 | | var sourceParameter = Expression.Parameter(typeof(IQueryable)); |
| 7 | 78 | | var selectorParameter = Expression.Parameter(typeof(LambdaExpression)); |
| | 79 | |
|
| 7 | 80 | | var call = Expression.Call( |
| 7 | 81 | | includeMethod, |
| 7 | 82 | | Expression.Convert(sourceParameter, typeof(IQueryable<>).MakeGenericType(cacheKey.EntityType)), |
| 7 | 83 | | Expression.Convert(selectorParameter, typeof(Expression<>).MakeGenericType(typeof(Func<,>).MakeGenericType |
| | 84 | |
|
| 7 | 85 | | var lambda = Expression.Lambda<Func<IQueryable, LambdaExpression, IQueryable>>(call, sourceParameter, selectorPa |
| 7 | 86 | | return lambda.Compile(); |
| | 87 | | } |
| | 88 | |
|
| | 89 | | private static Func<IQueryable, LambdaExpression, IQueryable> CreateThenIncludeDelegate(CacheKey cacheKey) |
| | 90 | | { |
| | 91 | | Debug.Assert(cacheKey.PreviousReturnType is not null); |
| | 92 | |
|
| 16 | 93 | | var thenIncludeInfo = IsGenericEnumerable(cacheKey.PreviousReturnType, out var previousPropertyType) |
| 16 | 94 | | ? _thenIncludeAfterEnumerableMethodInfo |
| 16 | 95 | | : _thenIncludeAfterReferenceMethodInfo; |
| | 96 | |
|
| 16 | 97 | | var thenIncludeMethod = thenIncludeInfo.MakeGenericMethod(cacheKey.EntityType, previousPropertyType, cacheKey.Pr |
| 16 | 98 | | var sourceParameter = Expression.Parameter(typeof(IQueryable)); |
| 16 | 99 | | var selectorParameter = Expression.Parameter(typeof(LambdaExpression)); |
| | 100 | |
|
| 16 | 101 | | var call = Expression.Call( |
| 16 | 102 | | thenIncludeMethod, |
| 16 | 103 | | // We must pass cacheKey.PreviousReturnType. It must be exact type, not the generic type argument |
| 16 | 104 | | Expression.Convert(sourceParameter, typeof(IIncludableQueryable<,>).MakeGenericType(cacheKey.EntityType, |
| 16 | 105 | | Expression.Convert(selectorParameter, typeof(Expression<>).MakeGenericType(typeof(Func<,>).MakeGenericTy |
| | 106 | |
|
| 16 | 107 | | var lambda = Expression.Lambda<Func<IQueryable, LambdaExpression, IQueryable>>(call, sourceParameter, selectorPa |
| 16 | 108 | | return lambda.Compile(); |
| | 109 | | } |
| | 110 | |
|
| | 111 | | private static bool IsGenericEnumerable(Type type, out Type propertyType) |
| | 112 | | { |
| 16 | 113 | | if (type.IsGenericType && typeof(IEnumerable).IsAssignableFrom(type)) |
| | 114 | | { |
| 9 | 115 | | propertyType = type.GenericTypeArguments[0]; |
| 9 | 116 | | return true; |
| | 117 | | } |
| | 118 | |
|
| 7 | 119 | | propertyType = type; |
| 7 | 120 | | return false; |
| | 121 | | } |
| | 122 | | } |