From: <fab...@us...> - 2010-07-28 21:19:54
|
Revision: 5075 http://nhibernate.svn.sourceforge.net/nhibernate/?rev=5075&view=rev Author: fabiomaulo Date: 2010-07-28 21:19:47 +0000 (Wed, 28 Jul 2010) Log Message: ----------- Refactoring (class renaming) Modified Paths: -------------- trunk/nhibernate/src/NHibernate/Linq/Functions/BaseHqlGeneratorForType.cs trunk/nhibernate/src/NHibernate/Linq/Functions/IHqlGeneratorForType.cs trunk/nhibernate/src/NHibernate/Linq/Functions/LinqToHqlGeneratorsRegistryFactory.cs trunk/nhibernate/src/NHibernate/NHibernate.csproj trunk/nhibernate/src/NHibernate.Test/CfgTest/Loquacious/LambdaConfigurationFixture.cs trunk/nhibernate/src/NHibernate.Test/Linq/LinqToHqlGeneratorsRegistryFactoryTest.cs Added Paths: ----------- trunk/nhibernate/src/NHibernate/Linq/Functions/DefaultLinqToHqlGeneratorsRegistry.cs Removed Paths: ------------- trunk/nhibernate/src/NHibernate/Linq/Functions/FunctionRegistry.cs Modified: trunk/nhibernate/src/NHibernate/Linq/Functions/BaseHqlGeneratorForType.cs =================================================================== --- trunk/nhibernate/src/NHibernate/Linq/Functions/BaseHqlGeneratorForType.cs 2010-07-28 21:15:15 UTC (rev 5074) +++ trunk/nhibernate/src/NHibernate/Linq/Functions/BaseHqlGeneratorForType.cs 2010-07-28 21:19:47 UTC (rev 5075) @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.Reflection; using NHibernate.Linq.Visitors; @@ -10,7 +10,7 @@ protected readonly List<IHqlGeneratorForMethod> MethodRegistry = new List<IHqlGeneratorForMethod>(); protected readonly List<IHqlGeneratorForProperty> PropertyRegistry = new List<IHqlGeneratorForProperty>(); - public void Register(FunctionRegistry functionRegistry) + public void Register(DefaultLinqToHqlGeneratorsRegistry functionRegistry) { foreach (var generator in MethodRegistry) { Copied: trunk/nhibernate/src/NHibernate/Linq/Functions/DefaultLinqToHqlGeneratorsRegistry.cs (from rev 5070, trunk/nhibernate/src/NHibernate/Linq/Functions/FunctionRegistry.cs) =================================================================== --- trunk/nhibernate/src/NHibernate/Linq/Functions/DefaultLinqToHqlGeneratorsRegistry.cs (rev 0) +++ trunk/nhibernate/src/NHibernate/Linq/Functions/DefaultLinqToHqlGeneratorsRegistry.cs 2010-07-28 21:19:47 UTC (rev 5075) @@ -0,0 +1,133 @@ +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 DefaultLinqToHqlGeneratorsRegistry : ILinqToHqlGeneratorsRegistry + { + private readonly Dictionary<MethodInfo, IHqlGeneratorForMethod> registeredMethods = new Dictionary<MethodInfo, IHqlGeneratorForMethod>(); + private readonly Dictionary<MemberInfo, IHqlGeneratorForProperty> registeredProperties = new Dictionary<MemberInfo, IHqlGeneratorForProperty>(); + private readonly List<IHqlGeneratorForType> typeGenerators = new List<IHqlGeneratorForType>(); + + public DefaultLinqToHqlGeneratorsRegistry() + { + // TODO - could use reflection here + Register(new QueryableGenerator()); + Register(new StringGenerator()); + Register(new DateTimeGenerator()); + Register(new ICollectionGenerator()); + } + + private bool GetMethodGeneratorForType(MethodInfo method, out IHqlGeneratorForMethod methodGenerator) + { + methodGenerator = null; + + foreach (var typeGenerator in typeGenerators.Where(typeGenerator => typeGenerator.SupportsMethod(method))) + { + methodGenerator = typeGenerator.GetMethodGenerator(method); + return true; + } + return false; + } + + private 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; + } + + private bool GetRegisteredMethodGenerator(MethodInfo method, out IHqlGeneratorForMethod methodGenerator) + { + if (registeredMethods.TryGetValue(method, out methodGenerator)) + { + return true; + } + return false; + } + + public bool TryGetGenerator(MethodInfo method, out IHqlGeneratorForMethod generator) + { + if (method.IsGenericMethod) + { + method = method.GetGenericMethodDefinition(); + } + + 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; + + return false; + } + + public bool TryGetGenerator(MemberInfo property, out IHqlGeneratorForProperty generator) + { + return registeredProperties.TryGetValue(property, out generator); + } + + public void RegisterGenerator(MethodInfo method, IHqlGeneratorForMethod generator) + { + registeredMethods.Add(method, generator); + } + + public void RegisterGenerator(MemberInfo property, IHqlGeneratorForProperty generator) + { + registeredProperties.Add(property, generator); + } + + private void Register(IHqlGeneratorForType typeMethodGenerator) + { + typeGenerators.Add(typeMethodGenerator); + 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 Deleted: trunk/nhibernate/src/NHibernate/Linq/Functions/FunctionRegistry.cs =================================================================== --- trunk/nhibernate/src/NHibernate/Linq/Functions/FunctionRegistry.cs 2010-07-28 21:15:15 UTC (rev 5074) +++ trunk/nhibernate/src/NHibernate/Linq/Functions/FunctionRegistry.cs 2010-07-28 21:19:47 UTC (rev 5075) @@ -1,133 +0,0 @@ -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 FunctionRegistry : ILinqToHqlGeneratorsRegistry - { - private readonly Dictionary<MethodInfo, IHqlGeneratorForMethod> registeredMethods = new Dictionary<MethodInfo, IHqlGeneratorForMethod>(); - private readonly Dictionary<MemberInfo, IHqlGeneratorForProperty> registeredProperties = new Dictionary<MemberInfo, IHqlGeneratorForProperty>(); - private readonly List<IHqlGeneratorForType> typeGenerators = new List<IHqlGeneratorForType>(); - - public FunctionRegistry() - { - // TODO - could use reflection here - Register(new QueryableGenerator()); - Register(new StringGenerator()); - Register(new DateTimeGenerator()); - Register(new ICollectionGenerator()); - } - - private bool GetMethodGeneratorForType(MethodInfo method, out IHqlGeneratorForMethod methodGenerator) - { - methodGenerator = null; - - foreach (var typeGenerator in typeGenerators.Where(typeGenerator => typeGenerator.SupportsMethod(method))) - { - methodGenerator = typeGenerator.GetMethodGenerator(method); - return true; - } - return false; - } - - private 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; - } - - private bool GetRegisteredMethodGenerator(MethodInfo method, out IHqlGeneratorForMethod methodGenerator) - { - if (registeredMethods.TryGetValue(method, out methodGenerator)) - { - return true; - } - return false; - } - - public bool TryGetGenerator(MethodInfo method, out IHqlGeneratorForMethod generator) - { - if (method.IsGenericMethod) - { - method = method.GetGenericMethodDefinition(); - } - - 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; - - return false; - } - - public bool TryGetGenerator(MemberInfo property, out IHqlGeneratorForProperty generator) - { - return registeredProperties.TryGetValue(property, out generator); - } - - public void RegisterGenerator(MethodInfo method, IHqlGeneratorForMethod generator) - { - registeredMethods.Add(method, generator); - } - - public void RegisterGenerator(MemberInfo property, IHqlGeneratorForProperty generator) - { - registeredProperties.Add(property, generator); - } - - private void Register(IHqlGeneratorForType typeMethodGenerator) - { - typeGenerators.Add(typeMethodGenerator); - 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 Modified: trunk/nhibernate/src/NHibernate/Linq/Functions/IHqlGeneratorForType.cs =================================================================== --- trunk/nhibernate/src/NHibernate/Linq/Functions/IHqlGeneratorForType.cs 2010-07-28 21:15:15 UTC (rev 5074) +++ trunk/nhibernate/src/NHibernate/Linq/Functions/IHqlGeneratorForType.cs 2010-07-28 21:19:47 UTC (rev 5075) @@ -1,10 +1,10 @@ -using System.Reflection; +using System.Reflection; namespace NHibernate.Linq.Functions { public interface IHqlGeneratorForType { - void Register(FunctionRegistry functionRegistry); + void Register(DefaultLinqToHqlGeneratorsRegistry functionRegistry); bool SupportsMethod(MethodInfo method); IHqlGeneratorForMethod GetMethodGenerator(MethodInfo method); } Modified: trunk/nhibernate/src/NHibernate/Linq/Functions/LinqToHqlGeneratorsRegistryFactory.cs =================================================================== --- trunk/nhibernate/src/NHibernate/Linq/Functions/LinqToHqlGeneratorsRegistryFactory.cs 2010-07-28 21:15:15 UTC (rev 5074) +++ trunk/nhibernate/src/NHibernate/Linq/Functions/LinqToHqlGeneratorsRegistryFactory.cs 2010-07-28 21:19:47 UTC (rev 5075) @@ -26,7 +26,7 @@ throw new HibernateException("Could not instantiate LinqToHqlGeneratorsRegistry: " + registry, e); } } - return new FunctionRegistry(); + return new DefaultLinqToHqlGeneratorsRegistry(); } } } \ No newline at end of file Modified: trunk/nhibernate/src/NHibernate/NHibernate.csproj =================================================================== --- trunk/nhibernate/src/NHibernate/NHibernate.csproj 2010-07-28 21:15:15 UTC (rev 5074) +++ trunk/nhibernate/src/NHibernate/NHibernate.csproj 2010-07-28 21:19:47 UTC (rev 5075) @@ -678,7 +678,7 @@ <Compile Include="Linq\GroupJoin\LocateGroupJoinQuerySource.cs" /> <Compile Include="Linq\GroupJoin\NonAggregatingGroupJoinRewriter.cs" /> <Compile Include="Linq\Functions\BaseHqlGeneratorForProperty.cs" /> - <Compile Include="Linq\Functions\FunctionRegistry.cs" /> + <Compile Include="Linq\Functions\DefaultLinqToHqlGeneratorsRegistry.cs" /> <Compile Include="Linq\Functions\IHqlGeneratorForMethod.cs" /> <Compile Include="Linq\Functions\IHqlGeneratorForProperty.cs" /> <Compile Include="Linq\Functions\BaseHqlGeneratorForMethod.cs" /> Modified: trunk/nhibernate/src/NHibernate.Test/CfgTest/Loquacious/LambdaConfigurationFixture.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Test/CfgTest/Loquacious/LambdaConfigurationFixture.cs 2010-07-28 21:15:15 UTC (rev 5074) +++ trunk/nhibernate/src/NHibernate.Test/CfgTest/Loquacious/LambdaConfigurationFixture.cs 2010-07-28 21:19:47 UTC (rev 5075) @@ -32,7 +32,7 @@ }); configure.CollectionTypeFactory<DefaultCollectionTypeFactory>(); configure.HqlQueryTranslator<ClassicQueryTranslatorFactory>(); - configure.LinqToHqlGeneratorsRegistry<FunctionRegistry>(); + configure.LinqToHqlGeneratorsRegistry<DefaultLinqToHqlGeneratorsRegistry>(); configure.Proxy(p => { p.Validation = false; @@ -102,7 +102,7 @@ Assert.That(configure.Properties[Environment.MaxFetchDepth], Is.EqualTo("11")); Assert.That(configure.Properties[Environment.QuerySubstitutions], Is.EqualTo("true 1, false 0, yes 'Y', no 'N'")); Assert.That(configure.Properties[Environment.Hbm2ddlAuto], Is.EqualTo("validate")); - configure.Properties[Environment.LinqToHqlGeneratorsRegistry].Should().Be(typeof(FunctionRegistry).AssemblyQualifiedName); + configure.Properties[Environment.LinqToHqlGeneratorsRegistry].Should().Be(typeof(DefaultLinqToHqlGeneratorsRegistry).AssemblyQualifiedName); } } } \ No newline at end of file Modified: trunk/nhibernate/src/NHibernate.Test/Linq/LinqToHqlGeneratorsRegistryFactoryTest.cs =================================================================== --- trunk/nhibernate/src/NHibernate.Test/Linq/LinqToHqlGeneratorsRegistryFactoryTest.cs 2010-07-28 21:15:15 UTC (rev 5074) +++ trunk/nhibernate/src/NHibernate.Test/Linq/LinqToHqlGeneratorsRegistryFactoryTest.cs 2010-07-28 21:19:47 UTC (rev 5075) @@ -15,7 +15,7 @@ { var registry = LinqToHqlGeneratorsRegistryFactory.CreateGeneratorsRegistry(new Dictionary<string, string>()); registry.Should().Not.Be.Null(); - registry.Should().Be.OfType<FunctionRegistry>(); + registry.Should().Be.OfType<DefaultLinqToHqlGeneratorsRegistry>(); } [Test] This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |