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