Skip to content

Commit

Permalink
Remove dependency on ICacheConfigurationSectionHandler from Configura…
Browse files Browse the repository at this point in the history
…tionProvider.Current
  • Loading branch information
bahusoid committed Apr 13, 2023
1 parent 196296f commit bc183b4
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public void TestGetConfigFromProvidedConfiguration()
Assert.That(fooRegion.Properties["expiration"], Is.EqualTo("500"));
}

private ConfigurationProviderBase<CacheConfig, CoreDistributedCacheSectionHandler> _configurationProviderBackup;
private ConfigurationProviderBase<CacheConfig> _configurationProviderBackup;

[SetUp]
public void OnSetup()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public void TestGetConfigFromProvidedConfiguration()
Assert.That(config.Regions[0].Properties["expiration"], Is.EqualTo("500"));
}

private ConfigurationProviderBase<CacheConfig, CoreMemoryCacheSectionHandler> _configurationProviderBackup;
private ConfigurationProviderBase<CacheConfig> _configurationProviderBackup;

[SetUp]
public void OnSetup()
Expand Down
66 changes: 38 additions & 28 deletions NHibernate.Caches.Common/ConfigurationProviderBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,45 +8,64 @@ namespace NHibernate.Caches.Common
/// Base generic class for the cache configuration settings.
/// </summary>
/// <typeparam name="TConfig">The configuration class.</typeparam>
/// <typeparam name="TConfigHandler">The configuration class section handler.</typeparam>
public abstract class ConfigurationProviderBase<TConfig, TConfigHandler>
public abstract class ConfigurationProviderBase<TConfig>
where TConfig : class
where TConfigHandler : ICacheConfigurationSectionHandler, new()
{
private static ConfigurationProviderBase<TConfig, TConfigHandler> _current;
private static readonly string ConfigurationSectionName;

static ConfigurationProviderBase()
{
ConfigurationSectionName = new TConfigHandler().ConfigurationSectionName;
}
private static ConfigurationProviderBase<TConfig> _current;

/// <summary>
/// Provides ability to override default <see cref="System.Configuration.ConfigurationManager"/> with custom implementation.
/// Can be set to null if all configuration is specified by code.
/// </summary>
public static ConfigurationProviderBase<TConfig, TConfigHandler> Current
public static ConfigurationProviderBase<TConfig> Current
{
get => _current ?? (_current = new StaticConfigurationManagerProvider());
get => _current;
set => _current = value ?? new NullConfigurationProvider();
}

/// <summary>
/// Get the cache configuration.
/// </summary>
/// <returns>The cache configuration.</returns>
public abstract TConfig GetConfiguration();

private class NullConfigurationProvider : ConfigurationProviderBase<TConfig>
{
/// <inheritdoc />
public override TConfig GetConfiguration()
{
return null;
}
}
}

/// <summary>
/// Base generic class for the cache configuration settings.
/// </summary>
/// <typeparam name="TConfig">The configuration class.</typeparam>
/// <typeparam name="TConfigHandler">The configuration class section handler.</typeparam>
public abstract class ConfigurationProviderBase<TConfig, TConfigHandler> : ConfigurationProviderBase<TConfig>
where TConfig : class
where TConfigHandler : ICacheConfigurationSectionHandler, new()
{
private static readonly string ConfigurationSectionName;

static ConfigurationProviderBase()
{
ConfigurationSectionName = new TConfigHandler().ConfigurationSectionName;
Current = new StaticConfigurationManagerProvider();
}

/// <summary>
/// Directly supply the configuration to be used to the configuration provider.
/// </summary>
/// <param name="configuration">The configuration, or <see langword="null" /> for resetting the provider to its
/// default behavior.</param>
public static void SetConfiguration(Configuration configuration)
{
_current = configuration == null ? null : new SystemConfigurationProvider(configuration);
Current = configuration == null ? null : new SystemConfigurationProvider(configuration);
}

/// <summary>
/// Get the cache configuration.
/// </summary>
/// <returns>The cache configuration.</returns>
public abstract TConfig GetConfiguration();

private class StaticConfigurationManagerProvider : ConfigurationProviderBase<TConfig, TConfigHandler>
{
/// <inheritdoc />
Expand Down Expand Up @@ -82,14 +101,5 @@ public override TConfig GetConfiguration()
return (TConfig) section.Create(null, null, xmlDoc.DocumentElement);
}
}

private class NullConfigurationProvider : ConfigurationProviderBase<TConfig, TConfigHandler>
{
/// <inheritdoc />
public override TConfig GetConfiguration()
{
return null;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public void TestGetConfigFromProvidedConfiguration()
Assert.That(config[0].Properties["expiration"], Is.EqualTo("500"));
}

private ConfigurationProviderBase<CacheConfig[], RtMemoryCacheSectionHandler> _configurationProviderBackup;
private ConfigurationProviderBase<CacheConfig[]> _configurationProviderBackup;

[SetUp]
public void OnSetup()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public void TestGetConfigFromProvidedConfiguration()
Assert.That(config.Regions[0].Expiration, Is.EqualTo(TimeSpan.FromSeconds(500)));
}

private ConfigurationProviderBase<CacheConfig, RedisSectionHandler> _configurationProviderBackup;
private ConfigurationProviderBase<CacheConfig> _configurationProviderBackup;

[SetUp]
public void OnSetup()
Expand Down

0 comments on commit bc183b4

Please sign in to comment.