Skip to content

Remove dialect instantiation in AddDeserializedMapping #1703

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 7 commits into from
May 24, 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
5 changes: 2 additions & 3 deletions src/NHibernate.Test/Async/NHSpecificTest/NH1007/Fixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,12 @@ public async Task MappingIdGeneratorWithAttributeTakesPrecendenceOverMappingWith

private void VerifyMapping(HbmMapping mapping)
{
var dialect = new MsSql2008Dialect();
var configuration = new Configuration();
var mappings = configuration.CreateMappings(dialect);
var mappings = configuration.CreateMappings();
mappings.DefaultAssembly = "NHibernate.Test";
mappings.DefaultNamespace = "NHibernate.Test.NHSpecificTest.NH1007";

var rootBinder = new MappingRootBinder(mappings, dialect);
var rootBinder = new MappingRootBinder(mappings);
rootBinder.Bind(mapping);

var employer = rootBinder.Mappings.GetClass("NHibernate.Test.NHSpecificTest.NH1007.Employer1");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,7 @@ public void AllowCustomCollectionTypeFactoryBeforeBuildFirstMapping()
// "mapping-sources" is better to limitate the moment of injectability.
var cfg = TestConfigurationHelper.GetDefaultConfiguration();
cfg.SetProperty(Environment.CollectionTypeFactoryClass, typeof(CustomCollectionTypeFactory).AssemblyQualifiedName);
Dialect.Dialect dialect = Dialect.Dialect.GetDialect(cfg.Properties);
cfg.CreateMappings(dialect);
cfg.CreateMappings();
Assert.That(Environment.BytecodeProvider.CollectionTypeFactory, Is.TypeOf<CustomCollectionTypeFactory>());
}

Expand Down
2 changes: 0 additions & 2 deletions src/NHibernate.Test/CfgTest/ConfigurationAddMappingEvents.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ public void WhenSubscribedToBeforeBindThenRaiseEventForEachMapping()
Assert.That(listOfCalls.Count, Is.EqualTo(2));
Assert.That(listOfCalls.Select(x => x.FileName).All(x => x != null), Is.True);
Assert.That(listOfCalls.Select(x => x.Mapping).All(x => x != null), Is.True);
Assert.That(listOfCalls.Select(x => x.Dialect).All(x => x.GetType() == typeof (MsSql2008Dialect)), Is.True);
}

[Test]
Expand All @@ -70,7 +69,6 @@ public void WhenSubscribedToAfterBindThenRaiseEventForEachMapping()
Assert.That(listOfCalls.Count, Is.EqualTo(2));
Assert.That(listOfCalls.Select(x => x.FileName).All(x => x != null), Is.True);
Assert.That(listOfCalls.Select(x => x.Mapping).All(x => x != null), Is.True);
Assert.That(listOfCalls.Select(x => x.Dialect).All(x => x.GetType() == typeof(MsSql2008Dialect)), Is.True);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public void AddTypeDef()
c.Alias = "HighLow";
c.Properties = new {max_lo = 99};
});
var mappings = configure.CreateMappings(Dialect.Dialect.GetDialect(configure.Properties));
var mappings = configure.CreateMappings();
var typeDef = mappings.GetTypeDef("HighLow");
Assert.That(typeDef, Is.Not.Null);
Assert.That(typeDef.Parameters["max_lo"], Is.EqualTo("99"));
Expand Down
42 changes: 35 additions & 7 deletions src/NHibernate.Test/NHSpecificTest/AliasFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,44 @@ public class AliasFixture
[Test]
public void NoLeadingUnderscores()
{
Alias alias = new Alias("suffix");
var alias = new Alias("suffix");
Dialect.Dialect dialect = new MsSql2000Dialect();

Assert.IsFalse(
alias.ToAliasString("__someIdentifier", dialect)
.StartsWith("_"));
Assert.That(alias.ToAliasString("__someIdentifier", dialect), Does.Not.StartWith("_"));

Assert.IsFalse(
alias.ToUnquotedAliasString("__someIdentifier", dialect)
.StartsWith("_"));
Assert.That(alias.ToUnquotedAliasString("__someIdentifier", dialect), Does.Not.StartsWith("_"));
}

