Skip to content

Commit 8cc7f9c

Browse files
committed
Added LinqToHqlGeneratorsRegistryFactory to create Generators Registry from configuration properties and through ObjectsFactory
SVN: trunk@5071
1 parent f4d4764 commit 8cc7f9c

File tree

5 files changed

+89
-0
lines changed

5 files changed

+89
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Reflection;
4+
using NHibernate.Linq.Functions;
5+
using NUnit.Framework;
6+
using SharpTestsEx;
7+
using Environment = NHibernate.Cfg.Environment;
8+
9+
namespace NHibernate.Test.Linq
10+
{
11+
public class LinqToHqlGeneratorsRegistryFactoryTest
12+
{
13+
[Test]
14+
public void WhenNotDefinedThenReturnDefaultRegistry()
15+
{
16+
var registry = LinqToHqlGeneratorsRegistryFactory.CreateGeneratorsRegistry(new Dictionary<string, string>());
17+
registry.Should().Not.Be.Null();
18+
registry.Should().Be.OfType<FunctionRegistry>();
19+
}
20+
21+
[Test]
22+
public void WhenDefinedThenReturnCustomtRegistry()
23+
{
24+
var properties = new Dictionary<string, string> { { Environment.LinqToHqlGeneratorsRegistry, typeof(MyLinqToHqlGeneratorsRegistry).AssemblyQualifiedName } };
25+
var registry = LinqToHqlGeneratorsRegistryFactory.CreateGeneratorsRegistry(properties);
26+
registry.Should().Not.Be.Null();
27+
registry.Should().Be.OfType<MyLinqToHqlGeneratorsRegistry>();
28+
}
29+
30+
private class MyLinqToHqlGeneratorsRegistry : ILinqToHqlGeneratorsRegistry
31+
{
32+
public bool TryGetGenerator(MethodInfo method, out IHqlGeneratorForMethod generator)
33+
{
34+
throw new NotImplementedException();
35+
}
36+
37+
public bool TryGetGenerator(MemberInfo property, out IHqlGeneratorForProperty generator)
38+
{
39+
throw new NotImplementedException();
40+
}
41+
42+
public void RegisterGenerator(MethodInfo method, IHqlGeneratorForMethod generator)
43+
{
44+
throw new NotImplementedException();
45+
}
46+
47+
public void RegisterGenerator(MemberInfo property, IHqlGeneratorForProperty generator)
48+
{
49+
throw new NotImplementedException();
50+
}
51+
}
52+
}
53+
}

src/NHibernate.Test/NHibernate.Test.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,7 @@
425425
<Compile Include="Linq\LinqQuerySamples.cs" />
426426
<Compile Include="Linq\LinqReadonlyTestsContext.cs" />
427427
<Compile Include="Linq\LinqTestCase.cs" />
428+
<Compile Include="Linq\LinqToHqlGeneratorsRegistryFactoryTest.cs" />
428429
<Compile Include="Linq\MethodCallTests.cs" />
429430
<Compile Include="Linq\MiscellaneousTextFixture.cs" />
430431
<Compile Include="Linq\NorthwindDbCreator.cs" />

src/NHibernate/Cfg/Environment.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,8 @@ public static string Version
159159
public const string DefaultBatchFetchSize = "default_batch_fetch_size";
160160

161161
public const string CollectionTypeFactoryClass = "collectiontype.factory_class";
162+
163+
public const string LinqToHqlGeneratorsRegistry = "linqtohql.generatorsregistry";
162164

163165
private static readonly Dictionary<string, string> GlobalProperties;
164166

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using log4net;
4+
using NHibernate.Util;
5+
using Environment = NHibernate.Cfg.Environment;
6+
7+
namespace NHibernate.Linq.Functions
8+
{
9+
public sealed class LinqToHqlGeneratorsRegistryFactory
10+
{
11+
private static readonly ILog log = LogManager.GetLogger(typeof (LinqToHqlGeneratorsRegistryFactory));
12+
13+
public static ILinqToHqlGeneratorsRegistry CreateGeneratorsRegistry(IDictionary<string, string> properties)
14+
{
15+
string registry;
16+
if (properties.TryGetValue(Environment.LinqToHqlGeneratorsRegistry, out registry))
17+
{
18+
try
19+
{
20+
log.Info("Initializing LinqToHqlGeneratorsRegistry: " + registry);
21+
return (ILinqToHqlGeneratorsRegistry) Environment.BytecodeProvider.ObjectsFactory.CreateInstance(ReflectHelper.ClassForName(registry));
22+
}
23+
catch (Exception e)
24+
{
25+
log.Fatal("Could not instantiate LinqToHqlGeneratorsRegistry", e);
26+
throw new HibernateException("Could not instantiate LinqToHqlGeneratorsRegistry: " + registry, e);
27+
}
28+
}
29+
return new FunctionRegistry();
30+
}
31+
}
32+
}

src/NHibernate/NHibernate.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -655,6 +655,7 @@
655655
<Compile Include="Linq\Expressions\AggregateExpressionNode.cs" />
656656
<Compile Include="Linq\EagerFetchingExtensionMethods.cs" />
657657
<Compile Include="Linq\Functions\ILinqToHqlGeneratorsRegistry.cs" />
658+
<Compile Include="Linq\Functions\LinqToHqlGeneratorsRegistryFactory.cs" />
658659
<Compile Include="Linq\LinqExtensionMethodAttribute.cs" />
659660
<Compile Include="Linq\TypeHelperExtensionMethods.cs" />
660661
<Compile Include="Linq\Visitors\NameUnNamedParameters.cs" />

0 commit comments

Comments
 (0)