Skip to content

Commit 3dc082f

Browse files
InlineAsmfredericDelaporte
authored andcommitted
Loquatious QueryCache constraint should be an IQueryCacheFactory constraint (#1849)
The configuration setting is used to create an instance of a factory, not of the query cache itself.
1 parent cef9f04 commit 3dc082f

File tree

5 files changed

+49
-11
lines changed

5 files changed

+49
-11
lines changed

src/NHibernate.Test/CfgTest/Loquacious/ConfigurationFixture.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public void CompleteConfiguration()
2929
.Through<HashtableCacheProvider>()
3030
.PrefixingRegionsWith("xyz")
3131
.Queries
32-
.Through<StandardQueryCache>()
32+
.Through<StandardQueryCacheFactory>()
3333
.UsingMinimalPuts()
3434
.WithDefaultExpiration(15)
3535
.GeneratingCollections
@@ -69,7 +69,7 @@ public void CompleteConfiguration()
6969
Assert.That(cfg.Properties[Environment.SessionFactoryName], Is.EqualTo("SomeName"));
7070
Assert.That(cfg.Properties[Environment.CacheProvider], Is.EqualTo(typeof(HashtableCacheProvider).AssemblyQualifiedName));
7171
Assert.That(cfg.Properties[Environment.CacheRegionPrefix], Is.EqualTo("xyz"));
72-
Assert.That(cfg.Properties[Environment.QueryCacheFactory], Is.EqualTo(typeof(StandardQueryCache).AssemblyQualifiedName));
72+
Assert.That(cfg.Properties[Environment.QueryCacheFactory], Is.EqualTo(typeof(StandardQueryCacheFactory).AssemblyQualifiedName));
7373
Assert.That(cfg.Properties[Environment.UseMinimalPuts], Is.EqualTo("true"));
7474
Assert.That(cfg.Properties[Environment.CacheDefaultExpiration], Is.EqualTo("15"));
7575
Assert.That(cfg.Properties[Environment.CollectionTypeFactoryClass], Is.EqualTo(typeof(DefaultCollectionTypeFactory).AssemblyQualifiedName));
@@ -97,6 +97,12 @@ public void CompleteConfiguration()
9797
Assert.That(cfg.Properties[Environment.MaxFetchDepth], Is.EqualTo("11"));
9898
Assert.That(cfg.Properties[Environment.QuerySubstitutions], Is.EqualTo("true 1, false 0, yes 'Y', no 'N'"));
9999
Assert.That(cfg.Properties[Environment.Hbm2ddlAuto], Is.EqualTo("validate"));
100+
101+
// Keywords import and auto-validation require a valid connection string, disable them before checking
102+
// the session factory can be built.
103+
cfg.SetProperty(Environment.Hbm2ddlKeyWords, "none");
104+
cfg.SetProperty(Environment.Hbm2ddlAuto, null);
105+
Assert.That(() => cfg.BuildSessionFactory().Dispose(), Throws.Nothing);
100106
}
101107

102108
[Test]

src/NHibernate.Test/CfgTest/Loquacious/LambdaConfigurationFixture.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using NHibernate.Bytecode;
44
using NHibernate.Cache;
55
using NHibernate.Cfg;
6+
using NHibernate.Cfg.Loquacious;
67
using NHibernate.Dialect;
78
using NHibernate.Driver;
89
using NHibernate.Hql.Ast.ANTLR;
@@ -27,7 +28,7 @@ public void FullConfiguration()
2728
c.DefaultExpiration = 15;
2829
c.RegionsPrefix = "xyz";
2930
c.Provider<HashtableCacheProvider>();
30-
c.QueryCache<StandardQueryCache>();
31+
c.QueryCacheFactory<StandardQueryCacheFactory>();
3132
});
3233
configure.CollectionTypeFactory<DefaultCollectionTypeFactory>();
3334
configure.HqlQueryTranslator<ASTQueryTranslatorFactory>();
@@ -69,7 +70,7 @@ public void FullConfiguration()
6970
Is.EqualTo(typeof(HashtableCacheProvider).AssemblyQualifiedName));
7071
Assert.That(configure.Properties[Environment.CacheRegionPrefix], Is.EqualTo("xyz"));
7172
Assert.That(configure.Properties[Environment.QueryCacheFactory],
72-
Is.EqualTo(typeof(StandardQueryCache).AssemblyQualifiedName));
73+
Is.EqualTo(typeof(StandardQueryCacheFactory).AssemblyQualifiedName));
7374
Assert.That(configure.Properties[Environment.UseMinimalPuts], Is.EqualTo("true"));
7475
Assert.That(configure.Properties[Environment.CacheDefaultExpiration], Is.EqualTo("15"));
7576
Assert.That(configure.Properties[Environment.CollectionTypeFactoryClass],
@@ -106,6 +107,12 @@ public void FullConfiguration()
106107
Assert.That(configure.Properties[Environment.QuerySubstitutions], Is.EqualTo("true 1, false 0, yes 'Y', no 'N'"));
107108
Assert.That(configure.Properties[Environment.Hbm2ddlAuto], Is.EqualTo("validate"));
108109
Assert.That(configure.Properties[Environment.LinqToHqlGeneratorsRegistry], Is.EqualTo(typeof(DefaultLinqToHqlGeneratorsRegistry).AssemblyQualifiedName));
110+
111+
// Keywords import and auto-validation require a valid connection string, disable them before checking
112+
// the session factory can be built.
113+
configure.SetProperty(Environment.Hbm2ddlKeyWords, "none");
114+
configure.SetProperty(Environment.Hbm2ddlAuto, null);
115+
Assert.That(() => configure.BuildSessionFactory().Dispose(), Throws.Nothing);
109116
}
110117
}
111118
}

src/NHibernate/Cfg/Loquacious/CacheConfiguration.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System;
12
using NHibernate.Cache;
23

34
namespace NHibernate.Cfg.Loquacious
@@ -39,7 +40,13 @@ public void Provider<TProvider>() where TProvider : ICacheProvider
3940
cfg.SetProperty(Environment.CacheProvider, typeof(TProvider).AssemblyQualifiedName);
4041
}
4142

43+
[Obsolete("This method is invalid and should not be used. Use QueryCacheFactory method instead.", true)]
4244
public void QueryCache<TFactory>() where TFactory : IQueryCache
45+
{
46+
throw new InvalidOperationException("This method is invalid and should not be used. Use QueryCacheFactory method instead.");
47+
}
48+
49+
public void QueryCacheFactory<TFactory>() where TFactory : IQueryCacheFactory
4350
{
4451
UseSecondLevelCache = true;
4552
UseQueryCache = true;
@@ -111,8 +118,12 @@ public QueryCacheConfiguration(CacheConfiguration cc)
111118

112119
#region Implementation of IQueryCacheConfiguration
113120

114-
public ICacheConfiguration Through<TFactory>() where TFactory : IQueryCache
121+
// 6.0 TODO: enable constraint and remove runtime type check
122+
public ICacheConfiguration Through<TFactory>() // where TFactory : IQueryCacheFactory
115123
{
124+
if (!typeof(IQueryCacheFactory).IsAssignableFrom(typeof(TFactory)))
125+
throw new ArgumentException($"{nameof(TFactory)} must be an {nameof(IQueryCacheFactory)}", nameof(TFactory));
126+
116127
cc.Configuration.SetProperty(Environment.UseSecondLevelCache, "true");
117128
cc.Configuration.SetProperty(Environment.UseQueryCache, "true");
118129
cc.Configuration.SetProperty(Environment.QueryCacheFactory, typeof(TFactory).AssemblyQualifiedName);
@@ -121,4 +132,4 @@ public ICacheConfiguration Through<TFactory>() where TFactory : IQueryCache
121132

122133
#endregion
123134
}
124-
}
135+
}

src/NHibernate/Cfg/Loquacious/ICacheConfiguration.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
using System;
12
using NHibernate.Cache;
3+
using NHibernate.Util;
4+
25
namespace NHibernate.Cfg.Loquacious
36
{
47
public interface ICacheConfiguration
@@ -17,6 +20,18 @@ public interface ICacheConfigurationProperties
1720
string RegionsPrefix { set; }
1821
int DefaultExpiration { set; }
1922
void Provider<TProvider>() where TProvider : ICacheProvider;
23+
[Obsolete("This method is invalid and should not be used. Use the QueryCacheFactory extension method instead.", true)]
2024
void QueryCache<TFactory>() where TFactory : IQueryCache;
2125
}
22-
}
26+
27+
// 6.0 TODO: merge into ICacheConfigurationProperties
28+
public static class CacheConfigurationPropertiesExtensions
29+
{
30+
public static void QueryCacheFactory<TFactory>(this ICacheConfigurationProperties config) where TFactory : IQueryCacheFactory
31+
{
32+
ReflectHelper
33+
.CastOrThrow<CacheConfigurationProperties>(config, "Setting the query cache factory with Loquacious")
34+
.QueryCacheFactory<TFactory>();
35+
}
36+
}
37+
}
Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
using NHibernate.Cache;
2-
31
namespace NHibernate.Cfg.Loquacious
42
{
53
public interface IQueryCacheConfiguration
64
{
7-
ICacheConfiguration Through<TFactory>() where TFactory : IQueryCache;
5+
// 6.0 TODO: enable constraint
6+
ICacheConfiguration Through<TFactory>(); // where TFactory : IQueryCacheFactory;
87
}
9-
}
8+
}

0 commit comments

Comments
 (0)