Skip to content

Replace CreateTupleAccessExpressionForConstant with simpler implementation #1469

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 1 commit into from
Feb 18, 2024
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,9 @@ private Expression CreateLambdaBodyInitializerForTypeHierarchy(FieldSelection se
private static BinaryExpression CreateRuntimeTypeCheck(LambdaScope lambdaScope, Type concreteClrType)
{
// Emitting "resource.GetType() == typeof(Article)" instead of "resource is Article" so we don't need to check for most-derived
// types first. This way, we can fallback to "anything else" at the end without worrying about order.
// types first. This way, we can fall back to "anything else" at the end without worrying about order.

Expression concreteTypeConstant = concreteClrType.CreateTupleAccessExpressionForConstant(typeof(Type));
Expression concreteTypeConstant = SystemExpressionBuilder.CloseOver(concreteClrType);
MethodCallExpression getTypeCall = Expression.Call(lambdaScope.Accessor, TypeGetTypeMethod);

return Expression.MakeBinary(ExpressionType.Equal, getTypeCall, concreteTypeConstant, false, TypeOpEqualityMethod);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public override Expression VisitPagination(PaginationExpression expression, Quer

private static Expression ExtensionMethodCall(Expression source, string operationName, int value, QueryClauseBuilderContext context)
{
Expression constant = value.CreateTupleAccessExpressionForConstant(typeof(int));
Expression constant = SystemExpressionBuilder.CloseOver(value);

return Expression.Call(context.ExtensionType, operationName, [context.LambdaScope.Parameter.Type], source, constant);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,6 @@ public override Expression VisitNullConstant(NullConstantExpression expression,

public override Expression VisitLiteralConstant(LiteralConstantExpression expression, QueryClauseBuilderContext context)
{
Type type = expression.TypedValue.GetType();
return expression.TypedValue.CreateTupleAccessExpressionForConstant(type);
return SystemExpressionBuilder.CloseOver(expression.TypedValue);
}
}
34 changes: 34 additions & 0 deletions src/JsonApiDotNetCore/Queries/SystemExpressionBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System.Linq.Expressions;
using System.Reflection;

#pragma warning disable AV1008

namespace JsonApiDotNetCore.Queries;

internal static class SystemExpressionBuilder
{
private static readonly MethodInfo CloseOverOpenMethod =
typeof(SystemExpressionBuilder).GetMethods().Single(method => method is { Name: nameof(CloseOver), IsGenericMethod: true });

// 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
//
// CloseOver can be used to change a query like:
// SELECT ... FROM ... WHERE x."Age" = 3
// into:
// SELECT ... FROM ... WHERE x."Age" = @p0

public static Expression CloseOver(object value)
{
ArgumentGuard.NotNull(value);

MethodInfo closeOverClosedMethod = CloseOverOpenMethod.MakeGenericMethod(value.GetType());
return (Expression)closeOverClosedMethod.Invoke(null, [value])!;
}

public static Expression CloseOver<T>(T value)
{
// From https://github.com/dotnet/efcore/issues/28151#issuecomment-1374480257.
return ((Expression<Func<T>>)(() => value)).Body;
}
}
33 changes: 0 additions & 33 deletions src/JsonApiDotNetCore/Queries/SystemExpressionExtensions.cs

This file was deleted.

2 changes: 1 addition & 1 deletion src/JsonApiDotNetCore/Resources/ResourceFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public NewExpression CreateNewExpression(Type resourceClrType)
{
object constructorArgument = ActivatorUtilities.GetServiceOrCreateInstance(_serviceProvider, constructorParameter.ParameterType);

Expression argumentExpression = constructorArgument.CreateTupleAccessExpressionForConstant(constructorArgument.GetType());
Expression argumentExpression = SystemExpressionBuilder.CloseOver(constructorArgument);
constructorArguments.Add(argumentExpression);
}
#pragma warning disable AV1210 // Catch a specific exception instead of Exception, SystemException or ApplicationException
Expand Down