-
Notifications
You must be signed in to change notification settings - Fork 933
Ability to replace ConfigurationManager with a custom config provider #2116
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
28b0a96
Ability to replace ConfigurationManager with custom configuration pro…
bahusoid 9f2b4a5
Restore logger configuration
bahusoid d2c690f
Add "null" ConfigurationManager
bahusoid 60bab54
Add SystemConfigurationManager that supports user provided Configuration
bahusoid 0c1909b
Use abstract class for extensibility and replace GetAppConfig with sp…
bahusoid ed3c4c4
formatting
bahusoid 86d1635
Address comments
bahusoid 2fdfbb9
Rename ConfigurationProvider related objects
bahusoid 52b6ed8
Merge branch 'master' into configManager
hazzik 1878f49
Revert unnecessary changes
hazzik 766c007
Clean up
bahusoid 3873b25
Move static ConfigurationProvider from Settings to itself.
hazzik ca58d66
Merge branch 'master' into configManager
hazzik File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
namespace NHibernate.Cfg | ||
{ | ||
/// <summary> | ||
/// Configuration App Settings | ||
/// </summary> | ||
public static class AppSettings | ||
{ | ||
/// <summary> | ||
/// Type that implements <see cref="INHibernateLoggerFactory"/> | ||
/// </summary> | ||
public const string LoggerFactoryClassName = "nhibernate-logger"; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
namespace NHibernate.Cfg | ||
{ | ||
/// <summary> | ||
/// Base class for NHibernate configuration settings | ||
/// </summary> | ||
public abstract class ConfigurationProvider | ||
{ | ||
private static ConfigurationProvider _current = new StaticConfigurationManagerProvider(); | ||
|
||
/// <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 ConfigurationProvider Current | ||
{ | ||
get => _current; | ||
set => _current = value ?? new NullConfigurationProvider(); | ||
} | ||
|
||
public abstract IHibernateConfiguration GetConfiguration(); | ||
|
||
public abstract string GetNamedConnectionString(string name); | ||
|
||
/// <summary> | ||
/// Type that implements <see cref="INHibernateLoggerFactory"/> | ||
/// </summary> | ||
public abstract string GetLoggerFactoryClassName(); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
namespace NHibernate.Cfg | ||
{ | ||
class NullConfigurationProvider : ConfigurationProvider | ||
{ | ||
public override IHibernateConfiguration GetConfiguration() | ||
{ | ||
return null; | ||
} | ||
|
||
public override string GetNamedConnectionString(string name) | ||
{ | ||
return null; | ||
} | ||
|
||
public override string GetLoggerFactoryClassName() | ||
{ | ||
return null; | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
using System; | ||
using System.Configuration; | ||
using System.Linq; | ||
using NHibernate.Cfg.ConfigurationSchema; | ||
|
||
namespace NHibernate.Cfg | ||
{ | ||
class StaticConfigurationManagerProvider : ConfigurationProvider | ||
{ | ||
public override IHibernateConfiguration GetConfiguration() | ||
{ | ||
//TODO 6.0: Throw if not null and not IHibernateConfiguration | ||
return ConfigurationManager.GetSection(CfgXmlHelper.CfgSectionName) as IHibernateConfiguration; | ||
} | ||
|
||
public override string GetNamedConnectionString(string name) | ||
{ | ||
return ConfigurationManager.ConnectionStrings[name]?.ConnectionString; | ||
} | ||
|
||
public override string GetLoggerFactoryClassName() | ||
{ | ||
var name = AppSettings.LoggerFactoryClassName; | ||
var value = ConfigurationManager.AppSettings[name]; | ||
|
||
//TODO 6.0: Return value right away. Don't do ignore case search and document it as possible breaking change. | ||
if (value != null) | ||
return value; | ||
|
||
return GetAppSettingIgnoreCase(name); | ||
} | ||
|
||
//TODO 6.0: Remove it | ||
private static string GetAppSettingIgnoreCase(string name) | ||
{ | ||
var key = ConfigurationManager.AppSettings.Keys.Cast<string>().FirstOrDefault(k => name.Equals(k, StringComparison.OrdinalIgnoreCase)); | ||
return string.IsNullOrEmpty(key) | ||
? null | ||
: ConfigurationManager.AppSettings[key]; | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
using System.Configuration; | ||
using NHibernate.Cfg.ConfigurationSchema; | ||
|
||
namespace NHibernate.Cfg | ||
{ | ||
/// <summary> | ||
/// Configuration manager that supports user provided configuration | ||
/// </summary> | ||
public class SystemConfigurationProvider : ConfigurationProvider | ||
{ | ||
private readonly System.Configuration.Configuration _configuration; | ||
|
||
public SystemConfigurationProvider(System.Configuration.Configuration configuration) | ||
{ | ||
_configuration = configuration; | ||
} | ||
|
||
public override IHibernateConfiguration GetConfiguration() | ||
{ | ||
ConfigurationSection configurationSection = _configuration.GetSection(CfgXmlHelper.CfgSectionName); | ||
var xml = configurationSection?.SectionInformation.GetRawXml(); | ||
return xml == null ? null : HibernateConfiguration.FromAppConfig(xml); | ||
} | ||
|
||
public override string GetNamedConnectionString(string name) | ||
{ | ||
return _configuration.ConnectionStrings.ConnectionStrings[name]?.ConnectionString; | ||
} | ||
|
||
public override string GetLoggerFactoryClassName() | ||
{ | ||
return GetAppSetting(AppSettings.LoggerFactoryClassName); | ||
} | ||
|
||
private string GetAppSetting(string name) | ||
{ | ||
return _configuration.AppSettings.Settings[name]?.Value; | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.