Skip to content

Commit 79d923b

Browse files
NH-3964 - normalize linq reflection.
1 parent 0f88aa1 commit 79d923b

File tree

5 files changed

+62
-43
lines changed

5 files changed

+62
-43
lines changed

src/NHibernate/Linq/DefaultQueryProvider.cs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -79,17 +79,13 @@ protected virtual NhLinqExpression PrepareQuery(Expression expression, out IQuer
7979
return nhLinqExpression;
8080
}
8181

82+
private static readonly MethodInfo Future = ReflectionHelper.GetMethodDefinition<IQuery>(q => q.Future<object>());
83+
private static readonly MethodInfo FutureValue = ReflectionHelper.GetMethodDefinition<IQuery>(q => q.FutureValue<object>());
84+
8285
protected virtual object ExecuteFutureQuery(NhLinqExpression nhLinqExpression, IQuery query, NhLinqExpression nhQuery)
8386
{
84-
MethodInfo method;
85-
if (nhLinqExpression.ReturnType == NhLinqExpressionReturnType.Sequence)
86-
{
87-
method = typeof (IQuery).GetMethod("Future").MakeGenericMethod(nhQuery.Type);
88-
}
89-
else
90-
{
91-
method = typeof (IQuery).GetMethod("FutureValue").MakeGenericMethod(nhQuery.Type);
92-
}
87+
var method = (nhLinqExpression.ReturnType == NhLinqExpressionReturnType.Sequence ? Future : FutureValue)
88+
.MakeGenericMethod(nhQuery.Type);
9389

9490
object result = method.Invoke(query, new object[0]);
9591

src/NHibernate/Linq/NhRelinqQueryParser.cs

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,18 +53,26 @@ public NHibernateNodeTypeProvider()
5353
{
5454
var methodInfoRegistry = new MethodInfoBasedNodeTypeRegistry();
5555

56-
methodInfoRegistry.Register(new[] { typeof(EagerFetchingExtensionMethods).GetMethod("Fetch") }, typeof(FetchOneExpressionNode));
57-
methodInfoRegistry.Register(new[] { typeof(EagerFetchingExtensionMethods).GetMethod("FetchMany") }, typeof(FetchManyExpressionNode));
58-
methodInfoRegistry.Register(new[] { typeof(EagerFetchingExtensionMethods).GetMethod("ThenFetch") }, typeof(ThenFetchOneExpressionNode));
59-
methodInfoRegistry.Register(new[] { typeof(EagerFetchingExtensionMethods).GetMethod("ThenFetchMany") }, typeof(ThenFetchManyExpressionNode));
56+
methodInfoRegistry.Register(
57+
new[] { ReflectionHelper.GetMethodDefinition(() => EagerFetchingExtensionMethods.Fetch<object, object>(null, null)) },
58+
typeof(FetchOneExpressionNode));
59+
methodInfoRegistry.Register(
60+
new[] { ReflectionHelper.GetMethodDefinition(() => EagerFetchingExtensionMethods.FetchMany<object, object>(null, null)) },
61+
typeof(FetchManyExpressionNode));
62+
methodInfoRegistry.Register(
63+
new[] { ReflectionHelper.GetMethodDefinition(() => EagerFetchingExtensionMethods.ThenFetch<object, object, object>(null, null)) },
64+
typeof(ThenFetchOneExpressionNode));
65+
methodInfoRegistry.Register(
66+
new[] { ReflectionHelper.GetMethodDefinition(() => EagerFetchingExtensionMethods.ThenFetchMany<object, object, object>(null, null)) },
67+
typeof(ThenFetchManyExpressionNode));
6068

6169
methodInfoRegistry.Register(
6270
new[]
63-
{
64-
typeof(LinqExtensionMethods).GetMethod("Cacheable"),
65-
typeof(LinqExtensionMethods).GetMethod("CacheMode"),
66-
typeof(LinqExtensionMethods).GetMethod("CacheRegion"),
67-
}, typeof(CacheableExpressionNode));
71+
{
72+
ReflectionHelper.GetMethodDefinition(() => LinqExtensionMethods.Cacheable<object>(null)),
73+
ReflectionHelper.GetMethodDefinition(() => LinqExtensionMethods.CacheMode<object>(null, CacheMode.Normal)),
74+
ReflectionHelper.GetMethodDefinition(() => LinqExtensionMethods.CacheRegion<object>(null, null)),
75+
}, typeof(CacheableExpressionNode));
6876

6977
methodInfoRegistry.Register(
7078
new[]

src/NHibernate/Linq/Visitors/ExpressionParameterVisitor.cs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,19 @@ public class ExpressionParameterVisitor : ExpressionTreeVisitor
1818
private readonly Dictionary<ConstantExpression, NamedParameter> _parameters = new Dictionary<ConstantExpression, NamedParameter>();
1919
private readonly ISessionFactoryImplementor _sessionFactory;
2020

21+
private static readonly MethodInfo QueryableSkipDefinition =
22+
ReflectionHelper.GetMethodDefinition(() => Queryable.Skip<object>(null, 0));
23+
private static readonly MethodInfo QueryableTakeDefinition =
24+
ReflectionHelper.GetMethodDefinition(() => Queryable.Take<object>(null, 0));
25+
private static readonly MethodInfo EnumerableSkipDefinition =
26+
ReflectionHelper.GetMethodDefinition(() => Enumerable.Skip<object>(null, 0));
27+
private static readonly MethodInfo EnumerableTakeDefinition =
28+
ReflectionHelper.GetMethodDefinition(() => Enumerable.Take<object>(null, 0));
29+
2130
private readonly ICollection<MethodBase> _pagingMethods = new HashSet<MethodBase>
2231
{
23-
ReflectionHelper.GetMethodDefinition(() => Queryable.Skip<object>(null, 0)),
24-
ReflectionHelper.GetMethodDefinition(() => Queryable.Take<object>(null, 0)),
25-
ReflectionHelper.GetMethodDefinition(() => Enumerable.Skip<object>(null, 0)),
26-
ReflectionHelper.GetMethodDefinition(() => Enumerable.Take<object>(null, 0)),
32+
QueryableSkipDefinition, QueryableTakeDefinition,
33+
EnumerableSkipDefinition, EnumerableTakeDefinition
2734
};
2835

2936
public ExpressionParameterVisitor(ISessionFactoryImplementor sessionFactory)
Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,23 @@
11
using System.Linq;
2+
using System.Reflection;
23
using Remotion.Linq.Clauses.ResultOperators;
34

45
namespace NHibernate.Linq.Visitors.ResultOperatorProcessors
56
{
6-
public class ProcessFirst : ProcessFirstOrSingleBase, IResultOperatorProcessor<FirstResultOperator>
7-
{
8-
public void Process(FirstResultOperator resultOperator, QueryModelVisitor queryModelVisitor, IntermediateHqlTree tree)
9-
{
10-
var firstMethod = resultOperator.ReturnDefaultWhenEmpty
11-
? ReflectionHelper.GetMethodDefinition(() => Queryable.FirstOrDefault<object>(null))
12-
: ReflectionHelper.GetMethodDefinition(() => Queryable.First<object>(null));
7+
public class ProcessFirst : ProcessFirstOrSingleBase, IResultOperatorProcessor<FirstResultOperator>
8+
{
9+
private static readonly MethodInfo FirstOrDefault =
10+
ReflectionHelper.GetMethodDefinition(() => Queryable.FirstOrDefault<object>(null));
11+
private static readonly MethodInfo First =
12+
ReflectionHelper.GetMethodDefinition(() => Queryable.First<object>(null));
1313

14-
AddClientSideEval(firstMethod, queryModelVisitor, tree);
14+
public void Process(FirstResultOperator resultOperator, QueryModelVisitor queryModelVisitor, IntermediateHqlTree tree)
15+
{
16+
var firstMethod = resultOperator.ReturnDefaultWhenEmpty ? FirstOrDefault : First;
1517

16-
tree.AddTakeClause(tree.TreeBuilder.Constant(1));
17-
}
18-
}
18+
AddClientSideEval(firstMethod, queryModelVisitor, tree);
19+
20+
tree.AddTakeClause(tree.TreeBuilder.Constant(1));
21+
}
22+
}
1923
}
Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
11
using System.Linq;
2+
using System.Reflection;
23
using Remotion.Linq.Clauses.ResultOperators;
34

45
namespace NHibernate.Linq.Visitors.ResultOperatorProcessors
56
{
6-
public class ProcessSingle : ProcessFirstOrSingleBase, IResultOperatorProcessor<SingleResultOperator>
7-
{
8-
public void Process(SingleResultOperator resultOperator, QueryModelVisitor queryModelVisitor, IntermediateHqlTree tree)
9-
{
10-
var firstMethod = resultOperator.ReturnDefaultWhenEmpty
11-
? ReflectionHelper.GetMethodDefinition(() => Queryable.SingleOrDefault<object>(null))
12-
: ReflectionHelper.GetMethodDefinition(() => Queryable.Single<object>(null));
7+
public class ProcessSingle : ProcessFirstOrSingleBase, IResultOperatorProcessor<SingleResultOperator>
8+
{
9+
private static readonly MethodInfo SingleOrDefault =
10+
ReflectionHelper.GetMethodDefinition(() => Queryable.SingleOrDefault<object>(null));
11+
private static readonly MethodInfo Single =
12+
ReflectionHelper.GetMethodDefinition(() => Queryable.Single<object>(null));
1313

14-
AddClientSideEval(firstMethod, queryModelVisitor, tree);
15-
}
16-
}
14+
public void Process(SingleResultOperator resultOperator, QueryModelVisitor queryModelVisitor, IntermediateHqlTree tree)
15+
{
16+
var firstMethod = resultOperator.ReturnDefaultWhenEmpty ? SingleOrDefault : Single;
17+
18+
AddClientSideEval(firstMethod, queryModelVisitor, tree);
19+
}
20+
}
1721
}

0 commit comments

Comments
 (0)