Skip to content

Commit

Permalink
Add SystemConfigurationManager that supports user provided Configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
bahusoid committed Apr 11, 2019
1 parent 2b2b4ba commit 6402e58
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 34 deletions.
33 changes: 2 additions & 31 deletions src/NHibernate.Test/TestsContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using System.IO;
using log4net.Repository.Hierarchy;
using NHibernate.Cfg;
using NHibernate.Cfg.ConfigurationSchema;
#endif

namespace NHibernate.Test
Expand All @@ -26,7 +25,8 @@ public void RunBeforeAnyTests()
//so we need to explicitly load the configuration
if (ExecutingWithVsTest)
{
Settings.ConfigurationManager = new NetCoreConfigurationManager();
var assemblyPath = Path.Combine(TestContext.CurrentContext.TestDirectory, Path.GetFileName(typeof(TestsContext).Assembly.Location));
Settings.ConfigurationManager = new SystemConfigurationManager(ConfigurationManager.OpenExeConfiguration(assemblyPath));
}

ConfigureLog4Net();
Expand All @@ -49,35 +49,6 @@ private static void ConfigureLog4Net()
hierarchy.Root.AddAppender(consoleAppender);
hierarchy.Configured = true;
}

class NetCoreConfigurationManager : IConfigurationManager
{
private readonly System.Configuration.Configuration _configuration;

public NetCoreConfigurationManager()
{
var assemblyPath =
Path.Combine(TestContext.CurrentContext.TestDirectory, Path.GetFileName(typeof(TestsContext).Assembly.Location));
_configuration = ConfigurationManager.OpenExeConfiguration(assemblyPath);
}

public IHibernateConfiguration GetConfiguration()
{
ConfigurationSection configurationSection = _configuration.GetSection(CfgXmlHelper.CfgSectionName);
var xml = configurationSection?.SectionInformation.GetRawXml();
return xml == null ? null : HibernateConfiguration.FromAppConfig(xml);
}

public string GetNamedConnectionString(string name)
{
return _configuration.ConnectionStrings.ConnectionStrings[name]?.ConnectionString;
}

public string GetAppSetting(string name)
{
return _configuration.AppSettings.Settings[name]?.Value;
}
}
#endif
}
}
32 changes: 30 additions & 2 deletions src/NHibernate/Cfg/IConfigurationManager.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Configuration;
using System.Linq;
using NHibernate.Cfg.ConfigurationSchema;

namespace NHibernate.Cfg
{
Expand All @@ -9,7 +10,7 @@ internal static class ConfigurationManagerExtensions
//TODO 6.0: Replace with GetAppSetting and document as possible breaking change all usages.
internal static string GetAppSettingIgnoringCase(this IConfigurationManager config, string name)
{
if (!(config is SystemConfigurationManager))
if (!(config is StaticSystemConfigurationManager))
return config.GetAppSetting(name);

var key = ConfigurationManager.AppSettings.Keys.Cast<string>().FirstOrDefault(k => name.Equals(k, StringComparison.OrdinalIgnoreCase));
Expand All @@ -26,7 +27,34 @@ public interface IConfigurationManager
string GetAppSetting(string name);
}

class SystemConfigurationManager : IConfigurationManager
public class SystemConfigurationManager : IConfigurationManager
{
private readonly System.Configuration.Configuration _configuration;

public SystemConfigurationManager(System.Configuration.Configuration configuration)
{
_configuration = configuration;
}

public IHibernateConfiguration GetConfiguration()
{
ConfigurationSection configurationSection = _configuration.GetSection(CfgXmlHelper.CfgSectionName);
var xml = configurationSection?.SectionInformation.GetRawXml();
return xml == null ? null : HibernateConfiguration.FromAppConfig(xml);
}

public string GetNamedConnectionString(string name)
{
return _configuration.ConnectionStrings.ConnectionStrings[name]?.ConnectionString;
}

public string GetAppSetting(string name)
{
return _configuration.AppSettings.Settings[name]?.Value;
}
}

class StaticSystemConfigurationManager : IConfigurationManager
{
public IHibernateConfiguration GetConfiguration()
{
Expand Down
2 changes: 1 addition & 1 deletion src/NHibernate/Cfg/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ namespace NHibernate.Cfg
/// </summary>
public sealed class Settings
{
private static IConfigurationManager _configurationManager = new SystemConfigurationManager();
private static IConfigurationManager _configurationManager = new StaticSystemConfigurationManager();

/// <summary>
/// Provides ability to override default <see cref="System.Configuration.ConfigurationManager"/> with custom implementation.
Expand Down

0 comments on commit 6402e58

Please sign in to comment.