Skip to content

Loquatious QueryCache constraint should be an IQueryCacheFactory constraint #1849

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 18, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/NHibernate.Test/CfgTest/Loquacious/ConfigurationFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public void CompleteConfiguration()
.Through<HashtableCacheProvider>()
.PrefixingRegionsWith("xyz")
.Queries
.Through<StandardQueryCache>()
.Through<StandardQueryCacheFactory>()
.UsingMinimalPuts()
.WithDefaultExpiration(15)
.GeneratingCollections
Expand Down Expand Up @@ -69,7 +69,7 @@ public void CompleteConfiguration()
Assert.That(cfg.Properties[Environment.SessionFactoryName], Is.EqualTo("SomeName"));
Assert.That(cfg.Properties[Environment.CacheProvider], Is.EqualTo(typeof(HashtableCacheProvider).AssemblyQualifiedName));
Assert.That(cfg.Properties[Environment.CacheRegionPrefix], Is.EqualTo("xyz"));
Assert.That(cfg.Properties[Environment.QueryCacheFactory], Is.EqualTo(typeof(StandardQueryCache).AssemblyQualifiedName));
Assert.That(cfg.Properties[Environment.QueryCacheFactory], Is.EqualTo(typeof(StandardQueryCacheFactory).AssemblyQualifiedName));
Assert.That(cfg.Properties[Environment.UseMinimalPuts], Is.EqualTo("true"));
Assert.That(cfg.Properties[Environment.CacheDefaultExpiration], Is.EqualTo("15"));
Assert.That(cfg.Properties[Environment.CollectionTypeFactoryClass], Is.EqualTo(typeof(DefaultCollectionTypeFactory).AssemblyQualifiedName));
Expand Down Expand Up @@ -97,6 +97,12 @@ public void CompleteConfiguration()
Assert.That(cfg.Properties[Environment.MaxFetchDepth], Is.EqualTo("11"));
Assert.That(cfg.Properties[Environment.QuerySubstitutions], Is.EqualTo("true 1, false 0, yes 'Y', no 'N'"));
Assert.That(cfg.Properties[Environment.Hbm2ddlAuto], Is.EqualTo("validate"));

// Keywords import and auto-validation require a valid connection string, disable them before checking
// the session factory can be built.
cfg.SetProperty(Environment.Hbm2ddlKeyWords, "none");
cfg.SetProperty(Environment.Hbm2ddlAuto, null);
Assert.That(() => cfg.BuildSessionFactory().Dispose(), Throws.Nothing);
}

[Test]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using NHibernate.Bytecode;
using NHibernate.Cache;
using NHibernate.Cfg;
using NHibernate.Cfg.Loquacious;
using NHibernate.Dialect;
using NHibernate.Driver;
using NHibernate.Hql.Ast.ANTLR;
Expand All @@ -27,7 +28,7 @@ public void FullConfiguration()
c.DefaultExpiration = 15;
c.RegionsPrefix = "xyz";
c.Provider<HashtableCacheProvider>();
c.QueryCache<StandardQueryCache>();
c.QueryCacheFactory<StandardQueryCacheFactory>();
});
configure.CollectionTypeFactory<DefaultCollectionTypeFactory>();
configure.HqlQueryTranslator<ASTQueryTranslatorFactory>();
Expand Down Expand Up @@ -69,7 +70,7 @@ public void FullConfiguration()
Is.EqualTo(typeof(HashtableCacheProvider).AssemblyQualifiedName));
Assert.That(configure.Properties[Environment.CacheRegionPrefix], Is.EqualTo("xyz"));
Assert.That(configure.Properties[Environment.QueryCacheFactory],
Is.EqualTo(typeof(StandardQueryCache).AssemblyQualifiedName));
Is.EqualTo(typeof(StandardQueryCacheFactory).AssemblyQualifiedName));
Assert.That(configure.Properties[Environment.UseMinimalPuts], Is.EqualTo("true"));
Assert.That(configure.Properties[Environment.CacheDefaultExpiration], Is.EqualTo("15"));
Assert.That(configure.Properties[Environment.CollectionTypeFactoryClass],
Expand Down Expand Up @@ -106,6 +107,12 @@ public void FullConfiguration()
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"));
Assert.That(configure.Properties[Environment.LinqToHqlGeneratorsRegistry], Is.EqualTo(typeof(DefaultLinqToHqlGeneratorsRegistry).AssemblyQualifiedName));

// Keywords import and auto-validation require a valid connection string, disable them before checking
// the session factory can be built.
configure.SetProperty(Environment.Hbm2ddlKeyWords, "none");
configure.SetProperty(Environment.Hbm2ddlAuto, null);
Assert.That(() => configure.BuildSessionFactory().Dispose(), Throws.Nothing);
}
}
}
15 changes: 13 additions & 2 deletions src/NHibernate/Cfg/Loquacious/CacheConfiguration.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using NHibernate.Cache;

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

