| | | 1 | | namespace Configuration.Helper; |
| | | 2 | | |
| | | 3 | | /// <summary>Class to contain the settings from a configuration file section.</summary> |
| | | 4 | | public class SettingsSection : ISettingsSection |
| | | 5 | | { |
| | | 6 | | #region Properties |
| | | 7 | | |
| | | 8 | | /// <inheritdoc /> |
| | 206 | 9 | | public IDictionary<string, string> Settings { get; } |
| | | 10 | | |
| | | 11 | | #endregion |
| | | 12 | | |
| | | 13 | | #region Constructor |
| | | 14 | | |
| | | 15 | | /// <summary>Constructor using a string comparer.</summary> |
| | | 16 | | /// <param name="comparer">Comparer to use for the key/value collection.</param> |
| | 101 | 17 | | internal SettingsSection( StringComparer comparer ) |
| | 101 | 18 | | { |
| | 101 | 19 | | Settings = new SortedDictionary<string, string>( comparer ); |
| | 101 | 20 | | } |
| | | 21 | | |
| | | 22 | | #endregion |
| | | 23 | | |
| | | 24 | | #region Internal Methods |
| | | 25 | | |
| | | 26 | | /// <summary>Adds or updates a setting in the key/value collection.</summary> |
| | | 27 | | /// <param name="key">Key of the setting.</param> |
| | | 28 | | /// <param name="value">Value of the setting.</param> |
| | | 29 | | /// <returns>True if the setting has been added or updated.</returns> |
| | | 30 | | internal bool AddSetting( string key, string value ) |
| | 96 | 31 | | { |
| | | 32 | | // Check the required parameters are supplied |
| | 98 | 33 | | if( string.IsNullOrWhiteSpace( key ) ) { return false; } |
| | | 34 | | |
| | | 35 | | // Update or add the setting |
| | 95 | 36 | | key = key.Trim(); |
| | 95 | 37 | | value = value.Trim(); |
| | 98 | 38 | | if( Settings.ContainsKey( key ) ) { Settings[key] = value; } |
| | 282 | 39 | | else { Settings.Add( key, value ); } |
| | | 40 | | |
| | 95 | 41 | | return true; |
| | 96 | 42 | | } |
| | | 43 | | |
| | | 44 | | #endregion |
| | | 45 | | |
| | | 46 | | #region Public Methods |
| | | 47 | | |
| | | 48 | | /// <inheritdoc /> |
| | | 49 | | public string GetSetting( string settingKey ) |
| | 8 | 50 | | { |
| | | 51 | | // Check the required parameter is supplied |
| | 8 | 52 | | if( string.IsNullOrWhiteSpace( settingKey ) ) { return string.Empty; } |
| | 8 | 53 | | settingKey = settingKey.Trim(); |
| | | 54 | | |
| | | 55 | | // Return the setting value |
| | 8 | 56 | | return Settings.ContainsKey( settingKey ) ? Settings[settingKey] : string.Empty; |
| | 8 | 57 | | } |
| | | 58 | | |
| | | 59 | | #endregion |
| | | 60 | | } |