Skip to content

Commit 448bba9

Browse files
mirapalhetajagregory
authored andcommitted
* Moved cache configuration from PersistentConfiguration to FluentConfiguration
* Moved methods CurrentSessionContext, ProxyFactoryFactory and CollectionTypeFactory to FluentConfiguration. * Changed from fixed text configurations to NHibernate 3.0 Environment constants. * Added second level cache configuration.
1 parent 3c15235 commit 448bba9

File tree

5 files changed

+216
-201
lines changed

5 files changed

+216
-201
lines changed

src/FluentNHibernate.Testing/Cfg/Db/PersistenceConfigurationTester.cs

Lines changed: 0 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -76,47 +76,6 @@ public void Use_Reflection_Optimizer_should_set_value_to_const_true()
7676

7777
}
7878

79-
[Test]
80-
public void Provider_Class_should_set_property_value()
81-
{
82-
_config.Cache(c => c
83-
.ProviderClass("foo"));
84-
ValueOf("cache.provider_class").ShouldEqual("foo");
85-
86-
}
87-
88-
[Test]
89-
public void Use_Minimal_Puts_should_set_value_to_const_true()
90-
{
91-
_config.Cache(c => c
92-
.UseMinimalPuts());
93-
ValueOf("cache.use_minimal_puts").ShouldEqual("true");
94-
}
95-
96-
[Test]
97-
public void Use_Query_Cache_should_set_value_to_const_true()
98-
{
99-
_config.Cache(c => c
100-
.UseQueryCache());
101-
ValueOf("cache.use_query_cache").ShouldEqual("true");
102-
}
103-
104-
[Test]
105-
public void Query_Cache_Factory_should_set_property_value()
106-
{
107-
_config.Cache(c => c
108-
.QueryCacheFactory("foo"));
109-
ValueOf("cache.query_cache_factory").ShouldEqual("foo");
110-
}
111-
112-
[Test]
113-
public void Region_Prefix_should_set_property_value()
114-
{
115-
_config.Cache(c => c
116-
.RegionPrefix("foo"));
117-
ValueOf("cache.region_prefix").ShouldEqual("foo");
118-
}
119-
12079
[Test]
12180
public void Query_Substitutions_should_set_property_value()
12281
{

src/FluentNHibernate.Testing/Cfg/FluentConfigurationTests.cs

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ public void ShouldGetAConfigurationIfEverythingIsOK()
130130
public void ShouldSetCurrentSessionContext()
131131
{
132132
var configuration = Fluently.Configure()
133-
.Database(SQLiteConfiguration.Standard.CurrentSessionContext("thread_static").InMemory)
133+
.CurrentSessionContext("thread_static")
134134
.BuildConfiguration();
135135

136136
configuration.Properties["current_session_context_class"].ShouldEqual("thread_static");
@@ -140,7 +140,7 @@ public void ShouldSetCurrentSessionContext()
140140
public void ShouldSetCurrentSessionContextUsingGeneric()
141141
{
142142
var configuration = Fluently.Configure()
143-
.Database(SQLiteConfiguration.Standard.CurrentSessionContext<NHibernate.Context.ThreadStaticSessionContext>())
143+
.CurrentSessionContext<NHibernate.Context.ThreadStaticSessionContext>()
144144
.BuildConfiguration();
145145

146146
configuration.Properties["current_session_context_class"].ShouldEqual(typeof(NHibernate.Context.ThreadStaticSessionContext).AssemblyQualifiedName);
@@ -155,6 +155,55 @@ public void ShouldSetConnectionIsolationLevel()
155155

156156
configuration.Properties["connection.isolation"].ShouldEqual("ReadUncommitted");
157157
}
158+
159+
[Test]
160+
public void Use_Minimal_Puts_should_set_value_to_const_true()
161+
{
162+
var configuration = Fluently.Configure()
163+
.Cache(x => x.UseMinimalPuts())
164+
.BuildConfiguration();
165+
166+
configuration.Properties.ShouldContain("cache.use_minimal_puts", "true");
167+
}
168+
169+
[Test]
170+
public void Use_Query_Cache_should_set_value_to_const_true()
171+
{
172+
var configuration = Fluently.Configure()
173+
.Cache(x => x.UseQueryCache())
174+
.BuildConfiguration();
175+
176+
configuration.Properties.ShouldContain("cache.use_query_cache", "true");
177+
}
178+
179+
[Test]
180+
public void Query_Cache_Factory_should_set_property_value()
181+
{
182+
var configuration = Fluently.Configure()
183+
.Cache(x => x.QueryCacheFactory("foo"))
184+
.BuildConfiguration();
185+
186+
configuration.Properties.ShouldContain("cache.query_cache_factory", "foo");
187+
}
188+
189+
[Test]
190+
public void Region_Prefix_should_set_property_value()
191+
{
192+
var configuration = Fluently.Configure()
193+
.Cache(x => x.RegionPrefix("foo"))
194+
.BuildConfiguration();
195+
196+
configuration.Properties.ShouldContain("cache.region_prefix", "foo");
197+
}
198+
199+
[Test]
200+
public void Provider_Class_should_set_property_value()
201+
{
202+
var configuration = Fluently.Configure()
203+
.Cache(x => x.ProviderClass("foo"))
204+
.BuildConfiguration();
205+
206+
}
158207
}
159208

160209
[TestFixture]

src/FluentNHibernate/Cfg/Db/CacheSettingsBuilder.cs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
using System.Collections.Generic;
22
using System.Diagnostics;
33
using NHibernate.Cache;
4+
using NHibEnvironment = NHibernate.Cfg.Environment;
45

56
namespace FluentNHibernate.Cfg.Db
67
{
78
public class CacheSettingsBuilder
89
{
9-
protected const string ProviderClassKey = "cache.provider_class";
10-
protected const string CacheUseMininmalPutsKey = "cache.use_minimal_puts";
11-
protected const string CacheUseQueryCacheKey = "cache.use_query_cache";
12-
protected const string CacheQueryCacheFactoryKey = "cache.query_cache_factory";
13-
protected const string CacheRegionPrefixKey = "cache.region_prefix";
10+
protected const string ProviderClassKey = NHibEnvironment.CacheProvider;
11+
protected const string CacheUseMininmalPutsKey = NHibEnvironment.UseMinimalPuts;
12+
protected const string CacheUseQueryCacheKey = NHibEnvironment.UseQueryCache;
13+
protected const string CacheUseSecondLevelCacheKey = NHibEnvironment.UseSecondLevelCache;
14+
protected const string CacheQueryCacheFactoryKey = NHibEnvironment.QueryCacheFactory;
15+
protected const string CacheRegionPrefixKey = NHibEnvironment.CacheRegionPrefix;
1416

1517
private readonly IDictionary<string, string> settings = new Dictionary<string, string>();
1618
private bool nextBool = true;
@@ -54,6 +56,14 @@ public CacheSettingsBuilder UseQueryCache()
5456
return this;
5557
}
5658

59+
public CacheSettingsBuilder UseSecondLevelCache()
60+
{
61+
settings.Add(CacheUseSecondLevelCacheKey, nextBool.ToString().ToLowerInvariant());
62+
nextBool = true;
63+
IsDirty = true;
64+
return this;
65+
}
66+
5767
public CacheSettingsBuilder QueryCacheFactory(string factory)
5868
{
5969
settings.Add(CacheQueryCacheFactoryKey, factory);

src/FluentNHibernate/Cfg/Db/PersistenceConfiguration.cs

Lines changed: 14 additions & 136 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using NHibernate.Dialect;
77
using NHibernate.Driver;
88
using NHibConfiguration = NHibernate.Cfg.Configuration;
9+
using NHibEnvironment = NHibernate.Cfg.Environment;
910

1011
namespace FluentNHibernate.Cfg.Db
1112
{
@@ -17,37 +18,31 @@ public abstract class PersistenceConfiguration<TThisConfiguration, TConnectionSt
1718
where TThisConfiguration : PersistenceConfiguration<TThisConfiguration, TConnectionString>
1819
where TConnectionString : ConnectionStringBuilder, new()
1920
{
20-
protected const string DialectKey = "dialect"; // Newer one, but not supported by everything
21+
protected const string DialectKey = NHibEnvironment.Dialect; // Newer one, but not supported by everything
2122
protected const string AltDialectKey = "hibernate.dialect"; // Some older NHib tools require this
2223
protected const string DefaultSchemaKey = "default_schema";
2324
protected const string UseOuterJoinKey = "use_outer_join";
24-
protected const string MaxFetchDepthKey = "max_fetch_depth";
25-
protected const string UseReflectionOptimizerKey = "use_reflection_optimizer";
26-
protected const string QuerySubstitutionsKey = "query.substitutions";
27-
protected const string ShowSqlKey = "show_sql";
28-
protected const string FormatSqlKey = "format_sql";
29-
30-
protected const string CollectionTypeFactoryClassKey = NHibernate.Cfg.Environment.CollectionTypeFactoryClass;
31-
protected const string ConnectionProviderKey = "connection.provider";
25+
protected const string MaxFetchDepthKey = NHibEnvironment.MaxFetchDepth;
26+
protected const string UseReflectionOptimizerKey = NHibEnvironment.PropertyUseReflectionOptimizer;
27+
protected const string QuerySubstitutionsKey = NHibEnvironment.QuerySubstitutions;
28+
protected const string ShowSqlKey = NHibEnvironment.ShowSql;
29+
protected const string FormatSqlKey = NHibEnvironment.FormatSql;
30+
31+
protected const string ConnectionProviderKey = NHibEnvironment.ConnectionProvider;
3232
protected const string DefaultConnectionProviderClassName = "NHibernate.Connection.DriverConnectionProvider";
33-
protected const string DriverClassKey = "connection.driver_class";
34-
protected const string ConnectionStringKey = "connection.connection_string";
35-
const string IsolationLevelKey = "connection.isolation";
36-
protected const string ProxyFactoryFactoryClassKey = "proxyfactory.factory_class";
37-
protected const string DefaultProxyFactoryFactoryClassName = "NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle";
38-
protected const string AdoNetBatchSizeKey = "adonet.batch_size";
39-
protected const string CurrentSessionContextClassKey = "current_session_context_class";
33+
protected const string DriverClassKey = NHibEnvironment.ConnectionDriver;
34+
protected const string ConnectionStringKey = NHibEnvironment.ConnectionString;
35+
protected const string IsolationLevelKey = NHibEnvironment.Isolation;
36+
protected const string AdoNetBatchSizeKey = NHibEnvironment.BatchSize;
4037

4138
private readonly Dictionary<string, string> values = new Dictionary<string, string>();
4239

4340
private bool nextBoolSettingValue = true;
4441
private readonly TConnectionString connectionString;
45-
private readonly CacheSettingsBuilder cache = new CacheSettingsBuilder();
4642

4743
protected PersistenceConfiguration()
4844
{
4945
values[ConnectionProviderKey] = DefaultConnectionProviderClassName;
50-
values[ProxyFactoryFactoryClassKey] = DefaultProxyFactoryFactoryClassName;
5146
connectionString = new TConnectionString();
5247
}
5348

@@ -56,22 +51,14 @@ protected virtual IDictionary<string, string> CreateProperties()
5651
if (connectionString.IsDirty)
5752
Raw(ConnectionStringKey, connectionString.Create());
5853

59-
if (cache.IsDirty)
60-
{
61-
foreach (var pair in cache.Create())
62-
{
63-
Raw(pair.Key, pair.Value);
64-
}
65-
}
66-
6754
return values;
6855
}
6956

7057
public NHibConfiguration ConfigureProperties(NHibConfiguration nhibernateConfig)
7158
{
7259
var settings = CreateProperties();
7360

74-
nhibernateConfig.SetProperties(settings);
61+
nhibernateConfig.AddProperties(settings);
7562

7663
return nhibernateConfig;
7764
}
@@ -276,24 +263,6 @@ public TThisConfiguration ConnectionString(string value)
276263
return (TThisConfiguration)this;
277264
}
278265

279-
/// <summary>
280-
/// Configure caching.
281-
/// </summary>
282-
/// <example>
283-
/// Cache(x =>
284-
/// {
285-
/// x.UseQueryCache();
286-
/// x.UseMinimalPuts();
287-
/// });
288-
/// </example>
289-
/// <param name="cacheExpression">Closure for configuring caching</param>
290-
/// <returns>Configuration builder</returns>
291-
public TThisConfiguration Cache(Action<CacheSettingsBuilder> cacheExpression)
292-
{
293-
cacheExpression(cache);
294-
return (TThisConfiguration)this;
295-
}
296-
297266
/// <summary>
298267
/// Sets a raw property on the NHibernate configuration. Use this method
299268
/// if there isn't a specific option available in the API.
@@ -307,76 +276,6 @@ public TThisConfiguration Raw(string key, string value)
307276
return (TThisConfiguration) this;
308277
}
309278

310-
/// <summary>
311-
/// Sets the collectiontype.factory_class property.
312-
/// NOTE: NHibernate 2.1 only
313-
/// </summary>
314-
/// <param name="collectionTypeFactoryClass">factory class</param>
315-
/// <returns>Configuration</returns>
316-
public TThisConfiguration CollectionTypeFactory(string collectionTypeFactoryClass)
317-
{
318-
values[CollectionTypeFactoryClassKey] = collectionTypeFactoryClass;
319-
return (TThisConfiguration)this;
320-
}
321-
322-
/// <summary>
323-
/// Sets the collectiontype.factory_class property.
324-
/// NOTE: NHibernate 2.1 only
325-
/// </summary>
326-
/// <param name="collectionTypeFactoryClass">factory class</param>
327-
/// <returns>Configuration</returns>
328-
public TThisConfiguration CollectionTypeFactory(Type collectionTypeFactoryClass)
329-
{
330-
values[CollectionTypeFactoryClassKey] = collectionTypeFactoryClass.AssemblyQualifiedName;
331-
return (TThisConfiguration)this;
332-
}
333-
334-
/// <summary>
335-
/// Sets the collectiontype.factory_class property.
336-
/// NOTE: NHibernate 2.1 only
337-
/// </summary>
338-
/// <typeparam name="TCollectionTypeFactory">factory class</typeparam>
339-
/// <returns>Configuration</returns>
340-
public TThisConfiguration CollectionTypeFactory<TCollectionTypeFactory>() where TCollectionTypeFactory : ICollectionTypeFactory
341-
{
342-
return CollectionTypeFactory(typeof(TCollectionTypeFactory));
343-
}
344-
345-
/// <summary>
346-
/// Sets the proxyfactory.factory_class property.
347-
/// NOTE: NHibernate 2.1 only
348-
/// </summary>
349-
/// <param name="proxyFactoryFactoryClass">factory class</param>
350-
/// <returns>Configuration</returns>
351-
public TThisConfiguration ProxyFactoryFactory(string proxyFactoryFactoryClass)
352-
{
353-
values[ProxyFactoryFactoryClassKey] = proxyFactoryFactoryClass;
354-
return (TThisConfiguration)this;
355-
}
356-
357-
/// <summary>
358-
/// Sets the proxyfactory.factory_class property.
359-
/// NOTE: NHibernate 2.1 only
360-
/// </summary>
361-
/// <param name="proxyFactoryFactory">factory class</param>
362-
/// <returns>Configuration</returns>
363-
public TThisConfiguration ProxyFactoryFactory(Type proxyFactoryFactory)
364-
{
365-
values[ProxyFactoryFactoryClassKey] = proxyFactoryFactory.AssemblyQualifiedName;
366-
return (TThisConfiguration)this;
367-
}
368-
369-
/// <summary>
370-
/// Sets the proxyfactory.factory_class property.
371-
/// NOTE: NHibernate 2.1 only
372-
/// </summary>
373-
/// <typeparam name="TProxyFactoryFactory">factory class</typeparam>
374-
/// <returns>Configuration</returns>
375-
public TThisConfiguration ProxyFactoryFactory<TProxyFactoryFactory>() where TProxyFactoryFactory : IProxyFactoryFactory
376-
{
377-
return ProxyFactoryFactory(typeof(TProxyFactoryFactory));
378-
}
379-
380279
/// <summary>
381280
/// Sets the adonet.batch_size property.
382281
/// </summary>
@@ -388,27 +287,6 @@ public TThisConfiguration AdoNetBatchSize(int size)
388287
return (TThisConfiguration)this;
389288
}
390289

391-
/// <summary>
392-
/// Sets the current_session_context_class property.
393-
/// </summary>
394-
/// <param name="currentSessionContextClass">current session context class</param>
395-
/// <returns>Configuration</returns>
396-
public TThisConfiguration CurrentSessionContext(string currentSessionContextClass)
397-
{
398-
values[CurrentSessionContextClassKey] = currentSessionContextClass;
399-
return (TThisConfiguration)this;
400-
}
401-
402-
/// <summary>
403-
/// Sets the current_session_context_class property.
404-
/// </summary>
405-
/// <typeparam name="TSessionContext">Implementation of ICurrentSessionContext to use</typeparam>
406-
/// <returns>Configuration</returns>
407-
public TThisConfiguration CurrentSessionContext<TSessionContext>() where TSessionContext : NHibernate.Context.ICurrentSessionContext
408-
{
409-
return CurrentSessionContext(typeof(TSessionContext).AssemblyQualifiedName);
410-
}
411-
412290
/// <summary>
413291
/// Sets the connection isolation level. NHibernate setting: connection.isolation
414292
/// </summary>

0 commit comments

Comments
 (0)