[Obsolete("This method is invalid and should not be used. Use QueryCacheFactory method instead.", true)]
public void QueryCache<TFactory>() where TFactory : IQueryCache
{
throw new InvalidOperationException("This method is invalid and should not be used. Use QueryCacheFactory method instead.");
}

public void QueryCacheFactory<TFactory>() where TFactory : IQueryCacheFactory
{
UseSecondLevelCache = true;
UseQueryCache = true;
Expand Down Expand Up @@ -111,8 +118,12 @@ public QueryCacheConfiguration(CacheConfiguration cc)

#region Implementation of IQueryCacheConfiguration

public ICacheConfiguration Through<TFactory>() where TFactory : IQueryCache
// 6.0 TODO: enable constraint and remove runtime type check
public ICacheConfiguration Through<TFactory>() // where TFactory : IQueryCacheFactory
{
if (!typeof(IQueryCacheFactory).IsAssignableFrom(typeof(TFactory)))
throw new ArgumentException($"{nameof(TFactory)} must be an {nameof(IQueryCacheFactory)}", nameof(TFactory));

cc.Configuration.SetProperty(Environment.UseSecondLevelCache, "true");
cc.Configuration.SetProperty(Environment.UseQueryCache, "true");
cc.Configuration.SetProperty(Environment.QueryCacheFactory, typeof(TFactory).AssemblyQualifiedName);
Expand All @@ -121,4 +132,4 @@ public ICacheConfiguration Through<TFactory>() where TFactory : IQueryCache

#endregion
}
}
}
17 changes: 16 additions & 1 deletion src/NHibernate/Cfg/Loquacious/ICacheConfiguration.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using System;
using NHibernate.Cache;
using NHibernate.Util;

namespace NHibernate.Cfg.Loquacious
{
public interface ICacheConfiguration
Expand All @@ -17,6 +20,18 @@ public interface ICacheConfigurationProperties
string RegionsPrefix { set; }
int DefaultExpiration { set; }
void Provider<TProvider>() where TProvider : ICacheProvider;
[Obsolete("This method is invalid and should not be used. Use the QueryCacheFactory extension method instead.", true)]
void QueryCache<TFactory>() where TFactory : IQueryCache;
}
}

// 6.0 TODO: merge into ICacheConfigurationProperties
public static class CacheConfigurationPropertiesExtensions
{
public static void QueryCacheFactory<TFactory>(this ICacheConfigurationProperties config) where TFactory : IQueryCacheFactory
{
ReflectHelper
.CastOrThrow<CacheConfigurationProperties>(config, "Setting the query cache factory with Loquacious")
.QueryCacheFactory<TFactory>();
}
}
}
7 changes: 3 additions & 4 deletions src/NHibernate/Cfg/Loquacious/IQueryCacheConfiguration.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
using NHibernate.Cache;

namespace NHibernate.Cfg.Loquacious
{
public interface IQueryCacheConfiguration
{
ICacheConfiguration Through<TFactory>() where TFactory : IQueryCache;
// 6.0 TODO: enable constraint
ICacheConfiguration Through<TFactory>(); // where TFactory : IQueryCacheFactory;
}
}
}