|
| 1 | +using System; |
| 2 | +using System.Configuration; |
| 3 | +using System.Linq; |
| 4 | +using NHibernate.Cfg.ConfigurationSchema; |
| 5 | + |
| 6 | +namespace NHibernate.Cfg |
| 7 | +{ |
| 8 | + /// <summary> |
| 9 | + /// Base class for configuration settings that NHibernate might require. |
| 10 | + /// </summary> |
| 11 | + public abstract class ConfigurationManagerBase |
| 12 | + { |
| 13 | + public abstract IHibernateConfiguration GetConfiguration(); |
| 14 | + public abstract string GetNamedConnectionString(string name); |
| 15 | + |
| 16 | + /// <summary> |
| 17 | + /// Type that implements <see cref="INHibernateLoggerFactory"/> |
| 18 | + /// </summary> |
| 19 | + public abstract string GetLoggerFactoryClassName(); |
| 20 | + } |
| 21 | + |
| 22 | + /// <summary> |
| 23 | + /// Configuration manager that supports user provided configuration |
| 24 | + /// </summary> |
| 25 | + public class SystemConfigurationManager : ConfigurationManagerBase |
| 26 | + { |
| 27 | + private readonly System.Configuration.Configuration _configuration; |
| 28 | + |
| 29 | + public SystemConfigurationManager(System.Configuration.Configuration configuration) |
| 30 | + { |
| 31 | + _configuration = configuration; |
| 32 | + } |
| 33 | + |
| 34 | + public override IHibernateConfiguration GetConfiguration() |
| 35 | + { |
| 36 | + ConfigurationSection configurationSection = _configuration.GetSection(CfgXmlHelper.CfgSectionName); |
| 37 | + var xml = configurationSection?.SectionInformation.GetRawXml(); |
| 38 | + return xml == null ? null : HibernateConfiguration.FromAppConfig(xml); |
| 39 | + } |
| 40 | + |
| 41 | + public override string GetNamedConnectionString(string name) |
| 42 | + { |
| 43 | + return _configuration.ConnectionStrings.ConnectionStrings[name]?.ConnectionString; |
| 44 | + } |
| 45 | + |
| 46 | + public override string GetLoggerFactoryClassName() |
| 47 | + { |
| 48 | + return GetAppSetting(AppSettings.LoggerFactoryClassName); |
| 49 | + } |
| 50 | + |
| 51 | + private string GetAppSetting(string name) |
| 52 | + { |
| 53 | + return _configuration.AppSettings.Settings[name]?.Value; |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + class StaticSystemConfigurationManager : ConfigurationManagerBase |
| 58 | + { |
| 59 | + public override IHibernateConfiguration GetConfiguration() |
| 60 | + { |
| 61 | + //TODO 6.0: Throw if not null and not IHibernateConfiguration |
| 62 | + return ConfigurationManager.GetSection(CfgXmlHelper.CfgSectionName) as IHibernateConfiguration; |
| 63 | + } |
| 64 | + |
| 65 | + public override string GetNamedConnectionString(string name) |
| 66 | + { |
| 67 | + return ConfigurationManager.ConnectionStrings[name]?.ConnectionString; |
| 68 | + } |
| 69 | + |
| 70 | + public override string GetLoggerFactoryClassName() |
| 71 | + { |
| 72 | + var name = AppSettings.LoggerFactoryClassName; |
| 73 | + var value = ConfigurationManager.AppSettings[name]; |
| 74 | + |
| 75 | + //TODO 6.0: Return value right away. Don't do ignore case search and document it as possible breaking change. |
| 76 | + if (value != null) |
| 77 | + return value; |
| 78 | + |
| 79 | + return GetAppSettingIgnoreCase(name); |
| 80 | + } |
| 81 | + |
| 82 | + //TODO 6.0: Remove it |
| 83 | + private static string GetAppSettingIgnoreCase(string name) |
| 84 | + { |
| 85 | + var key = ConfigurationManager.AppSettings.Keys.Cast<string>().FirstOrDefault(k => name.Equals(k, StringComparison.OrdinalIgnoreCase)); |
| 86 | + return string.IsNullOrEmpty(key) |
| 87 | + ? null |
| 88 | + : ConfigurationManager.AppSettings[key]; |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + class NullConfigurationManager : ConfigurationManagerBase |
| 93 | + { |
| 94 | + public override IHibernateConfiguration GetConfiguration() |
| 95 | + { |
| 96 | + return null; |
| 97 | + } |
| 98 | + |
| 99 | + public override string GetNamedConnectionString(string name) |
| 100 | + { |
| 101 | + return null; |
| 102 | + } |
| 103 | + |
| 104 | + public override string GetLoggerFactoryClassName() |
| 105 | + { |
| 106 | + return null; |
| 107 | + } |
| 108 | + } |
| 109 | +} |
0 commit comments