Skip to content

Caching of Property Dialect in class Configuration.Mapping #1950

Closed
@ValResnick

Description

@ValResnick

This Property should be cached, because the creation of Dialect can be expensive.

This is our scenario: We have a base class with a lot of subclasses (over 400).

The class UnionSubclassEntityPersister creates for our base class a very long (over 20 Million characters) SubQuery in GenerateSubquery.

// from UnionSubclassEntityPersister
subquery = GenerateSubquery(persistentClass, mapping);

Everytime GetSqlTypeCode is called for an instance of NullableType the Dialect will be created again.

// from UnionSubclassEntityPersister.GenerateSubquery
var sqlType = col.GetSqlTypeCode(mapping);
// from NullableType
static SqlType OverrideSqlType(IMapping mapping, SqlType type)
{
   return mapping != null ? mapping.Dialect.OverrideSqlType(type) : type;
}

This is a possible solution for the problem.

[Serializable]
private class Mapping : IMapping
{
   private readonly Configuration configuration;

   [NonSerialized]
   private readonly Lazy<Dialect.Dialect> _lazyDialect;

   public Mapping(Configuration configuration)
   {
      this.configuration = configuration;
				
      _lazyDialect = new Lazy<Dialect.Dialect>(() => NHibernate.Dialect.Dialect.GetDialect(this.configuration.Properties));
   }
   
   ...

   public Dialect.Dialect Dialect => _lazyDialect.Value;
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions