Skip to content

ConfigurationFactoryBase

ConfigurationFactoryBase implements IConfigurationFactory and owns the connection-string-keyed caching and configuration-file deserialization logic shared by both database engines; OracleConfigurationFactory and SqlServerConfigurationFactory supply only the engine-specific pieces (driver/dialect configuration and the serialized-config file name).

Purpose

  • Caches built PersistenceConfiguration instances in a static ConcurrentDictionary<string, PersistenceConfiguration> keyed by connection string, so GetConfiguration is a cache hit on repeat calls for the same connection string.
  • Attempts to deserialize a previously-serialized NHibernate Configuration from a binary file (via BinaryFormatter) before building a fresh one, silently swallowing any deserialization exception and falling back to a fresh build — a behavior documented in Architecture as a design constraint rather than a business rule.
  • Delegates engine-specific bootstrap (Fluently.Configure() with the correct driver/dialect, assembly scanning for ClassMap<T>/.hbm.xml mappings) to the abstract GetNewConfiguration/GetConfigurationFileName members implemented by subclasses.

Context

  • Namespace: SAIF.Data.NHibernate
  • Assembly/package: SAIF.Framework.Data.NHibernate

Public members

Member Signature Purpose
GetConfiguration PersistenceConfiguration GetConfiguration(string connection, int batchSize, bool showSql) Returns a cached PersistenceConfiguration for connection if one exists; otherwise calls CreateConfiguration to build and cache one.

Protected members

Member Signature Purpose
CreateConfiguration protected PersistenceConfiguration CreateConfiguration(string connection, int batchSize, bool showSql) Attempts deserialization of a previously-serialized Configuration; on failure or absence, calls the engine-specific GetNewConfiguration, then sets Environment.ConnectionString/BatchSize/ShowSql.
GetSerializedConfiguration protected Configuration GetSerializedConfiguration() Deserializes a Configuration from {assemblyDirectory}\{GetConfigurationFileName()} using BinaryFormatter; returns null on any exception (including file-not-found).
GetNewConfiguration protected abstract Configuration GetNewConfiguration(bool formatSql) Engine-specific: configures Fluently.Configure() with the correct driver/dialect and scans for ClassMap<T>/.hbm.xml mappings. Implemented by OracleConfigurationFactory/SqlServerConfigurationFactory.
GetConfigurationFileName protected abstract string GetConfigurationFileName() Engine-specific: returns the serialized-config file name (Oracle.config or SqlServer.config). Implemented by OracleConfigurationFactory/SqlServerConfigurationFactory.

Behavioral notes

  • Deserialization failures are silently swallowed (caught Exception, configuration set to null, forcing a fresh build) — this affects startup latency after a corrupted or stale .config cache file but not correctness, since a fresh configuration is always built as a fallback.

Constraints and usage notes

  • Subclasses must implement GetNewConfiguration and GetConfigurationFileName; ConfigurationFactoryBase itself cannot be instantiated directly.
  • The cache (Configurations dictionary) is static and shared per-process across all connection strings passed to any instance of this class or its subclasses.

Sources

  • Factories/ConfigurationFactoryBase.cs