|
From: <fab...@us...> - 2010-07-28 22:19:27
|
Revision: 5078
http://nhibernate.svn.sourceforge.net/nhibernate/?rev=5078&view=rev
Author: fabiomaulo
Date: 2010-07-28 22:19:19 +0000 (Wed, 28 Jul 2010)
Log Message:
-----------
Refactoring (extracted StandardLinqExtensionMethodGenerator as an IHqlGeneratorForType)
Modified Paths:
--------------
trunk/nhibernate/src/NHibernate/Linq/Functions/DefaultLinqToHqlGeneratorsRegistry.cs
trunk/nhibernate/src/NHibernate/NHibernate.csproj
Added Paths:
-----------
trunk/nhibernate/src/NHibernate/Linq/Functions/StandardLinqExtensionMethodGenerator.cs
Modified: trunk/nhibernate/src/NHibernate/Linq/Functions/DefaultLinqToHqlGeneratorsRegistry.cs
===================================================================
--- trunk/nhibernate/src/NHibernate/Linq/Functions/DefaultLinqToHqlGeneratorsRegistry.cs 2010-07-28 21:42:28 UTC (rev 5077)
+++ trunk/nhibernate/src/NHibernate/Linq/Functions/DefaultLinqToHqlGeneratorsRegistry.cs 2010-07-28 22:19:19 UTC (rev 5078)
@@ -17,6 +17,7 @@
public DefaultLinqToHqlGeneratorsRegistry()
{
// TODO - could use reflection here
+ Register(new StandardLinqExtensionMethodGenerator());
Register(new QueryableGenerator());
Register(new StringGenerator());
Register(new DateTimeGenerator());
@@ -35,21 +36,6 @@
return false;
}
- protected bool GetStandardLinqExtensionMethodGenerator(MethodInfo method, out IHqlGeneratorForMethod methodGenerator)
- {
- methodGenerator = null;
-
- var attr = method.GetCustomAttributes(typeof(LinqExtensionMethodAttribute), false);
-
- if (attr.Length == 1)
- {
- // It is
- methodGenerator = new HqlGeneratorForExtensionMethod((LinqExtensionMethodAttribute)attr[0], method);
- return true;
- }
- return false;
- }
-
protected bool GetRegisteredMethodGenerator(MethodInfo method, out IHqlGeneratorForMethod methodGenerator)
{
if (registeredMethods.TryGetValue(method, out methodGenerator))
@@ -68,9 +54,6 @@
if (GetRegisteredMethodGenerator(method, out generator)) return true;
- // No method generator registered. Look to see if it's a standard LinqExtensionMethod
- if (GetStandardLinqExtensionMethodGenerator(method, out generator)) return true;
-
// Not that either. Let's query each type generator to see if it can handle it
if (GetMethodGeneratorForType(method, out generator)) return true;
@@ -98,36 +81,4 @@
typeMethodGenerator.Register(this);
}
}
-
- public class HqlGeneratorForExtensionMethod : BaseHqlGeneratorForMethod
- {
- private readonly string _name;
-
- public HqlGeneratorForExtensionMethod(LinqExtensionMethodAttribute attribute, MethodInfo method)
- {
- _name = string.IsNullOrEmpty(attribute.Name) ? method.Name : attribute.Name;
- }
-
- public override HqlTreeNode BuildHql(MethodInfo method, Expression targetObject, ReadOnlyCollection<Expression> arguments, HqlTreeBuilder treeBuilder, IHqlExpressionVisitor visitor)
- {
- var args = visitor.Visit(targetObject)
- .Union(arguments.Select(a => visitor.Visit(a)))
- .Cast<HqlExpression>();
-
- return treeBuilder.MethodCall(_name, args);
- }
- }
-
- static class UnionExtension
- {
- public static IEnumerable<HqlTreeNode> Union(this HqlTreeNode first, IEnumerable<HqlTreeNode> rest)
- {
- yield return first;
-
- foreach (var x in rest)
- {
- yield return x;
- }
- }
- }
}
\ No newline at end of file
Added: trunk/nhibernate/src/NHibernate/Linq/Functions/StandardLinqExtensionMethodGenerator.cs
===================================================================
--- trunk/nhibernate/src/NHibernate/Linq/Functions/StandardLinqExtensionMethodGenerator.cs (rev 0)
+++ trunk/nhibernate/src/NHibernate/Linq/Functions/StandardLinqExtensionMethodGenerator.cs 2010-07-28 22:19:19 UTC (rev 5078)
@@ -0,0 +1,64 @@
+using System.Collections.Generic;
+using System.Collections.ObjectModel;
+using System.Linq;
+using System.Linq.Expressions;
+using System.Reflection;
+using NHibernate.Hql.Ast;
+using NHibernate.Linq.Visitors;
+
+namespace NHibernate.Linq.Functions
+{
+ public class StandardLinqExtensionMethodGenerator : IHqlGeneratorForType
+ {
+ #region IHqlGeneratorForType Members
+
+ public void Register(ILinqToHqlGeneratorsRegistry functionRegistry)
+ {
+ // nothing to do
+ }
+
+ public bool SupportsMethod(MethodInfo method)
+ {
+ return method.GetCustomAttributes(typeof (LinqExtensionMethodAttribute), false).Any();
+ }
+
+ public IHqlGeneratorForMethod GetMethodGenerator(MethodInfo method)
+ {
+ return new HqlGeneratorForExtensionMethod((LinqExtensionMethodAttribute) method.GetCustomAttributes(typeof (LinqExtensionMethodAttribute), false).First(), method);
+ }
+
+ #endregion
+ }
+
+ public class HqlGeneratorForExtensionMethod : BaseHqlGeneratorForMethod
+ {
+ private readonly string _name;
+
+ public HqlGeneratorForExtensionMethod(LinqExtensionMethodAttribute attribute, MethodInfo method)
+ {
+ _name = string.IsNullOrEmpty(attribute.Name) ? method.Name : attribute.Name;
+ }
+
+ public override HqlTreeNode BuildHql(MethodInfo method, Expression targetObject, ReadOnlyCollection<Expression> arguments, HqlTreeBuilder treeBuilder, IHqlExpressionVisitor visitor)
+ {
+ var args = visitor.Visit(targetObject)
+ .Union(arguments.Select(a => visitor.Visit(a)))
+ .Cast<HqlExpression>();
+
+ return treeBuilder.MethodCall(_name, args);
+ }
+ }
+
+ static class UnionExtension
+ {
+ public static IEnumerable<HqlTreeNode> Union(this HqlTreeNode first, IEnumerable<HqlTreeNode> rest)
+ {
+ yield return first;
+
+ foreach (var x in rest)
+ {
+ yield return x;
+ }
+ }
+ }
+}
\ No newline at end of file
Modified: trunk/nhibernate/src/NHibernate/NHibernate.csproj
===================================================================
--- trunk/nhibernate/src/NHibernate/NHibernate.csproj 2010-07-28 21:42:28 UTC (rev 5077)
+++ trunk/nhibernate/src/NHibernate/NHibernate.csproj 2010-07-28 22:19:19 UTC (rev 5078)
@@ -656,6 +656,7 @@
<Compile Include="Linq\EagerFetchingExtensionMethods.cs" />
<Compile Include="Linq\Functions\ILinqToHqlGeneratorsRegistry.cs" />
<Compile Include="Linq\Functions\LinqToHqlGeneratorsRegistryFactory.cs" />
+ <Compile Include="Linq\Functions\StandardLinqExtensionMethodGenerator.cs" />
<Compile Include="Linq\LinqExtensionMethodAttribute.cs" />
<Compile Include="Linq\TypeHelperExtensionMethods.cs" />
<Compile Include="Linq\Visitors\NameUnNamedParameters.cs" />
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|