[Test]
public void TestDialectQuotedAlias()
{
var alias = new Alias(15, "PK");
var aliasString = alias.ToAliasString("[Hello]]World]", new MsSql2000Dialect());
Assert.That(aliasString, Is.EqualTo("[Hello]]WorldPK]"));
}

[Test]
public void TestDialectQuotedAliasWithEscapedStringAtTheEdge()
{
var alias = new Alias(15, "PK");
var aliasString = alias.ToAliasString("[Table0123456]]7890]", new MsSql2000Dialect());
Assert.That(aliasString, Is.EqualTo("[Table0123456]]PK]"));
}

[Test]
public void TestQuotedAlias()
{
var alias = new Alias(15, "PK");
var aliasString = alias.ToAliasString("[Hello]]World]");
Assert.That(aliasString, Is.EqualTo("[Hello]]WorldPK]"));
}

[Test]
public void TestQuotedAliasWithEscapedStringAtTheEdge()
{
var alias = new Alias(15, "PK");
var aliasString = alias.ToAliasString("[Table0123456]]7890]");
Assert.That(aliasString, Is.EqualTo("[Table0123456PK]"));
}
}
}
5 changes: 2 additions & 3 deletions src/NHibernate.Test/NHSpecificTest/NH1007/Fixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,12 @@ public void ConfiguringIdGeneratorUsingAttributeAndElementIsEquivalent()

