Skip to content

Commit 2a31626

Browse files
Merge pull request #2011 from fredericDelaporte/SpatialDialect
Use a statically resolved dialect when building the session factory
2 parents 9119714 + 8bc24b7 commit 2a31626

File tree

1 file changed

+60
-9
lines changed

1 file changed

+60
-9
lines changed

src/NHibernate/Cfg/Configuration.cs

Lines changed: 60 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,40 @@ public bool HasNonIdentifierPropertyNamedId(string className)
239239
NHibernate.Dialect.Dialect.GetDialect(configuration.Properties);
240240
}
241241

242+
[Serializable]
243+
private class StaticDialectMappingWrapper : IMapping
244+
{
245+
private readonly IMapping _mapping;
246+
247+
public StaticDialectMappingWrapper(IMapping mapping)
248+
{
249+
_mapping = mapping;
250+
Dialect = mapping.Dialect;
251+
}
252+
253+
public IType GetIdentifierType(string className)
254+
{
255+
return _mapping.GetIdentifierType(className);
256+
}
257+
258+
public string GetIdentifierPropertyName(string className)
259+
{
260+
return _mapping.GetIdentifierPropertyName(className);
261+
}
262+
263+
public IType GetReferencedPropertyType(string className, string propertyName)
264+
{
265+
return _mapping.GetReferencedPropertyType(className, propertyName);
266+
}
267+
268+
public bool HasNonIdentifierPropertyNamedId(string className)
269+
{
270+
return _mapping.HasNonIdentifierPropertyNamedId(className);
271+
}
272+
273+
public Dialect.Dialect Dialect { get; }
274+
}
275+
242276
private IMapping mapping;
243277

244278
protected Configuration(SettingsFactory settingsFactory)
@@ -1254,6 +1288,7 @@ protected virtual void ConfigureProxyFactoryFactory()
12541288

12551289
#endregion
12561290
}
1291+
12571292
/// <summary>
12581293
/// Instantiate a new <see cref="ISessionFactory" />, using the properties and mappings in this
12591294
/// configuration. The <see cref="ISessionFactory" /> will be immutable, so changes made to the
@@ -1262,17 +1297,33 @@ protected virtual void ConfigureProxyFactoryFactory()
12621297
/// <returns>An <see cref="ISessionFactory" /> instance.</returns>
12631298
public ISessionFactory BuildSessionFactory()
12641299
{
1300+
var dynamicDialectMapping = mapping;
1301+
// Use a mapping which does store the dialect instead of instantiating a new one
1302+
// at each access. The dialect does not change while building a session factory.
1303+
// It furthermore allows some hack on NHibernate.Spatial side to go on working,
1304+
// See nhibernate/NHibernate.Spatial#104
1305+
mapping = new StaticDialectMappingWrapper(mapping);
1306+
try
1307+
{
1308+
ConfigureProxyFactoryFactory();
1309+
SecondPassCompile();
1310+
Validate();
1311+
Environment.VerifyProperties(properties);
1312+
Settings settings = BuildSettings();
12651313

1266-
ConfigureProxyFactoryFactory();
1267-
SecondPassCompile();
1268-
Validate();
1269-
Environment.VerifyProperties(properties);
1270-
Settings settings = BuildSettings();
1271-
1272-
// Ok, don't need schemas anymore, so free them
1273-
Schemas = null;
1314+
// Ok, don't need schemas anymore, so free them
1315+
Schemas = null;
12741316

1275-
return new SessionFactoryImpl(this, mapping, settings, GetInitializedEventListeners());
1317+
return new SessionFactoryImpl(
1318+
this,
1319+
mapping,
1320+
settings,
1321+
GetInitializedEventListeners());
1322+
}
1323+
finally
1324+
{
1325+
mapping = dynamicDialectMapping;
1326+
}
12761327
}
12771328

12781329
/// <summary>

0 commit comments

Comments
 (0)