Skip to content
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

Do not store mapping field in Configuration #3398

Merged
merged 16 commits into from
Aug 15, 2023
Merged
69 changes: 27 additions & 42 deletions src/NHibernate/Cfg/Configuration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ public Configuration(SerializationInfo info, StreamingContext context)
FilterDefinitions = GetSerialedObject<IDictionary<string, FilterDefinition>>(info, "filterDefinitions");
Imports = GetSerialedObject<IDictionary<string, string>>(info, "imports");
interceptor = GetSerialedObject<IInterceptor>(info, "interceptor");
mapping = GetSerialedObject<IMapping>(info, "mapping");
NamedQueries = GetSerialedObject<IDictionary<string, NamedQueryDefinition>>(info, "namedQueries");
NamedSQLQueries = GetSerialedObject<IDictionary<string, NamedSQLQueryDefinition>>(info, "namedSqlQueries");
namingStrategy = GetSerialedObject<INamingStrategy>(info, "namingStrategy");
Expand All @@ -124,7 +123,7 @@ public void GetObjectData(SerializationInfo info, StreamingContext context)
{
ConfigureProxyFactoryFactory();
SecondPassCompile();
Validate();
Validate(BuildMapping());
bahusoid marked this conversation as resolved.
Show resolved Hide resolved

info.AddValue("entityNotFoundDelegate", EntityNotFoundDelegate);

Expand All @@ -139,7 +138,6 @@ public void GetObjectData(SerializationInfo info, StreamingContext context)
info.AddValue("filterDefinitions", FilterDefinitions);
info.AddValue("imports", Imports);
info.AddValue("interceptor", interceptor);
info.AddValue("mapping", mapping);
info.AddValue("namedQueries", NamedQueries);
info.AddValue("namedSqlQueries", NamedSQLQueries);
info.AddValue("namingStrategy", namingStrategy);
Expand Down Expand Up @@ -238,18 +236,18 @@ public bool HasNonIdentifierPropertyNamedId(string className)
}

public Dialect.Dialect Dialect =>
NHibernate.Dialect.Dialect.GetDialect(configuration.Properties);
throw new InvalidOperationException("The dialect is not ready at this stage of the configuration.");
hazzik marked this conversation as resolved.
Show resolved Hide resolved
}

[Serializable]
private class StaticDialectMappingWrapper : IMapping
{
private readonly IMapping _mapping;

public StaticDialectMappingWrapper(IMapping mapping)
public StaticDialectMappingWrapper(IMapping mapping, Dialect.Dialect dialect)
{
_mapping = mapping;
Dialect = mapping.Dialect;
Dialect = dialect;
}

public IType GetIdentifierType(string className)
Expand All @@ -275,20 +273,12 @@ public bool HasNonIdentifierPropertyNamedId(string className)
public Dialect.Dialect Dialect { get; }
}

private IMapping mapping;

protected Configuration(SettingsFactory settingsFactory)
{
InitBlock();
this.settingsFactory = settingsFactory;
Reset();
}

private void InitBlock()
{
mapping = BuildMapping();
}

public virtual IMapping BuildMapping()
{
return new Mapping(this);
Expand Down Expand Up @@ -934,6 +924,9 @@ public static bool IncludeAction(SchemaAction actionsSource, SchemaAction includ
/// <param name="dialect"></param>
public string[] GenerateSchemaCreationScript(Dialect.Dialect dialect)
{
var m = BuildMapping();
var mapping = new StaticDialectMappingWrapper(m, dialect);

SecondPassCompile();

var defaultCatalog = GetQuotedDefaultCatalog(dialect);
Expand Down Expand Up @@ -1001,11 +994,11 @@ public string[] GenerateSchemaCreationScript(Dialect.Dialect dialect)
return script.ToArray();
}

private void Validate()
private void Validate(IMapping mapping)
{
ValidateEntities();
ValidateEntities(mapping);

ValidateCollections();
ValidateCollections(mapping);

ValidateFilterDefs();
}
Expand Down Expand Up @@ -1059,15 +1052,15 @@ private void ValidateFilterDefs()
}
}

private void ValidateCollections()
private void ValidateCollections(IMapping mapping)
{
foreach (var col in collections.Values)
{
col.Validate(mapping);
}
}

private void ValidateEntities()
private void ValidateEntities(IMapping mapping)
{
bool validateProxy = PropertiesHelper.GetBoolean(Environment.UseProxyValidator, properties, true);
HashSet<string> allProxyErrors = null;
Expand Down Expand Up @@ -1281,8 +1274,7 @@ protected virtual void ConfigureProxyFactoryFactory()
//http://nhibernate.jira.com/browse/NH-975

var ipff = Environment.BytecodeProvider as IInjectableProxyFactoryFactory;
string pffClassName;
properties.TryGetValue(Environment.ProxyFactoryFactoryClass, out pffClassName);
properties.TryGetValue(Environment.ProxyFactoryFactoryClass, out var pffClassName);
if (ipff != null && !string.IsNullOrEmpty(pffClassName))
{
ipff.SetProxyFactoryFactory(pffClassName);
Expand All @@ -1299,33 +1291,22 @@ protected virtual void ConfigureProxyFactoryFactory()
/// <returns>An <see cref="ISessionFactory" /> instance.</returns>
public ISessionFactory BuildSessionFactory()
{
var dynamicDialectMapping = mapping;
// Use a mapping which does store the dialect instead of instantiating a new one
// at each access. The dialect does not change while building a session factory.
// It furthermore allows some hack on NHibernate.Spatial side to go on working,
// See nhibernate/NHibernate.Spatial#104
mapping = new StaticDialectMappingWrapper(mapping);
try
{
ConfigureProxyFactoryFactory();
SecondPassCompile();
Validate();
Environment.VerifyProperties(properties);
Settings settings = BuildSettings();
var m = BuildMapping();
var settings = BuildSettings();
var mapping = new StaticDialectMappingWrapper(m, settings.Dialect);
hazzik marked this conversation as resolved.
Show resolved Hide resolved
ConfigureProxyFactoryFactory();
SecondPassCompile();
Validate(mapping);
Environment.VerifyProperties(properties);

// Ok, don't need schemas anymore, so free them
Schemas = null;
// Ok, don't need schemas anymore, so free them
Schemas = null;

return new SessionFactoryImpl(
this,
mapping,
settings,
GetInitializedEventListeners());
}
finally
{
mapping = dynamicDialectMapping;
}
return new SessionFactoryImpl(this, mapping, settings, GetInitializedEventListeners());
}

/// <summary>
Expand Down Expand Up @@ -2358,6 +2339,8 @@ private static T[] AppendListeners<T>(T[] existing, T[] listenersToAdd)
/// <seealso cref="NHibernate.Tool.hbm2ddl.SchemaUpdate"/>
public string[] GenerateSchemaUpdateScript(Dialect.Dialect dialect, IDatabaseMetadata databaseMetadata)
{
var m = BuildMapping();
var mapping = new StaticDialectMappingWrapper(m, dialect);
SecondPassCompile();

var defaultCatalog = GetQuotedDefaultCatalog(dialect);
Expand Down Expand Up @@ -2438,6 +2421,8 @@ public string[] GenerateSchemaUpdateScript(Dialect.Dialect dialect, IDatabaseMet

public void ValidateSchema(Dialect.Dialect dialect, IDatabaseMetadata databaseMetadata)
{
var m = BuildMapping();
var mapping = new StaticDialectMappingWrapper(m, dialect);
SecondPassCompile();

var defaultCatalog = GetQuotedDefaultCatalog(dialect);
Expand Down
Loading