private void VerifyMapping(HbmMapping mapping)
{
var dialect = new MsSql2008Dialect();
var configuration = new Configuration();
var mappings = configuration.CreateMappings(dialect);
var mappings = configuration.CreateMappings();
mappings.DefaultAssembly = "NHibernate.Test";
mappings.DefaultNamespace = "NHibernate.Test.NHSpecificTest.NH1007";

var rootBinder = new MappingRootBinder(mappings, dialect);
var rootBinder = new MappingRootBinder(mappings);
rootBinder.Bind(mapping);

var employer = rootBinder.Mappings.GetClass("NHibernate.Test.NHSpecificTest.NH1007.Employer1");
Expand Down
2 changes: 1 addition & 1 deletion src/NHibernate.Test/NHSpecificTest/NH2041/Fixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public void WhenJoinTableContainComponentsThenColumnsShouldBeInJoinedTable()
{
Configuration cfg = TestConfigurationHelper.GetDefaultConfiguration();
cfg.AddResource("NHibernate.Test.NHSpecificTest.NH2041.Mappings.hbm.xml", GetType().Assembly);
var mappings = cfg.CreateMappings(Dialect.Dialect.GetDialect(cfg.Properties));
var mappings = cfg.CreateMappings();
var table = mappings.GetTable(null, null, "Locations");
Assert.That(table, Is.Not.Null);
Assert.That(table.ColumnIterator.Select(c => c.Name), Is.EquivalentTo(new [] {"myclassId", "latitudecol", "longitudecol"}));
Expand Down
2 changes: 1 addition & 1 deletion src/NHibernate.Test/NHSpecificTest/NH2569/Fixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public void WhenMapHiloToDifferentSchemaThanClassThenIdHasTheMappedSchema()
conf.DataBaseIntegration(x=> x.Dialect<MsSql2008Dialect>());
conf.AddDeserializedMapping(mapper.CompileMappingForAllExplicitlyAddedEntities(), "wholeDomain");

var mappings = conf.CreateMappings(Dialect.Dialect.GetDialect());
var mappings = conf.CreateMappings();
var pc = mappings.GetClass(typeof(MyClass).FullName);
Assert.That(((SimpleValue)pc.Identifier).IdentifierGeneratorProperties["schema"], Is.EqualTo("gSchema"));
}
Expand Down
2 changes: 1 addition & 1 deletion src/NHibernate.Test/NHSpecificTest/NH2761/Fixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public void WhenMultipleConfigurationUseSameMappingsThenConstraintsHasSameNames(
private Mappings GetMappings()
{
var configuration = GetConfiguration();
return configuration.CreateMappings(Dialect.Dialect.GetDialect(configuration.Properties));
return configuration.CreateMappings();
}

private Configuration GetConfiguration()
Expand Down
4 changes: 2 additions & 2 deletions src/NHibernate.Test/NHSpecificTest/NH3153/Fixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public void ShouldGetIdentifierSchemaFromClassElement()
var conf = TestConfigurationHelper.GetDefaultConfiguration();
conf.AddResource("NHibernate.Test.NHSpecificTest.NH3153.SchemaInClass.hbm.xml", typeof(A).Assembly);

var mappings = conf.CreateMappings(Dialect.Dialect.GetDialect());
var mappings = conf.CreateMappings();

var pc = mappings.GetClass(typeof(A).FullName);
Assert.That(((SimpleValue)pc.Identifier).IdentifierGeneratorProperties["schema"], Is.EqualTo("Test"));
Expand All @@ -30,7 +30,7 @@ public void ShouldGetIdentifierSchemaFromMappingElement()
var conf = TestConfigurationHelper.GetDefaultConfiguration();
conf.AddResource("NHibernate.Test.NHSpecificTest.NH3153.SchemaInMapping.hbm.xml", typeof(A).Assembly);

var mappings = conf.CreateMappings(Dialect.Dialect.GetDialect());
var mappings = conf.CreateMappings();

var pc = mappings.GetClass(typeof(A).FullName);
Assert.That(((SimpleValue)pc.Identifier).IdentifierGeneratorProperties["schema"], Is.EqualTo("Test"));
Expand Down
21 changes: 16 additions & 5 deletions src/NHibernate/Cfg/BindMappingEventArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,28 @@

namespace NHibernate.Cfg
{
public class BindMappingEventArgs: EventArgs
public class BindMappingEventArgs : EventArgs
{
//6.0 TODO: Remove
internal Lazy<Dialect.Dialect> LazyDialect;

[Obsolete("Please use constructor without a dialect parameter.")]
public BindMappingEventArgs(Dialect.Dialect dialect, HbmMapping mapping, string fileName)
: this(mapping, fileName)
{
LazyDialect = new Lazy<Dialect.Dialect>(() => dialect);
}

public BindMappingEventArgs(HbmMapping mapping, string fileName)
{
Dialect = dialect;
Mapping = mapping;
FileName = fileName;
}

public Dialect.Dialect Dialect { get; private set; }
public HbmMapping Mapping { get; private set; }
public string FileName { get; private set; }
//Since v5.2
[Obsolete("This property will be removed in a future version.")]
public Dialect.Dialect Dialect => LazyDialect.Value;
public HbmMapping Mapping { get; }
public string FileName { get; }
}
}
23 changes: 16 additions & 7 deletions src/NHibernate/Cfg/Configuration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -518,12 +518,12 @@ public void AddDeserializedMapping(HbmMapping mappingDocument, string documentFi
}
try
{
Dialect.Dialect dialect = Dialect.Dialect.GetDialect(properties);
OnBeforeBindMapping(new BindMappingEventArgs(dialect, mappingDocument, documentFileName));
Mappings mappings = CreateMappings(dialect);

new MappingRootBinder(mappings, dialect).Bind(mappingDocument);
OnAfterBindMapping(new BindMappingEventArgs(dialect, mappingDocument, documentFileName));
var dialect = new Lazy<Dialect.Dialect>(() => Dialect.Dialect.GetDialect(properties));
OnBeforeBindMapping(new BindMappingEventArgs(mappingDocument, documentFileName) {LazyDialect = dialect});
var mappings = CreateMappings();
mappings.LazyDialect = dialect;
new MappingRootBinder(mappings).Bind(mappingDocument);
OnAfterBindMapping(new BindMappingEventArgs(mappingDocument, documentFileName) {LazyDialect = dialect});
}
catch (Exception e)
{
Expand Down Expand Up @@ -561,7 +561,16 @@ private void OnBeforeBindMapping(BindMappingEventArgs bindMappingEventArgs)
/// Create a new <see cref="Mappings" /> to add classes and collection
/// mappings to.
/// </summary>
//Since v5.2
[Obsolete("Please use overload without a dialect parameter.")]
public Mappings CreateMappings(Dialect.Dialect dialect)
{
var mappings = CreateMappings();
mappings.LazyDialect = new Lazy<Dialect.Dialect>(() => dialect);
return mappings;
}

public Mappings CreateMappings()
{
string defaultCatalog = PropertiesHelper.GetString(Environment.DefaultCatalog, properties, null);
string defaultSchema = PropertiesHelper.GetString(Environment.DefaultSchema, properties, null);
Expand All @@ -571,7 +580,7 @@ public Mappings CreateMappings(Dialect.Dialect dialect)
return new Mappings(classes, collections, tables, NamedQueries, NamedSQLQueries, SqlResultSetMappings, Imports,
secondPasses, filtersSecondPasses, propertyReferences, namingStrategy, typeDefs, FilterDefinitions, extendsQueue,
auxiliaryDatabaseObjects, tableNameBinding, columnNameBindingPerTable, defaultAssembly,
defaultNamespace, defaultCatalog, defaultSchema, preferPooledValuesLo, dialect);
defaultNamespace, defaultCatalog, defaultSchema, preferPooledValuesLo);
}

private void ProcessPreMappingBuildProperties()
Expand Down
9 changes: 2 additions & 7 deletions src/NHibernate/Cfg/ConfigurationExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,8 @@ public static Configuration TypeDefinition<TDef>(this Configuration configuratio
{
return configuration;
}
var mappings = GetMappings(configuration);
var mappings = configuration.CreateMappings();
mappings.LazyDialect = new Lazy<Dialect.Dialect>(() => Dialect.Dialect.GetDialect(configuration.Properties));
mappings.AddTypeDef(tdConfiguration.Alias, typeof(TDef).AssemblyQualifiedName, tdConfiguration.Properties.ToTypeParameters());
return configuration;
}
Expand All @@ -163,11 +164,5 @@ public static Configuration AddNamedQuery(this Configuration configuration, stri
configuration.NamedQueries.Add(queryIdentifier, builder.Build());
return configuration;
}

private static Mappings GetMappings(Configuration configuration)
{
Dialect.Dialect dialect = Dialect.Dialect.GetDialect(configuration.Properties);
return configuration.CreateMappings(dialect);
}
}
}
72 changes: 62 additions & 10 deletions src/NHibernate/Cfg/Mappings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ public sealed class PropertyReference
private string catalogName;
private string defaultCascade;
private string defaultNamespace;
private readonly Dialect.Dialect dialect;
private string defaultAssembly;
private string defaultAccess;
private bool autoImport;
Expand Down Expand Up @@ -89,6 +88,11 @@ public sealed class PropertyReference
/// </summary>
protected internal IDictionary<string, TableDescription> tableNameBinding;

