Skip to content

Fixed inefficient cache usage. #694

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

Merged
merged 2 commits into from
Feb 21, 2020
Merged
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
31 changes: 28 additions & 3 deletions src/JsonApiDotNetCore/Extensions/IQueryableExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ private static IQueryable<TSource> CallGenericWhereMethod<TSource>(IQueryable<TS
PropertyInfo relationProperty = null;
PropertyInfo property = null;
MemberExpression left;
ConstantExpression right;
Expression right;

// {model}
var parameter = Expression.Parameter(concreteType, "model");
Expand Down Expand Up @@ -287,8 +287,8 @@ private static IQueryable<TSource> CallGenericWhereMethod<TSource>(IQueryable<TS
// convert the incoming value to the target value type
// "1" -> 1
var convertedValue = TypeHelper.ConvertType(filter.Value, property.PropertyType);
// {1}
right = Expression.Constant(convertedValue, property.PropertyType);

right = CreateTupleAccessForConstantExpression(convertedValue, property.PropertyType);
}

var body = GetFilterExpressionLambda(left, right, filter.Operation);
Expand All @@ -302,6 +302,31 @@ private static IQueryable<TSource> CallGenericWhereMethod<TSource>(IQueryable<TS
}
}

private static Expression CreateTupleAccessForConstantExpression(object value, Type type)
{
// To enable efficient query plan caching, inline constants (that vary per request) should be converted into query parameters.
// https://stackoverflow.com/questions/54075758/building-a-parameterized-entityframework-core-expression

// This method can be used to change a query like:
// SELECT ... FROM ... WHERE x."Age" = 3
// into:
// SELECT ... FROM ... WHERE x."Age" = @p0

// The code below builds the next expression for a type T that is unknown at compile time:
// Expression.Property(Expression.Constant(Tuple.Create<T>(value)), "Item1")
// Which represents the next C# code:
// Tuple.Create<T>(value).Item1;

MethodInfo tupleCreateMethod = typeof(Tuple).GetMethods()
.Single(m => m.Name == "Create" && m.IsGenericMethod && m.GetGenericArguments().Length == 1);
MethodInfo constructedTupleCreateMethod = tupleCreateMethod.MakeGenericMethod(type);

ConstantExpression constantExpression = Expression.Constant(value, type);

MethodCallExpression tupleCreateCall = Expression.Call(constructedTupleCreateMethod, constantExpression);
return Expression.Property(tupleCreateCall, "Item1");
}

private static IQueryable<TSource> CallGenericSelectMethod<TSource>(IQueryable<TSource> source, List<string> columns)
{
var sourceBindings = new List<MemberAssignment>();
Expand Down