Skip to content

Commit 189ad87

Browse files
committed
Refactoring (extract interface)
SVN: trunk@5067
1 parent e2c8ec1 commit 189ad87

File tree

5 files changed

+52
-40
lines changed

5 files changed

+52
-40
lines changed

src/NHibernate/Linq/Functions/FunctionRegistry.cs

Lines changed: 27 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using System;
2-
using System.Collections.Generic;
1+
using System.Collections.Generic;
32
using System.Collections.ObjectModel;
43
using System.Linq;
54
using System.Linq.Expressions;
@@ -9,8 +8,8 @@
98

109
namespace NHibernate.Linq.Functions
1110
{
12-
public class FunctionRegistry
13-
{
11+
public class FunctionRegistry : ILinqToHqlGeneratorsRegistry
12+
{
1413
public static readonly FunctionRegistry Instance = new FunctionRegistry();
1514

1615
private readonly Dictionary<MethodInfo, IHqlGeneratorForMethod> registeredMethods = new Dictionary<MethodInfo, IHqlGeneratorForMethod>();
@@ -26,36 +25,6 @@ private FunctionRegistry()
2625
Register(new ICollectionGenerator());
2726
}
2827

29-
public IHqlGeneratorForMethod GetGenerator(MethodInfo method)
30-
{
31-
IHqlGeneratorForMethod methodGenerator;
32-
33-
if (!TryGetMethodGenerator(method, out methodGenerator))
34-
{
35-
throw new NotSupportedException(method.ToString());
36-
}
37-
38-
return methodGenerator;
39-
}
40-
41-
public bool TryGetMethodGenerator(MethodInfo method, out IHqlGeneratorForMethod methodGenerator)
42-
{
43-
if (method.IsGenericMethod)
44-
{
45-
method = method.GetGenericMethodDefinition();
46-
}
47-
48-
if (GetRegisteredMethodGenerator(method, out methodGenerator)) return true;
49-
50-
// No method generator registered. Look to see if it's a standard LinqExtensionMethod
51-
if (GetStandardLinqExtensionMethodGenerator(method, out methodGenerator)) return true;
52-
53-
// Not that either. Let's query each type generator to see if it can handle it
54-
if (GetMethodGeneratorForType(method, out methodGenerator)) return true;
55-
56-
return false;
57-
}
58-
5928
private bool GetMethodGeneratorForType(MethodInfo method, out IHqlGeneratorForMethod methodGenerator)
6029
{
6130
methodGenerator = null;
@@ -105,7 +74,30 @@ public IHqlGeneratorForProperty GetGenerator(MemberInfo member)
10574
return null;
10675
}
10776

108-
public void RegisterGenerator(MethodInfo method, IHqlGeneratorForMethod generator)
77+
public bool TryGetGenerator(MethodInfo method, out IHqlGeneratorForMethod generator)
78+
{
79+
if (method.IsGenericMethod)
80+
{
81+
method = method.GetGenericMethodDefinition();
82+
}
83+
84+
if (GetRegisteredMethodGenerator(method, out generator)) return true;
85+
86+
// No method generator registered. Look to see if it's a standard LinqExtensionMethod
87+
if (GetStandardLinqExtensionMethodGenerator(method, out generator)) return true;
88+
89+
// Not that either. Let's query each type generator to see if it can handle it
90+
if (GetMethodGeneratorForType(method, out generator)) return true;
91+
92+
return false;
93+
}
94+
95+
public bool TryGetGenerator(MemberInfo property, out IHqlGeneratorForProperty generator)
96+
{
97+
return registeredProperties.TryGetValue(property, out generator);
98+
}
99+
100+
public void RegisterGenerator(MethodInfo method, IHqlGeneratorForMethod generator)
109101
{
110102
registeredMethods.Add(method, generator);
111103
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using System.Reflection;
2+
3+
namespace NHibernate.Linq.Functions
4+
{
5+
public interface ILinqToHqlGeneratorsRegistry
6+
{
7+
bool TryGetGenerator(MethodInfo method, out IHqlGeneratorForMethod generator);
8+
bool TryGetGenerator(MemberInfo property, out IHqlGeneratorForProperty generator);
9+
void RegisterGenerator(MethodInfo method, IHqlGeneratorForMethod generator);
10+
void RegisterGenerator(MemberInfo property, IHqlGeneratorForProperty generator);
11+
}
12+
}

src/NHibernate/Linq/Visitors/HqlGeneratorExpressionTreeVisitor.cs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -351,9 +351,10 @@ protected HqlTreeNode VisitMemberExpression(MemberExpression expression)
351351
}
352352

353353
// Look for "special" properties (DateTime.Month etc)
354-
var generator = FunctionRegistry.GetGenerator(expression.Member);
354+
IHqlGeneratorForProperty generator;
355+
generator = FunctionRegistry.GetGenerator(expression.Member);
355356

356-
if (generator != null)
357+
if (generator != null)
357358
{
358359
return generator.BuildHql(expression.Member, expression.Expression, _hqlTreeBuilder, this);
359360
}
@@ -394,9 +395,15 @@ protected HqlTreeNode VisitConstantExpression(ConstantExpression expression)
394395

395396
protected HqlTreeNode VisitMethodCallExpression(MethodCallExpression expression)
396397
{
397-
var generator = FunctionRegistry.GetGenerator(expression.Method);
398+
IHqlGeneratorForMethod generator;
398399

399-
return generator.BuildHql(expression.Method, expression.Object, expression.Arguments, _hqlTreeBuilder, this);
400+
var method = expression.Method;
401+
if (!FunctionRegistry.TryGetGenerator(method, out generator))
402+
{
403+
throw new NotSupportedException(method.ToString());
404+
}
405+
406+
return generator.BuildHql(method, expression.Object, expression.Arguments, _hqlTreeBuilder, this);
400407
}
401408

402409
protected HqlTreeNode VisitLambdaExpression(LambdaExpression expression)

src/NHibernate/Linq/Visitors/SelectClauseVisitor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ private static bool CanBeEvaluatedInHqlSelectStatement(Expression expression)
8282
{
8383
// Depends if it's in the function registry
8484
IHqlGeneratorForMethod methodGenerator;
85-
if (!FunctionRegistry.TryGetMethodGenerator(((MethodCallExpression) expression).Method, out methodGenerator))
85+
if (!FunctionRegistry.TryGetGenerator(((MethodCallExpression) expression).Method, out methodGenerator))
8686
{
8787
return false;
8888
}

src/NHibernate/NHibernate.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -654,6 +654,7 @@
654654
<Compile Include="Impl\SessionIdLoggingContext.cs" />
655655
<Compile Include="Linq\Expressions\AggregateExpressionNode.cs" />
656656
<Compile Include="Linq\EagerFetchingExtensionMethods.cs" />
657+
<Compile Include="Linq\Functions\ILinqToHqlGeneratorsRegistry.cs" />
657658
<Compile Include="Linq\LinqExtensionMethodAttribute.cs" />
658659
<Compile Include="Linq\TypeHelperExtensionMethods.cs" />
659660
<Compile Include="Linq\Visitors\NameUnNamedParameters.cs" />

0 commit comments

Comments
 (0)