//6.0 TODO: Remove
internal Lazy<Dialect.Dialect> LazyDialect;

//Since v5.2
[Obsolete("Please use constructor without a dialect parameter.")]
protected internal Mappings(
IDictionary<string, PersistentClass> classes,
IDictionary<string, Mapping.Collection> collections,
Expand All @@ -112,7 +116,57 @@ protected internal Mappings(
string defaultCatalog,
string defaultSchema,
string preferPooledValuesLo,
Dialect.Dialect dialect)
Dialect.Dialect dialect) :
this(
classes,
collections,
tables,
queries,
sqlqueries,
resultSetMappings,
imports,
secondPasses,
filtersSecondPasses,
propertyReferences,
namingStrategy,
typeDefs,
filterDefinitions,
extendsQueue,
auxiliaryDatabaseObjects,
tableNameBinding,
columnNameBindingPerTable,
defaultAssembly,
defaultNamespace,
defaultCatalog,
defaultSchema,
preferPooledValuesLo)
{
LazyDialect = new Lazy<Dialect.Dialect>(() => dialect);
}

protected internal Mappings(
IDictionary<string, PersistentClass> classes,
IDictionary<string, Mapping.Collection> collections,
IDictionary<string, Table> tables,
IDictionary<string, NamedQueryDefinition> queries,
IDictionary<string, NamedSQLQueryDefinition> sqlqueries,
IDictionary<string, ResultSetMappingDefinition> resultSetMappings,
IDictionary<string, string> imports,
IList<SecondPassCommand> secondPasses,
Queue<FilterSecondPassArgs> filtersSecondPasses,
IList<PropertyReference> propertyReferences,
INamingStrategy namingStrategy,
IDictionary<string, TypeDef> typeDefs,
IDictionary<string, FilterDefinition> filterDefinitions,
ISet<ExtendsQueueEntry> extendsQueue,
IList<IAuxiliaryDatabaseObject> auxiliaryDatabaseObjects,
IDictionary<string, TableDescription> tableNameBinding,
IDictionary<Table, ColumnNames> columnNameBindingPerTable,
string defaultAssembly,
string defaultNamespace,
string defaultCatalog,
string defaultSchema,
string preferPooledValuesLo)
{
this.classes = classes;
this.collections = collections;
Expand All @@ -135,7 +189,6 @@ protected internal Mappings(
DefaultCatalog = defaultCatalog;
DefaultSchema = defaultSchema;
PreferPooledValuesLo = preferPooledValuesLo;
this.dialect = dialect;
this.filtersSecondPasses = filtersSecondPasses;
}

Expand Down Expand Up @@ -183,10 +236,9 @@ public PersistentClass GetClass(string className)
return result;
}

