| | 1 | | using System.Xml.Linq; |
| | 2 | | using System.Text.Json; |
| | 3 | |
|
| | 4 | | namespace Configuration.Helper; |
| | 5 | |
|
| | 6 | | /// <summary>Base class for Settings Store implementations.</summary> |
| | 7 | | public abstract class SettingsStoreBase : ISettingsStore |
| | 8 | | { |
| | 9 | | #region ISettingsStore Implementation |
| | 10 | |
|
| | 11 | | #region Properties |
| | 12 | |
|
| | 13 | | /// <inheritdoc /> |
| 61 | 14 | | public string Location { get; protected set; } |
| | 15 | |
|
| | 16 | | /// <inheritdoc /> |
| 128 | 17 | | public IDictionary<string, SettingsSection> Sections { get; } |
| | 18 | |
|
| | 19 | | /// <inheritdoc /> |
| 40 | 20 | | public bool IsInitialized { get; protected set; } |
| | 21 | |
|
| | 22 | | /// <inheritdoc /> |
| 6 | 23 | | public ISettingsSection AppSettings => GetSection( cAppSettings ); |
| | 24 | |
|
| | 25 | | /// <inheritdoc /> |
| 1 | 26 | | public ISettingsSection ConnectionStrings => GetSection( cConnections ); |
| | 27 | |
|
| | 28 | | /// <inheritdoc /> |
| 24 | 29 | | public IOHelper.PathType Source { get; protected set; } |
| | 30 | |
|
| | 31 | | #endregion |
| | 32 | |
|
| | 33 | | #region Methods |
| | 34 | |
|
| | 35 | | /// <inheritdoc /> |
| | 36 | | public bool AddSetting( string settingKey, string settingValue ) |
| 3 | 37 | | { |
| | 38 | | // Add the setting value to the AppSettings section |
| 3 | 39 | | string section = cAppSettings; |
| 3 | 40 | | AddSetting( ref section, settingKey, settingValue ); |
| 3 | 41 | | return true; |
| 3 | 42 | | } |
| | 43 | |
|
| | 44 | | /// <inheritdoc /> |
| | 45 | | public ISettingsSection GetSection( string? sectionName ) |
| 13 | 46 | | { |
| | 47 | | // Check the required parameter is supplied |
| | 48 | | const string cMethod = @"SettingsStoreBase.GetSection"; |
| 15 | 49 | | if( null == sectionName ) { throw new ArgumentNullException( nameof( sectionName ), cMethod ); } |
| 12 | 50 | | sectionName = sectionName.Trim(); |
| 14 | 51 | | if( sectionName.Length == 0 ) { throw new ArgumentException( cMethod, nameof( sectionName ) ); } |
| | 52 | |
|
| | 53 | | // Return the section if it exists |
| 27 | 54 | | if( Sections.TryGetValue( sectionName, out SettingsSection? value ) ) { return value; } |
| | 55 | |
|
| | 56 | | // Return a new section if not found |
| 3 | 57 | | SettingsSection retValue = new( _comparer ); |
| 3 | 58 | | Sections.Add( sectionName, retValue ); |
| 3 | 59 | | return retValue; |
| 11 | 60 | | } |
| | 61 | |
|
| | 62 | | /// <inheritdoc /> |
| | 63 | | public string GetSetting( string settingKey ) |
| 1 | 64 | | { |
| 1 | 65 | | return AppSettings.GetSetting( settingKey ); |
| 1 | 66 | | } |
| | 67 | |
|
| | 68 | | #endregion |
| | 69 | |
|
| | 70 | | #endregion |
| | 71 | |
|
| | 72 | | /// <summary>Configuration settings file extension.</summary> |
| 40 | 73 | | protected string fileExtension = string.Empty; |
| | 74 | |
|
| | 75 | | #region Constants and Protected Constructor |
| | 76 | |
|
| | 77 | | private const string cAppSettings = @"appSettings"; // Application settings section name |
| | 78 | | private const string cAppSettingKey = @"key"; // Application setting key name |
| | 79 | | private const string cConnections = @"connectionStrings"; // Connection strings section name |
| | 80 | | private const string cConnectionKey = @"name"; // Connection string key name |
| 40 | 81 | | private readonly StringComparer _comparer = StringComparer.CurrentCultureIgnoreCase; |
| | 82 | |
|
| | 83 | | /// <summary>Default constructor.</summary> |
| 40 | 84 | | protected SettingsStoreBase() |
| 40 | 85 | | { |
| 40 | 86 | | Location = string.Empty; |
| 40 | 87 | | Sections = new Dictionary<string, SettingsSection>( _comparer ) |
| 40 | 88 | | { |
| 40 | 89 | | // Add the standard sections |
| 40 | 90 | | {cAppSettings, new SettingsSection( _comparer )}, |
| 40 | 91 | | {cConnections, new SettingsSection( _comparer )} |
| 40 | 92 | | }; |
| 40 | 93 | | } |
| | 94 | |
|
| | 95 | | #endregion |
| | 96 | |
|
| | 97 | | #region Private Methods |
| | 98 | |
|
| | 99 | | private void ProcessSetting( JsonElement element, string sectionName ) |
| 6 | 100 | | { |
| 6 | 101 | | if( element.ValueKind != JsonValueKind.Object ) return; |
| | 102 | |
|
| 38 | 103 | | foreach( JsonProperty item in element.EnumerateObject() ) |
| 10 | 104 | | { |
| 10 | 105 | | string? val = item.Value.GetString(); |
| 10 | 106 | | AddSetting( ref sectionName, item.Name, val ?? string.Empty ); |
| 10 | 107 | | } |
| 6 | 108 | | } |
| | 109 | |
|
| | 110 | | private void ProcessSetting( XElement elem ) |
| 83 | 111 | | { |
| | 112 | | // The element must have a parent section |
| 83 | 113 | | if( null == elem || null == elem.Parent ) { return; } |
| 83 | 114 | | string sectionName = elem.Parent.Name.LocalName; |
| | 115 | |
|
| | 116 | | // Check for application setting |
| 83 | 117 | | XAttribute? settingKey = elem.Attribute( cAppSettingKey ) ?? elem.Attribute( cConnectionKey ); |
| | 118 | |
|
| | 119 | | // Get the setting value |
| 83 | 120 | | XAttribute? settingVal = ( settingKey?.Name.LocalName ) switch |
| 83 | 121 | | { |
| 19 | 122 | | cConnectionKey => elem.Attribute( @"connectionString" ), |
| 64 | 123 | | _ => elem.Attribute( @"value" ), |
| 83 | 124 | | }; |
| | 125 | |
|
| 83 | 126 | | if( null != settingVal && null != settingKey ) |
| 83 | 127 | | { |
| | 128 | | // Add the value to the section |
| 83 | 129 | | AddSetting( ref sectionName, settingKey.Value, settingVal.Value ); |
| 83 | 130 | | } |
| 83 | 131 | | } |
| | 132 | |
|
| | 133 | | private void AddSetting( ref string sectionName, string settingKey, string settingVal ) |
| 96 | 134 | | { |
| | 135 | | // Try getting existing section |
| 96 | 136 | | SettingsSection? section = Sections.TryGetValue( sectionName, out SettingsSection? value ) ? value : null; |
| | 137 | |
|
| 96 | 138 | | if( null == section ) |
| 18 | 139 | | { |
| | 140 | | // Create a new section |
| 18 | 141 | | section = new SettingsSection( _comparer ); |
| 18 | 142 | | Sections.Add( sectionName, section ); |
| 18 | 143 | | } |
| | 144 | |
|
| | 145 | | // Add the setting |
| 96 | 146 | | section.AddSetting( settingKey, settingVal ); |
| 96 | 147 | | } |
| | 148 | |
|
| | 149 | | #endregion |
| | 150 | |
|
| | 151 | | #region Protected Methods |
| | 152 | |
|
| | 153 | | /// <summary>Initializes the Setting Store object.</summary> |
| | 154 | | /// <param name="config">String containing the configuration file contents.</param> |
| | 155 | | /// <exception cref="ArgumentException">Thrown if the parameter is null or empty.</exception> |
| | 156 | | /// <exception cref="JsonException">The JSON text to parse does not represent a valid single JSON value.</exception> |
| | 157 | | protected void LoadFromStream( ref string config ) |
| 24 | 158 | | { |
| | 159 | | // Check the required parameter is supplied |
| | 160 | | const string cMethod = @"SettingsStoreBase.LoadFromStream"; |
| | 161 | |
|
| | 162 | | // Check the required parameter has a value |
| 26 | 163 | | if( string.IsNullOrWhiteSpace( config ) ) { throw new ArgumentException( cMethod, nameof( config ) ); } |
| 23 | 164 | | config = config.Trim(); |
| | 165 | |
|
| 23 | 166 | | if( ConfigFileHelper.cJsonExtension.Equals( fileExtension, StringComparison.CurrentCultureIgnoreCase ) ) |
| 4 | 167 | | { |
| | 168 | | // Convert the string to JSON |
| 4 | 169 | | using JsonDocument doc = JsonDocument.Parse( config, new JsonDocumentOptions |
| 4 | 170 | | { AllowTrailingCommas = true, MaxDepth = 2 } ); |
| 2 | 171 | | { |
| 2 | 172 | | JsonElement root = doc.RootElement; |
| 2 | 173 | | if( root.ValueKind == JsonValueKind.Object ) |
| 2 | 174 | | { |
| | 175 | | // find name of section |
| 18 | 176 | | foreach( JsonProperty element in root.EnumerateObject() ) |
| 6 | 177 | | { |
| 6 | 178 | | string section = element.Name; |
| 6 | 179 | | ProcessSetting( element.Value, section ); |
| 6 | 180 | | } |
| 2 | 181 | | } |
| 2 | 182 | | } |
| 2 | 183 | | } |
| | 184 | | else |
| 19 | 185 | | { |
| | 186 | | // Convert the string to XML |
| 19 | 187 | | XElement xml = XElement.Parse( config ); |
| | 188 | |
|
| | 189 | | // Process each configuration settings |
| 223 | 190 | | foreach( XElement elem in xml.Descendants( @"add" ) ) |
| 83 | 191 | | { |
| 83 | 192 | | ProcessSetting( elem ); |
| 83 | 193 | | } |
| 19 | 194 | | } |
| 21 | 195 | | } |
| | 196 | |
|
| | 197 | | #endregion |
| | 198 | | } |