From bc183b42b827f8b0026f1262c954ba240312dbd4 Mon Sep 17 00:00:00 2001 From: Roman Artiukhin Date: Thu, 13 Apr 2023 16:56:53 +0300 Subject: [PATCH] Remove dependency on ICacheConfigurationSectionHandler from ConfigurationProvider.Current --- ...reDistributedCacheSectionHandlerFixture.cs | 2 +- .../CoreMemoryCacheSectionHandlerFixture.cs | 2 +- .../ConfigurationProviderBase.cs | 66 +++++++++++-------- .../RtMemoryCacheSectionHandlerFixture.cs | 2 +- .../RedisSectionHandlerFixture.cs | 2 +- 5 files changed, 42 insertions(+), 32 deletions(-) diff --git a/CoreDistributedCache/NHibernate.Caches.CoreDistributedCache.Tests/CoreDistributedCacheSectionHandlerFixture.cs b/CoreDistributedCache/NHibernate.Caches.CoreDistributedCache.Tests/CoreDistributedCacheSectionHandlerFixture.cs index 3506446..5987625 100644 --- a/CoreDistributedCache/NHibernate.Caches.CoreDistributedCache.Tests/CoreDistributedCacheSectionHandlerFixture.cs +++ b/CoreDistributedCache/NHibernate.Caches.CoreDistributedCache.Tests/CoreDistributedCacheSectionHandlerFixture.cs @@ -123,7 +123,7 @@ public void TestGetConfigFromProvidedConfiguration() Assert.That(fooRegion.Properties["expiration"], Is.EqualTo("500")); } - private ConfigurationProviderBase _configurationProviderBackup; + private ConfigurationProviderBase _configurationProviderBackup; [SetUp] public void OnSetup() diff --git a/CoreMemoryCache/NHibernate.Caches.CoreMemoryCache.Tests/CoreMemoryCacheSectionHandlerFixture.cs b/CoreMemoryCache/NHibernate.Caches.CoreMemoryCache.Tests/CoreMemoryCacheSectionHandlerFixture.cs index 0e1995a..6379599 100644 --- a/CoreMemoryCache/NHibernate.Caches.CoreMemoryCache.Tests/CoreMemoryCacheSectionHandlerFixture.cs +++ b/CoreMemoryCache/NHibernate.Caches.CoreMemoryCache.Tests/CoreMemoryCacheSectionHandlerFixture.cs @@ -95,7 +95,7 @@ public void TestGetConfigFromProvidedConfiguration() Assert.That(config.Regions[0].Properties["expiration"], Is.EqualTo("500")); } - private ConfigurationProviderBase _configurationProviderBackup; + private ConfigurationProviderBase _configurationProviderBackup; [SetUp] public void OnSetup() diff --git a/NHibernate.Caches.Common/ConfigurationProviderBase.cs b/NHibernate.Caches.Common/ConfigurationProviderBase.cs index 9946157..3345580 100644 --- a/NHibernate.Caches.Common/ConfigurationProviderBase.cs +++ b/NHibernate.Caches.Common/ConfigurationProviderBase.cs @@ -8,29 +8,54 @@ namespace NHibernate.Caches.Common /// Base generic class for the cache configuration settings. /// /// The configuration class. - /// The configuration class section handler. - public abstract class ConfigurationProviderBase + public abstract class ConfigurationProviderBase where TConfig : class - where TConfigHandler : ICacheConfigurationSectionHandler, new() { - private static ConfigurationProviderBase _current; - private static readonly string ConfigurationSectionName; - - static ConfigurationProviderBase() - { - ConfigurationSectionName = new TConfigHandler().ConfigurationSectionName; - } + private static ConfigurationProviderBase _current; /// /// Provides ability to override default with custom implementation. /// Can be set to null if all configuration is specified by code. /// - public static ConfigurationProviderBase Current + public static ConfigurationProviderBase Current { - get => _current ?? (_current = new StaticConfigurationManagerProvider()); + get => _current; set => _current = value ?? new NullConfigurationProvider(); } + /// + /// Get the cache configuration. + /// + /// The cache configuration. + public abstract TConfig GetConfiguration(); + + private class NullConfigurationProvider : ConfigurationProviderBase + { + /// + public override TConfig GetConfiguration() + { + return null; + } + } + } + + /// + /// Base generic class for the cache configuration settings. + /// + /// The configuration class. + /// The configuration class section handler. + public abstract class ConfigurationProviderBase : ConfigurationProviderBase + where TConfig : class + where TConfigHandler : ICacheConfigurationSectionHandler, new() + { + private static readonly string ConfigurationSectionName; + + static ConfigurationProviderBase() + { + ConfigurationSectionName = new TConfigHandler().ConfigurationSectionName; + Current = new StaticConfigurationManagerProvider(); + } + /// /// Directly supply the configuration to be used to the configuration provider. /// @@ -38,15 +63,9 @@ public static ConfigurationProviderBase Current /// default behavior. public static void SetConfiguration(Configuration configuration) { - _current = configuration == null ? null : new SystemConfigurationProvider(configuration); + Current = configuration == null ? null : new SystemConfigurationProvider(configuration); } - /// - /// Get the cache configuration. - /// - /// The cache configuration. - public abstract TConfig GetConfiguration(); - private class StaticConfigurationManagerProvider : ConfigurationProviderBase { /// @@ -82,14 +101,5 @@ public override TConfig GetConfiguration() return (TConfig) section.Create(null, null, xmlDoc.DocumentElement); } } - - private class NullConfigurationProvider : ConfigurationProviderBase - { - /// - public override TConfig GetConfiguration() - { - return null; - } - } } } diff --git a/RtMemoryCache/NHibernate.Caches.RtMemoryCache.Tests/RtMemoryCacheSectionHandlerFixture.cs b/RtMemoryCache/NHibernate.Caches.RtMemoryCache.Tests/RtMemoryCacheSectionHandlerFixture.cs index 6e37036..c39a2fb 100644 --- a/RtMemoryCache/NHibernate.Caches.RtMemoryCache.Tests/RtMemoryCacheSectionHandlerFixture.cs +++ b/RtMemoryCache/NHibernate.Caches.RtMemoryCache.Tests/RtMemoryCacheSectionHandlerFixture.cs @@ -85,7 +85,7 @@ public void TestGetConfigFromProvidedConfiguration() Assert.That(config[0].Properties["expiration"], Is.EqualTo("500")); } - private ConfigurationProviderBase _configurationProviderBackup; + private ConfigurationProviderBase _configurationProviderBackup; [SetUp] public void OnSetup() diff --git a/StackExchangeRedis/NHibernate.Caches.StackExchangeRedis.Tests/RedisSectionHandlerFixture.cs b/StackExchangeRedis/NHibernate.Caches.StackExchangeRedis.Tests/RedisSectionHandlerFixture.cs index 99a29e6..b0b9dd4 100644 --- a/StackExchangeRedis/NHibernate.Caches.StackExchangeRedis.Tests/RedisSectionHandlerFixture.cs +++ b/StackExchangeRedis/NHibernate.Caches.StackExchangeRedis.Tests/RedisSectionHandlerFixture.cs @@ -92,7 +92,7 @@ public void TestGetConfigFromProvidedConfiguration() Assert.That(config.Regions[0].Expiration, Is.EqualTo(TimeSpan.FromSeconds(500))); } - private ConfigurationProviderBase _configurationProviderBackup; + private ConfigurationProviderBase _configurationProviderBackup; [SetUp] public void OnSetup()