public Dialect.Dialect Dialect
{
get { return dialect; }
}
//Since v5.2
[Obsolete("This property will be removed in a future version.")]
public Dialect.Dialect Dialect => LazyDialect.Value;

/// <summary>
///
Expand Down Expand Up @@ -260,7 +312,7 @@ public void AddImport(string className, string rename)

public Table AddTable(string schema, string catalog, string name, string subselect, bool isAbstract, string schemaAction)
{
string key = subselect ?? dialect.Qualify(catalog, schema, name);
string key = subselect ?? BuildTableNameKey(catalog, schema, name);
Copy link
Member Author

@hazzik hazzik May 21, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The "Keys" property of tables dictionary and so key here are the internal things and are not exposed outside, so we are not concerning by dialect specific qualification of it (eg catalog.schema.table in SQL Server vs catalog_schema_table in SQLite and SQL Server Compact).

Table table;
if (!tables.TryGetValue(key, out table))
{
Expand Down Expand Up @@ -327,7 +379,7 @@ private static SchemaAction GetSchemaActions(string schemaAction)

public Table AddDenormalizedTable(string schema, string catalog, string name, bool isAbstract, string subselect, Table includedTable)
{
string key = subselect ?? dialect.Qualify(schema, catalog, name);
string key = subselect ?? BuildTableNameKey(schema, catalog, name);

Table table = new DenormalizedTable(includedTable)
{
Expand Down Expand Up @@ -370,7 +422,7 @@ public void AddTableBinding(string schema, string catalog, string logicalName, s

public Table GetTable(string schema, string catalog, string name)
{
string key = dialect.Qualify(catalog, schema, name);
string key = BuildTableNameKey(catalog, schema, name);
return tables[key];
}

Expand Down
Loading