From 87e57e19a549e4ad5ae58be5672c017153682df4 Mon Sep 17 00:00:00 2001 From: Ben Fulton Date: Wed, 3 Aug 2011 21:31:31 -0400 Subject: [PATCH] Fix line endings, I hope. --- .gitattributes | 3 +- .../Cfg/Db/PersistenceConfigurationTester.cs | 414 ++++---- .../Cfg/Db/PersistenceConfiguration.cs | 910 +++++++++--------- 3 files changed, 664 insertions(+), 663 deletions(-) diff --git a/.gitattributes b/.gitattributes index f993ac866..d605d2aa4 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1 +1,2 @@ -"* text=false" +[core] + autocrlf = false diff --git a/src/FluentNHibernate.Testing/Cfg/Db/PersistenceConfigurationTester.cs b/src/FluentNHibernate.Testing/Cfg/Db/PersistenceConfigurationTester.cs index fe07ea1ca..4bccb45e2 100644 --- a/src/FluentNHibernate.Testing/Cfg/Db/PersistenceConfigurationTester.cs +++ b/src/FluentNHibernate.Testing/Cfg/Db/PersistenceConfigurationTester.cs @@ -1,208 +1,208 @@ -using FluentNHibernate.Cfg.Db; -using NHibernate.Cfg; -using NUnit.Framework; - -namespace FluentNHibernate.Testing.Cfg.Db -{ - [TestFixture] - public class PersistenceConfigurationTester - { - #region Test Setup - private ConfigTester _config; - private Configuration _nhibConfig; - - [SetUp] - public void SetUp() - { - _nhibConfig = null; - _config = new ConfigTester(); - } - - public string ValueOf(string key) - { - if( _nhibConfig == null ) - { - _nhibConfig = new Configuration(); - _config.ConfigureProperties(_nhibConfig); - } - - return _nhibConfig.GetProperty(key); - } - #endregion - - [Test] - public void ConfigureProperties_should_not_override_values_already_set() - { - _nhibConfig = new Configuration(); - _nhibConfig.Properties["proxyfactory.factory_class"] = "foo"; - ValueOf("proxyfactory.factory_class").ShouldEqual("foo"); - - } - - [Test] - public void ConfigureProperties_should_override_values_already_set_with_values_set_in_code() - { - _nhibConfig = new Configuration(); - _nhibConfig.Properties["proxyfactory.factory_class"] = "foo"; - _config.ProxyFactoryFactory("bar").ConfigureProperties(_nhibConfig); - ValueOf("proxyfactory.factory_class").ShouldEqual("bar"); - - } - - [Test] - public void Setting_raw_values_should_populate_dictionary() - { - _config.Raw("TESTKEY", "TESTVALUE"); - - ValueOf("TESTKEY").ShouldEqual("TESTVALUE"); - } - - [Test] - public void Dialect_should_set_both_old_and_new_dialect_keys() - { - _config.Dialect("foo"); - - ValueOf("dialect").ShouldEqual("foo"); - ValueOf("hibernate.dialect").ShouldEqual("foo"); - } - - [Test] - public void Default_Schema_should_be_set_to_schema_name() - { - _config.DefaultSchema("foo"); - ValueOf("default_schema").ShouldEqual("foo"); - } - - [Test] - public void UseOuterJoin_should_set_value_to_const_true() - { - _config.UseOuterJoin(); - ValueOf("use_outer_join").ShouldEqual("true"); - } - - [Test] - public void Max_Fetch_Depth_should_set_property_value() - { - _config.MaxFetchDepth(2); - ValueOf("max_fetch_depth").ShouldEqual("2"); - } - - [Test] - public void Use_Reflection_Optimizer_should_set_value_to_const_true() - { - _config.UseReflectionOptimizer(); - ValueOf("use_reflection_optimizer").ShouldEqual("true"); - - } - - [Test] - public void Provider_Class_should_set_property_value() - { - _config.Cache(c => c - .ProviderClass("foo")); - ValueOf("cache.provider_class").ShouldEqual("foo"); - - } - - [Test] - public void Use_Minimal_Puts_should_set_value_to_const_true() - { - _config.Cache(c => c - .UseMinimalPuts()); - ValueOf("cache.use_minimal_puts").ShouldEqual("true"); - } - - [Test] - public void Use_Query_Cache_should_set_value_to_const_true() - { - _config.Cache(c => c - .UseQueryCache()); - ValueOf("cache.use_query_cache").ShouldEqual("true"); - } - - [Test] - public void Query_Cache_Factory_should_set_property_value() - { - _config.Cache(c => c - .QueryCacheFactory("foo")); - ValueOf("cache.query_cache_factory").ShouldEqual("foo"); - } - - [Test] - public void Region_Prefix_should_set_property_value() - { - _config.Cache(c => c - .RegionPrefix("foo")); - ValueOf("cache.region_prefix").ShouldEqual("foo"); - } - - [Test] - public void Query_Substitutions_should_set_property_value() - { - _config.QuerySubstitutions("foo"); - ValueOf("query.substitutions").ShouldEqual("foo"); - } - - [Test] - public void Show_Sql_should_set_value_to_const_true() - { - _config.ShowSql(); - ValueOf("show_sql").ShouldEqual("true"); - } - - [Test] - public void Format_Sql_should_set_value_to_const_true() - { - _config.FormatSql(); - ValueOf("format_sql").ShouldEqual("true"); - } - - [Test] - public void DoNot_ShowSql_should_set_the_property_to_const_false() - { - _config.DoNot.ShowSql(); - ValueOf("show_sql").ShouldEqual("false"); - } - - [Test] - public void DoNot_Should_only_affect_next_property() - { - _config.DoNot.ShowSql().UseReflectionOptimizer(); - ValueOf("show_sql").ShouldEqual("false"); - ValueOf("use_reflection_optimizer").ShouldEqual("true"); - } - - [Test] - public void Repeated_DoNot_Calls() - { - _config.DoNot.ShowSql().DoNot.UseReflectionOptimizer(); - ValueOf("show_sql").ShouldEqual("false"); - ValueOf("use_reflection_optimizer").ShouldEqual("false"); - } - - [Test] - public void DoNot_UseReflectionOptimizer_should_set_the_property_to_const_false() - { - _config.DoNot.UseReflectionOptimizer(); - ValueOf("use_reflection_optimizer").ShouldEqual("false"); - } - - [Test] - public void DoNot_UseOuterJoin_should_set_the_property_to_const_false() - { - _config.DoNot.UseOuterJoin(); - ValueOf("use_outer_join").ShouldEqual("false"); - } - - [Test] - public void AdoNetBatchSize_should_set_property_value() - { - _config.AdoNetBatchSize(500); - ValueOf("adonet.batch_size").ShouldEqual("500"); - } - - public class ConfigTester : PersistenceConfiguration - { - } - } +using FluentNHibernate.Cfg.Db; +using NHibernate.Cfg; +using NUnit.Framework; + +namespace FluentNHibernate.Testing.Cfg.Db +{ + [TestFixture] + public class PersistenceConfigurationTester + { + #region Test Setup + private ConfigTester _config; + private Configuration _nhibConfig; + + [SetUp] + public void SetUp() + { + _nhibConfig = null; + _config = new ConfigTester(); + } + + public string ValueOf(string key) + { + if( _nhibConfig == null ) + { + _nhibConfig = new Configuration(); + _config.ConfigureProperties(_nhibConfig); + } + + return _nhibConfig.GetProperty(key); + } + #endregion + + [Test] + public void ConfigureProperties_should_not_override_values_already_set() + { + _nhibConfig = new Configuration(); + _nhibConfig.Properties["proxyfactory.factory_class"] = "foo"; + ValueOf("proxyfactory.factory_class").ShouldEqual("foo"); + + } + + [Test] + public void ConfigureProperties_should_override_values_already_set_with_values_set_in_code() + { + _nhibConfig = new Configuration(); + _nhibConfig.Properties["proxyfactory.factory_class"] = "foo"; + _config.ProxyFactoryFactory("bar").ConfigureProperties(_nhibConfig); + ValueOf("proxyfactory.factory_class").ShouldEqual("bar"); + + } + + [Test] + public void Setting_raw_values_should_populate_dictionary() + { + _config.Raw("TESTKEY", "TESTVALUE"); + + ValueOf("TESTKEY").ShouldEqual("TESTVALUE"); + } + + [Test] + public void Dialect_should_set_both_old_and_new_dialect_keys() + { + _config.Dialect("foo"); + + ValueOf("dialect").ShouldEqual("foo"); + ValueOf("hibernate.dialect").ShouldEqual("foo"); + } + + [Test] + public void Default_Schema_should_be_set_to_schema_name() + { + _config.DefaultSchema("foo"); + ValueOf("default_schema").ShouldEqual("foo"); + } + + [Test] + public void UseOuterJoin_should_set_value_to_const_true() + { + _config.UseOuterJoin(); + ValueOf("use_outer_join").ShouldEqual("true"); + } + + [Test] + public void Max_Fetch_Depth_should_set_property_value() + { + _config.MaxFetchDepth(2); + ValueOf("max_fetch_depth").ShouldEqual("2"); + } + + [Test] + public void Use_Reflection_Optimizer_should_set_value_to_const_true() + { + _config.UseReflectionOptimizer(); + ValueOf("use_reflection_optimizer").ShouldEqual("true"); + + } + + [Test] + public void Provider_Class_should_set_property_value() + { + _config.Cache(c => c + .ProviderClass("foo")); + ValueOf("cache.provider_class").ShouldEqual("foo"); + + } + + [Test] + public void Use_Minimal_Puts_should_set_value_to_const_true() + { + _config.Cache(c => c + .UseMinimalPuts()); + ValueOf("cache.use_minimal_puts").ShouldEqual("true"); + } + + [Test] + public void Use_Query_Cache_should_set_value_to_const_true() + { + _config.Cache(c => c + .UseQueryCache()); + ValueOf("cache.use_query_cache").ShouldEqual("true"); + } + + [Test] + public void Query_Cache_Factory_should_set_property_value() + { + _config.Cache(c => c + .QueryCacheFactory("foo")); + ValueOf("cache.query_cache_factory").ShouldEqual("foo"); + } + + [Test] + public void Region_Prefix_should_set_property_value() + { + _config.Cache(c => c + .RegionPrefix("foo")); + ValueOf("cache.region_prefix").ShouldEqual("foo"); + } + + [Test] + public void Query_Substitutions_should_set_property_value() + { + _config.QuerySubstitutions("foo"); + ValueOf("query.substitutions").ShouldEqual("foo"); + } + + [Test] + public void Show_Sql_should_set_value_to_const_true() + { + _config.ShowSql(); + ValueOf("show_sql").ShouldEqual("true"); + } + + [Test] + public void Format_Sql_should_set_value_to_const_true() + { + _config.FormatSql(); + ValueOf("format_sql").ShouldEqual("true"); + } + + [Test] + public void DoNot_ShowSql_should_set_the_property_to_const_false() + { + _config.DoNot.ShowSql(); + ValueOf("show_sql").ShouldEqual("false"); + } + + [Test] + public void DoNot_Should_only_affect_next_property() + { + _config.DoNot.ShowSql().UseReflectionOptimizer(); + ValueOf("show_sql").ShouldEqual("false"); + ValueOf("use_reflection_optimizer").ShouldEqual("true"); + } + + [Test] + public void Repeated_DoNot_Calls() + { + _config.DoNot.ShowSql().DoNot.UseReflectionOptimizer(); + ValueOf("show_sql").ShouldEqual("false"); + ValueOf("use_reflection_optimizer").ShouldEqual("false"); + } + + [Test] + public void DoNot_UseReflectionOptimizer_should_set_the_property_to_const_false() + { + _config.DoNot.UseReflectionOptimizer(); + ValueOf("use_reflection_optimizer").ShouldEqual("false"); + } + + [Test] + public void DoNot_UseOuterJoin_should_set_the_property_to_const_false() + { + _config.DoNot.UseOuterJoin(); + ValueOf("use_outer_join").ShouldEqual("false"); + } + + [Test] + public void AdoNetBatchSize_should_set_property_value() + { + _config.AdoNetBatchSize(500); + ValueOf("adonet.batch_size").ShouldEqual("500"); + } + + public class ConfigTester : PersistenceConfiguration + { + } + } } \ No newline at end of file diff --git a/src/FluentNHibernate/Cfg/Db/PersistenceConfiguration.cs b/src/FluentNHibernate/Cfg/Db/PersistenceConfiguration.cs index 40457feb5..97fddbecf 100644 --- a/src/FluentNHibernate/Cfg/Db/PersistenceConfiguration.cs +++ b/src/FluentNHibernate/Cfg/Db/PersistenceConfiguration.cs @@ -1,456 +1,456 @@ -using System; -using System.Collections.Generic; -using System.Data; -using System.Linq; -using NHibernate.Bytecode; -using NHibernate.Connection; -using NHibernate.Dialect; -using NHibernate.Driver; -using NHibConfiguration = NHibernate.Cfg.Configuration; - -namespace FluentNHibernate.Cfg.Db -{ - public abstract class PersistenceConfiguration : PersistenceConfiguration - where TThisConfiguration : PersistenceConfiguration - {} - - public abstract class PersistenceConfiguration : IPersistenceConfigurer - where TThisConfiguration : PersistenceConfiguration - where TConnectionString : ConnectionStringBuilder, new() - { - protected const string DialectKey = "dialect"; // Newer one, but not supported by everything - protected const string AltDialectKey = "hibernate.dialect"; // Some older NHib tools require this - protected const string DefaultSchemaKey = "default_schema"; - protected const string UseOuterJoinKey = "use_outer_join"; - protected const string MaxFetchDepthKey = "max_fetch_depth"; - protected const string UseReflectionOptimizerKey = "use_reflection_optimizer"; - protected const string QuerySubstitutionsKey = "query.substitutions"; - protected const string ShowSqlKey = "show_sql"; - protected const string FormatSqlKey = "format_sql"; - - protected const string CollectionTypeFactoryClassKey = NHibernate.Cfg.Environment.CollectionTypeFactoryClass; - protected const string ConnectionProviderKey = "connection.provider"; - protected const string DefaultConnectionProviderClassName = "NHibernate.Connection.DriverConnectionProvider"; - protected const string DriverClassKey = "connection.driver_class"; - protected const string ConnectionStringKey = "connection.connection_string"; - const string IsolationLevelKey = "connection.isolation"; - protected const string ProxyFactoryFactoryClassKey = "proxyfactory.factory_class"; - protected const string DefaultProxyFactoryFactoryClassName = "NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle"; - protected const string AdoNetBatchSizeKey = "adonet.batch_size"; - protected const string CurrentSessionContextClassKey = "current_session_context_class"; - - private readonly Dictionary values = new Dictionary(); - - private bool nextBoolSettingValue = true; - private readonly TConnectionString connectionString; - private readonly CacheSettingsBuilder cache = new CacheSettingsBuilder(); - - protected PersistenceConfiguration() - { - values[ConnectionProviderKey] = DefaultConnectionProviderClassName; - values[ProxyFactoryFactoryClassKey] = DefaultProxyFactoryFactoryClassName; - connectionString = new TConnectionString(); - } - - protected virtual IDictionary CreateProperties() - { - if (connectionString.IsDirty) - Raw(ConnectionStringKey, connectionString.Create()); - - if (cache.IsDirty) - { - foreach (var pair in cache.Create()) - { - Raw(pair.Key, pair.Value); - } - } - - return values; - } - - static IEnumerable OverridenDefaults(IDictionary settings) - { - if (settings[ConnectionProviderKey] != DefaultConnectionProviderClassName) - yield return ConnectionProviderKey; - - if (settings[ProxyFactoryFactoryClassKey] != DefaultProxyFactoryFactoryClassName) - yield return ProxyFactoryFactoryClassKey; - } - - private static IEnumerable KeysToPreserve(NHibConfiguration nhibernateConfig, IDictionary settings) - { - var @default = new NHibConfiguration(); - return nhibernateConfig.Properties - .Except(@default.Properties) - .Select(k => k.Key) - .Except(OverridenDefaults(settings)); - } - - public NHibConfiguration ConfigureProperties(NHibConfiguration nhibernateConfig) - { - var settings = CreateProperties(); - var keepers = KeysToPreserve(nhibernateConfig, settings); - - foreach (var kv in settings.Where(s => !keepers.Contains(s.Key))) - { - nhibernateConfig.SetProperty(kv.Key, kv.Value); - } - - return nhibernateConfig; - } - - public IDictionary ToProperties() - { - return CreateProperties(); - } - - protected void ToggleBooleanSetting(string settingKey) - { - var value = nextBoolSettingValue.ToString().ToLowerInvariant(); - - values[settingKey] = value; - - nextBoolSettingValue = true; - } - - /// - /// Negates the next boolean option. - /// - public TThisConfiguration DoNot - { - get - { - nextBoolSettingValue = false; - return (TThisConfiguration)this; - } - } - - /// - /// Sets the database dialect. This shouldn't be necessary - /// if you've used one of the provided database configurations. - /// - /// Configuration builder - public TThisConfiguration Dialect(string dialect) - { - values[DialectKey] = dialect; - values[AltDialectKey] = dialect; - return (TThisConfiguration) this; - } - - /// - /// Sets the database dialect. This shouldn't be necessary - /// if you've used one of the provided database configurations. - /// - /// Configuration builder - public TThisConfiguration Dialect() - where T : Dialect - { - return Dialect(typeof (T).AssemblyQualifiedName); - } - - /// - /// Sets the default database schema - /// - /// Default schema name - /// Configuration builder - public TThisConfiguration DefaultSchema(string schema) - { - values[DefaultSchemaKey] = schema; - return (TThisConfiguration) this; - } - - /// - /// Enables the outer-join option. - /// - /// Configuration builder - public TThisConfiguration UseOuterJoin() - { - ToggleBooleanSetting(UseOuterJoinKey); - return (TThisConfiguration) this; - } - - /// - /// Sets the max fetch depth. - /// - /// Max fetch depth - /// Configuration builder - public TThisConfiguration MaxFetchDepth(int maxFetchDepth) - { - values[MaxFetchDepthKey] = maxFetchDepth.ToString(); - return (TThisConfiguration)this; - } - - /// - /// Enables the reflection optimizer. - /// - /// Configuration builder - public TThisConfiguration UseReflectionOptimizer() - { - ToggleBooleanSetting(UseReflectionOptimizerKey); - return (TThisConfiguration) this; - } - - /// - /// Sets any query stubstitutions that NHibernate should - /// perform. - /// - /// Substitutions - /// Configuration builder - public TThisConfiguration QuerySubstitutions(string substitutions) - { - values[QuerySubstitutionsKey] = substitutions; - return (TThisConfiguration)this; - } - - /// - /// Enables the show SQL option. - /// - /// Configuration builder - public TThisConfiguration ShowSql() - { - ToggleBooleanSetting(ShowSqlKey); - return (TThisConfiguration)this; - } - - /// - /// Enables the format SQL option. - /// - /// Configuration builder - public TThisConfiguration FormatSql() - { - ToggleBooleanSetting(FormatSqlKey); - return (TThisConfiguration)this; - } - - /// - /// Sets the database provider. This shouldn't be necessary - /// if you're using one of the provided database configurations. - /// - /// Provider type - /// Configuration builder - public TThisConfiguration Provider(string provider) - { - values[ConnectionProviderKey] = provider; - return (TThisConfiguration)this; - } - - /// - /// Sets the database provider. This shouldn't be necessary - /// if you're using one of the provided database configurations. - /// - /// Provider type - /// Configuration builder - public TThisConfiguration Provider() - where T : IConnectionProvider - { - return Provider(typeof(T).AssemblyQualifiedName); - } - - /// - /// Specify the database driver. This isn't necessary - /// if you're using one of the provided database configurations. - /// - /// Driver type - /// Configuration builder - public TThisConfiguration Driver(string driverClass) - { - values[DriverClassKey] = driverClass; - return (TThisConfiguration)this; - } - - /// - /// Specify the database driver. This isn't necessary - /// if you're using one of the provided database configurations. - /// - /// Driver type - /// Configuration builder - public TThisConfiguration Driver() - where T : IDriver - { - return Driver(typeof(T).AssemblyQualifiedName); - } - - /// - /// Configure the connection string - /// - /// - /// ConnectionString(x => - /// { - /// x.Server("db_server"); - /// x.Database("Products"); - /// }); - /// - /// Closure for building the connection string - /// Configuration builder - public TThisConfiguration ConnectionString(Action connectionStringExpression) - { - connectionStringExpression(connectionString); - return (TThisConfiguration)this; - } - - /// - /// Set the connection string. - /// - /// Connection string to use - /// Configuration builder - public TThisConfiguration ConnectionString(string value) - { - connectionString.Is(value); - return (TThisConfiguration)this; - } - - /// - /// Configure caching. - /// - /// - /// Cache(x => - /// { - /// x.UseQueryCache(); - /// x.UseMinimalPuts(); - /// }); - /// - /// Closure for configuring caching - /// Configuration builder - public TThisConfiguration Cache(Action cacheExpression) - { - cacheExpression(cache); - return (TThisConfiguration)this; - } - - /// - /// Sets a raw property on the NHibernate configuration. Use this method - /// if there isn't a specific option available in the API. - /// - /// Setting key - /// Setting value - /// Configuration builder - public TThisConfiguration Raw(string key, string value) - { - values[key] = value; - return (TThisConfiguration) this; - } - - /// - /// Sets the collectiontype.factory_class property. - /// NOTE: NHibernate 2.1 only - /// - /// factory class - /// Configuration - public TThisConfiguration CollectionTypeFactory(string collectionTypeFactoryClass) - { - values[CollectionTypeFactoryClassKey] = collectionTypeFactoryClass; - return (TThisConfiguration)this; - } - - /// - /// Sets the collectiontype.factory_class property. - /// NOTE: NHibernate 2.1 only - /// - /// factory class - /// Configuration - public TThisConfiguration CollectionTypeFactory(Type collectionTypeFactoryClass) - { - values[CollectionTypeFactoryClassKey] = collectionTypeFactoryClass.AssemblyQualifiedName; - return (TThisConfiguration)this; - } - - /// - /// Sets the collectiontype.factory_class property. - /// NOTE: NHibernate 2.1 only - /// - /// factory class - /// Configuration - public TThisConfiguration CollectionTypeFactory() where TCollectionTypeFactory : ICollectionTypeFactory - { - return CollectionTypeFactory(typeof(TCollectionTypeFactory)); - } - - /// - /// Sets the proxyfactory.factory_class property. - /// NOTE: NHibernate 2.1 only - /// - /// factory class - /// Configuration - public TThisConfiguration ProxyFactoryFactory(string proxyFactoryFactoryClass) - { - values[ProxyFactoryFactoryClassKey] = proxyFactoryFactoryClass; - return (TThisConfiguration)this; - } - - /// - /// Sets the proxyfactory.factory_class property. - /// NOTE: NHibernate 2.1 only - /// - /// factory class - /// Configuration - public TThisConfiguration ProxyFactoryFactory(Type proxyFactoryFactory) - { - values[ProxyFactoryFactoryClassKey] = proxyFactoryFactory.AssemblyQualifiedName; - return (TThisConfiguration)this; - } - - /// - /// Sets the proxyfactory.factory_class property. - /// NOTE: NHibernate 2.1 only - /// - /// factory class - /// Configuration - public TThisConfiguration ProxyFactoryFactory() where TProxyFactoryFactory : IProxyFactoryFactory - { - return ProxyFactoryFactory(typeof(TProxyFactoryFactory)); - } - - /// - /// Sets the adonet.batch_size property. - /// - /// Batch size - /// Configuration - public TThisConfiguration AdoNetBatchSize(int size) - { - values[AdoNetBatchSizeKey] = size.ToString(); - return (TThisConfiguration)this; - } - - /// - /// Sets the current_session_context_class property. - /// - /// current session context class - /// Configuration - public TThisConfiguration CurrentSessionContext(string currentSessionContextClass) - { - values[CurrentSessionContextClassKey] = currentSessionContextClass; - return (TThisConfiguration)this; - } - - /// - /// Sets the current_session_context_class property. - /// - /// Implementation of ICurrentSessionContext to use - /// Configuration - public TThisConfiguration CurrentSessionContext() where TSessionContext : NHibernate.Context.ICurrentSessionContext - { - return CurrentSessionContext(typeof(TSessionContext).AssemblyQualifiedName); - } - - /// - /// Sets the connection isolation level. NHibernate setting: connection.isolation - /// - /// Isolation level - /// Configuration builder - public TThisConfiguration IsolationLevel(IsolationLevel connectionIsolation) - { - return IsolationLevel(connectionIsolation.ToString()); - } - - /// - /// Sets the connection isolation level. NHibernate setting: connection.isolation - /// - /// Isolation level - /// Configuration builder - public TThisConfiguration IsolationLevel(string connectionIsolation) - { - values[IsolationLevelKey] = connectionIsolation; - return (TThisConfiguration)this; - } - } +using System; +using System.Collections.Generic; +using System.Data; +using System.Linq; +using NHibernate.Bytecode; +using NHibernate.Connection; +using NHibernate.Dialect; +using NHibernate.Driver; +using NHibConfiguration = NHibernate.Cfg.Configuration; + +namespace FluentNHibernate.Cfg.Db +{ + public abstract class PersistenceConfiguration : PersistenceConfiguration + where TThisConfiguration : PersistenceConfiguration + {} + + public abstract class PersistenceConfiguration : IPersistenceConfigurer + where TThisConfiguration : PersistenceConfiguration + where TConnectionString : ConnectionStringBuilder, new() + { + protected const string DialectKey = "dialect"; // Newer one, but not supported by everything + protected const string AltDialectKey = "hibernate.dialect"; // Some older NHib tools require this + protected const string DefaultSchemaKey = "default_schema"; + protected const string UseOuterJoinKey = "use_outer_join"; + protected const string MaxFetchDepthKey = "max_fetch_depth"; + protected const string UseReflectionOptimizerKey = "use_reflection_optimizer"; + protected const string QuerySubstitutionsKey = "query.substitutions"; + protected const string ShowSqlKey = "show_sql"; + protected const string FormatSqlKey = "format_sql"; + + protected const string CollectionTypeFactoryClassKey = NHibernate.Cfg.Environment.CollectionTypeFactoryClass; + protected const string ConnectionProviderKey = "connection.provider"; + protected const string DefaultConnectionProviderClassName = "NHibernate.Connection.DriverConnectionProvider"; + protected const string DriverClassKey = "connection.driver_class"; + protected const string ConnectionStringKey = "connection.connection_string"; + const string IsolationLevelKey = "connection.isolation"; + protected const string ProxyFactoryFactoryClassKey = "proxyfactory.factory_class"; + protected const string DefaultProxyFactoryFactoryClassName = "NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle"; + protected const string AdoNetBatchSizeKey = "adonet.batch_size"; + protected const string CurrentSessionContextClassKey = "current_session_context_class"; + + private readonly Dictionary values = new Dictionary(); + + private bool nextBoolSettingValue = true; + private readonly TConnectionString connectionString; + private readonly CacheSettingsBuilder cache = new CacheSettingsBuilder(); + + protected PersistenceConfiguration() + { + values[ConnectionProviderKey] = DefaultConnectionProviderClassName; + values[ProxyFactoryFactoryClassKey] = DefaultProxyFactoryFactoryClassName; + connectionString = new TConnectionString(); + } + + protected virtual IDictionary CreateProperties() + { + if (connectionString.IsDirty) + Raw(ConnectionStringKey, connectionString.Create()); + + if (cache.IsDirty) + { + foreach (var pair in cache.Create()) + { + Raw(pair.Key, pair.Value); + } + } + + return values; + } + + static IEnumerable OverridenDefaults(IDictionary settings) + { + if (settings[ConnectionProviderKey] != DefaultConnectionProviderClassName) + yield return ConnectionProviderKey; + + if (settings[ProxyFactoryFactoryClassKey] != DefaultProxyFactoryFactoryClassName) + yield return ProxyFactoryFactoryClassKey; + } + + private static IEnumerable KeysToPreserve(NHibConfiguration nhibernateConfig, IDictionary settings) + { + var @default = new NHibConfiguration(); + return nhibernateConfig.Properties + .Except(@default.Properties) + .Select(k => k.Key) + .Except(OverridenDefaults(settings)); + } + + public NHibConfiguration ConfigureProperties(NHibConfiguration nhibernateConfig) + { + var settings = CreateProperties(); + var keepers = KeysToPreserve(nhibernateConfig, settings); + + foreach (var kv in settings.Where(s => !keepers.Contains(s.Key))) + { + nhibernateConfig.SetProperty(kv.Key, kv.Value); + } + + return nhibernateConfig; + } + + public IDictionary ToProperties() + { + return CreateProperties(); + } + + protected void ToggleBooleanSetting(string settingKey) + { + var value = nextBoolSettingValue.ToString().ToLowerInvariant(); + + values[settingKey] = value; + + nextBoolSettingValue = true; + } + + /// + /// Negates the next boolean option. + /// + public TThisConfiguration DoNot + { + get + { + nextBoolSettingValue = false; + return (TThisConfiguration)this; + } + } + + /// + /// Sets the database dialect. This shouldn't be necessary + /// if you've used one of the provided database configurations. + /// + /// Configuration builder + public TThisConfiguration Dialect(string dialect) + { + values[DialectKey] = dialect; + values[AltDialectKey] = dialect; + return (TThisConfiguration) this; + } + + /// + /// Sets the database dialect. This shouldn't be necessary + /// if you've used one of the provided database configurations. + /// + /// Configuration builder + public TThisConfiguration Dialect() + where T : Dialect + { + return Dialect(typeof (T).AssemblyQualifiedName); + } + + /// + /// Sets the default database schema + /// + /// Default schema name + /// Configuration builder + public TThisConfiguration DefaultSchema(string schema) + { + values[DefaultSchemaKey] = schema; + return (TThisConfiguration) this; + } + + /// + /// Enables the outer-join option. + /// + /// Configuration builder + public TThisConfiguration UseOuterJoin() + { + ToggleBooleanSetting(UseOuterJoinKey); + return (TThisConfiguration) this; + } + + /// + /// Sets the max fetch depth. + /// + /// Max fetch depth + /// Configuration builder + public TThisConfiguration MaxFetchDepth(int maxFetchDepth) + { + values[MaxFetchDepthKey] = maxFetchDepth.ToString(); + return (TThisConfiguration)this; + } + + /// + /// Enables the reflection optimizer. + /// + /// Configuration builder + public TThisConfiguration UseReflectionOptimizer() + { + ToggleBooleanSetting(UseReflectionOptimizerKey); + return (TThisConfiguration) this; + } + + /// + /// Sets any query stubstitutions that NHibernate should + /// perform. + /// + /// Substitutions + /// Configuration builder + public TThisConfiguration QuerySubstitutions(string substitutions) + { + values[QuerySubstitutionsKey] = substitutions; + return (TThisConfiguration)this; + } + + /// + /// Enables the show SQL option. + /// + /// Configuration builder + public TThisConfiguration ShowSql() + { + ToggleBooleanSetting(ShowSqlKey); + return (TThisConfiguration)this; + } + + /// + /// Enables the format SQL option. + /// + /// Configuration builder + public TThisConfiguration FormatSql() + { + ToggleBooleanSetting(FormatSqlKey); + return (TThisConfiguration)this; + } + + /// + /// Sets the database provider. This shouldn't be necessary + /// if you're using one of the provided database configurations. + /// + /// Provider type + /// Configuration builder + public TThisConfiguration Provider(string provider) + { + values[ConnectionProviderKey] = provider; + return (TThisConfiguration)this; + } + + /// + /// Sets the database provider. This shouldn't be necessary + /// if you're using one of the provided database configurations. + /// + /// Provider type + /// Configuration builder + public TThisConfiguration Provider() + where T : IConnectionProvider + { + return Provider(typeof(T).AssemblyQualifiedName); + } + + /// + /// Specify the database driver. This isn't necessary + /// if you're using one of the provided database configurations. + /// + /// Driver type + /// Configuration builder + public TThisConfiguration Driver(string driverClass) + { + values[DriverClassKey] = driverClass; + return (TThisConfiguration)this; + } + + /// + /// Specify the database driver. This isn't necessary + /// if you're using one of the provided database configurations. + /// + /// Driver type + /// Configuration builder + public TThisConfiguration Driver() + where T : IDriver + { + return Driver(typeof(T).AssemblyQualifiedName); + } + + /// + /// Configure the connection string + /// + /// + /// ConnectionString(x => + /// { + /// x.Server("db_server"); + /// x.Database("Products"); + /// }); + /// + /// Closure for building the connection string + /// Configuration builder + public TThisConfiguration ConnectionString(Action connectionStringExpression) + { + connectionStringExpression(connectionString); + return (TThisConfiguration)this; + } + + /// + /// Set the connection string. + /// + /// Connection string to use + /// Configuration builder + public TThisConfiguration ConnectionString(string value) + { + connectionString.Is(value); + return (TThisConfiguration)this; + } + + /// + /// Configure caching. + /// + /// + /// Cache(x => + /// { + /// x.UseQueryCache(); + /// x.UseMinimalPuts(); + /// }); + /// + /// Closure for configuring caching + /// Configuration builder + public TThisConfiguration Cache(Action cacheExpression) + { + cacheExpression(cache); + return (TThisConfiguration)this; + } + + /// + /// Sets a raw property on the NHibernate configuration. Use this method + /// if there isn't a specific option available in the API. + /// + /// Setting key + /// Setting value + /// Configuration builder + public TThisConfiguration Raw(string key, string value) + { + values[key] = value; + return (TThisConfiguration) this; + } + + /// + /// Sets the collectiontype.factory_class property. + /// NOTE: NHibernate 2.1 only + /// + /// factory class + /// Configuration + public TThisConfiguration CollectionTypeFactory(string collectionTypeFactoryClass) + { + values[CollectionTypeFactoryClassKey] = collectionTypeFactoryClass; + return (TThisConfiguration)this; + } + + /// + /// Sets the collectiontype.factory_class property. + /// NOTE: NHibernate 2.1 only + /// + /// factory class + /// Configuration + public TThisConfiguration CollectionTypeFactory(Type collectionTypeFactoryClass) + { + values[CollectionTypeFactoryClassKey] = collectionTypeFactoryClass.AssemblyQualifiedName; + return (TThisConfiguration)this; + } + + /// + /// Sets the collectiontype.factory_class property. + /// NOTE: NHibernate 2.1 only + /// + /// factory class + /// Configuration + public TThisConfiguration CollectionTypeFactory() where TCollectionTypeFactory : ICollectionTypeFactory + { + return CollectionTypeFactory(typeof(TCollectionTypeFactory)); + } + + /// + /// Sets the proxyfactory.factory_class property. + /// NOTE: NHibernate 2.1 only + /// + /// factory class + /// Configuration + public TThisConfiguration ProxyFactoryFactory(string proxyFactoryFactoryClass) + { + values[ProxyFactoryFactoryClassKey] = proxyFactoryFactoryClass; + return (TThisConfiguration)this; + } + + /// + /// Sets the proxyfactory.factory_class property. + /// NOTE: NHibernate 2.1 only + /// + /// factory class + /// Configuration + public TThisConfiguration ProxyFactoryFactory(Type proxyFactoryFactory) + { + values[ProxyFactoryFactoryClassKey] = proxyFactoryFactory.AssemblyQualifiedName; + return (TThisConfiguration)this; + } + + /// + /// Sets the proxyfactory.factory_class property. + /// NOTE: NHibernate 2.1 only + /// + /// factory class + /// Configuration + public TThisConfiguration ProxyFactoryFactory() where TProxyFactoryFactory : IProxyFactoryFactory + { + return ProxyFactoryFactory(typeof(TProxyFactoryFactory)); + } + + /// + /// Sets the adonet.batch_size property. + /// + /// Batch size + /// Configuration + public TThisConfiguration AdoNetBatchSize(int size) + { + values[AdoNetBatchSizeKey] = size.ToString(); + return (TThisConfiguration)this; + } + + /// + /// Sets the current_session_context_class property. + /// + /// current session context class + /// Configuration + public TThisConfiguration CurrentSessionContext(string currentSessionContextClass) + { + values[CurrentSessionContextClassKey] = currentSessionContextClass; + return (TThisConfiguration)this; + } + + /// + /// Sets the current_session_context_class property. + /// + /// Implementation of ICurrentSessionContext to use + /// Configuration + public TThisConfiguration CurrentSessionContext() where TSessionContext : NHibernate.Context.ICurrentSessionContext + { + return CurrentSessionContext(typeof(TSessionContext).AssemblyQualifiedName); + } + + /// + /// Sets the connection isolation level. NHibernate setting: connection.isolation + /// + /// Isolation level + /// Configuration builder + public TThisConfiguration IsolationLevel(IsolationLevel connectionIsolation) + { + return IsolationLevel(connectionIsolation.ToString()); + } + + /// + /// Sets the connection isolation level. NHibernate setting: connection.isolation + /// + /// Isolation level + /// Configuration builder + public TThisConfiguration IsolationLevel(string connectionIsolation) + { + values[IsolationLevelKey] = connectionIsolation; + return (TThisConfiguration)this; + } + } } \ No newline at end of file