| | | 1 | | using System; |
| | | 2 | | using System.Collections.Concurrent; |
| | | 3 | | using System.Collections.Generic; |
| | | 4 | | |
| | | 5 | | namespace AmbientServices; |
| | | 6 | | |
| | | 7 | | /// <summary> |
| | | 8 | | /// A basic settings set implementation that may be used for unit test dependency injection. |
| | | 9 | | /// </summary> |
| | | 10 | | /// <remarks> |
| | | 11 | | /// <pitch>The zero-configuration, in-memory mutable settings set used unless overridden — well suited to unit-test depe |
| | | 12 | | /// <pledge><see cref="IAmbientSettingsSet"/></pledge> |
| | | 13 | | /// <plan> |
| | | 14 | | /// Keeps raw values in one <see cref="ConcurrentDictionary{TKey,TValue}"/> and eagerly-converted typed values in a seco |
| | | 15 | | /// </plan> |
| | | 16 | | /// </remarks> |
| | | 17 | | [DefaultAmbientService(typeof(IAmbientSettingsSet))] |
| | | 18 | | public class BasicAmbientSettingsSet : IAmbientSettingsSet |
| | | 19 | | { |
| | | 20 | | /// <summary> |
| | | 21 | | /// The set name of the default settings set. |
| | | 22 | | /// </summary> |
| | | 23 | | public const string DefaultSetName = "Default"; |
| | | 24 | | private readonly LazyUnsubscribeWeakEventListenerProxy<BasicAmbientSettingsSet, object?, IAmbientSettingInfo> _weakS |
| | | 25 | | private readonly ConcurrentDictionary<string, string> _rawValues; |
| | | 26 | | private readonly ConcurrentDictionary<string, object> _typedValues; |
| | | 27 | | |
| | | 28 | | /// <summary> |
| | | 29 | | /// Constructs the default ambient settings set. |
| | | 30 | | /// </summary> |
| | | 31 | | internal BasicAmbientSettingsSet() |
| | 3 | 32 | | : this (DefaultSetName) |
| | | 33 | | { |
| | 3 | 34 | | } |
| | | 35 | | /// <summary> |
| | | 36 | | /// Constructs a new ambient settings set with the specified values. |
| | | 37 | | /// </summary> |
| | | 38 | | /// <param name="name">The name of the set.</param> |
| | 3 | 39 | | public BasicAmbientSettingsSet(string name) |
| | | 40 | | { |
| | 3 | 41 | | SetName = name; |
| | 3 | 42 | | _rawValues = new ConcurrentDictionary<string, string>(); |
| | 3 | 43 | | _typedValues = new ConcurrentDictionary<string, object>(); |
| | 3 | 44 | | _weakSettingRegistered = new LazyUnsubscribeWeakEventListenerProxy<BasicAmbientSettingsSet, object?, IAmbientSet |
| | 2 | 45 | | this, NewSettingRegistered, wvc => SettingsRegistry.DefaultRegistry.SettingRegistered -= wvc.WeakEventHa |
| | 3 | 46 | | SettingsRegistry.DefaultRegistry.SettingRegistered += _weakSettingRegistered.WeakEventHandler; |
| | 3 | 47 | | } |
| | | 48 | | /// <summary> |
| | | 49 | | /// Constructs a new ambient settings set with the specified values. |
| | | 50 | | /// </summary> |
| | | 51 | | /// <param name="name">The name of the set.</param> |
| | | 52 | | /// <param name="values">A set of name-value pairs to use for the initial values for the set.</param> |
| | 2 | 53 | | public BasicAmbientSettingsSet(string name, IDictionary<string, string> values) |
| | | 54 | | { |
| | 2 | 55 | | SetName = name; |
| | 2 | 56 | | _rawValues = new ConcurrentDictionary<string, string>(values); |
| | 2 | 57 | | _typedValues = new ConcurrentDictionary<string, object>(); |
| | 2 | 58 | | if (values != null) |
| | | 59 | | { |
| | 2 | 60 | | foreach (string key in values.Keys) |
| | | 61 | | { |
| | 2 | 62 | | IAmbientSettingInfo? ps = SettingsRegistry.DefaultRegistry.TryGetSetting(key); |
| | 2 | 63 | | _typedValues[key] = (ps != null) ? ps.Convert(this, values[key]) : values[key]; |
| | | 64 | | } |
| | | 65 | | } |
| | 2 | 66 | | _weakSettingRegistered = new LazyUnsubscribeWeakEventListenerProxy<BasicAmbientSettingsSet, object?, IAmbientSet |
| | 2 | 67 | | this, NewSettingRegistered, wvc => SettingsRegistry.DefaultRegistry.SettingRegistered -= wvc.WeakEventHa |
| | 2 | 68 | | SettingsRegistry.DefaultRegistry.SettingRegistered += _weakSettingRegistered.WeakEventHandler; |
| | 2 | 69 | | } |
| | | 70 | | |
| | | 71 | | private static void NewSettingRegistered(BasicAmbientSettingsSet settingsSet, object? sender, IAmbientSettingInfo se |
| | | 72 | | { |
| | | 73 | | // is there a value for this setting? |
| | | 74 | | string? value; |
| | 3 | 75 | | if (settingsSet._rawValues.TryGetValue(setting.Key, out value)) |
| | | 76 | | { |
| | | 77 | | // get the typed value |
| | 2 | 78 | | settingsSet._typedValues[setting.Key] = setting.Convert(settingsSet, value); |
| | | 79 | | } |
| | 3 | 80 | | } |
| | | 81 | | |
| | | 82 | | /// <summary> |
| | | 83 | | /// Gets the name of the settings set so that a settings consumer can know where a changed setting value came from. |
| | | 84 | | /// </summary> |
| | | 85 | | public string SetName { get; } |
| | | 86 | | /// <summary> |
| | | 87 | | /// Gets the current raw (string) value for the specified key, or null if the setting is not set. |
| | | 88 | | /// </summary> |
| | | 89 | | /// <param name="key">A key identifying the setting whose value is to be retrieved.</param> |
| | | 90 | | /// <returns>The setting value, or null if the setting is not set.</returns> |
| | | 91 | | |
| | | 92 | | public string? GetRawValue(string key) |
| | | 93 | | { |
| | | 94 | | string? value; |
| | 2 | 95 | | return _rawValues.TryGetValue(key, out value) ? value : null; |
| | | 96 | | } |
| | | 97 | | /// <summary> |
| | | 98 | | /// Gets the current typed value for the setting with the specified key, or null if the setting is not set. |
| | | 99 | | /// </summary> |
| | | 100 | | /// <param name="key">A key identifying the setting whose value is to be retrieved.</param> |
| | | 101 | | /// <returns>The setting value, or null if the setting is not set.</returns> |
| | | 102 | | public object? GetTypedValue(string key) |
| | | 103 | | { |
| | | 104 | | object? value; |
| | 3 | 105 | | return _typedValues.TryGetValue(key, out value) ? value : null; |
| | | 106 | | } |
| | | 107 | | /// <summary> |
| | | 108 | | /// Gets whether or not the settings set is mutable. |
| | | 109 | | /// </summary> |
| | 3 | 110 | | public bool SettingsAreMutable => true; |
| | | 111 | | /// <summary> |
| | | 112 | | /// Changes the specified setting. |
| | | 113 | | /// For many ambient settings services, the value will only be reflected in memory until the process shuts down, but |
| | | 114 | | /// </summary> |
| | | 115 | | /// <param name="key">A string that uniquely identifies the setting.</param> |
| | | 116 | | /// <param name="value">The new string value for the setting, or null if the setting should be removed.</param> |
| | | 117 | | /// <returns>Whether or not the setting actually changed.</returns> |
| | | 118 | | public bool ChangeSetting(string key, string? value) |
| | | 119 | | { |
| | 3 | 120 | | if (value == null) |
| | | 121 | | { |
| | | 122 | | string? oldValue; |
| | 2 | 123 | | _rawValues.TryRemove(key, out oldValue); |
| | 2 | 124 | | _typedValues.TryRemove(key, out _); |
| | | 125 | | // did the value *not* change? bail out early |
| | 2 | 126 | | if (oldValue == null) return false; |
| | | 127 | | } |
| | | 128 | | else |
| | | 129 | | { |
| | 3 | 130 | | string? oldValue = null; |
| | 2 | 131 | | _rawValues.AddOrUpdate(key, value, (k, v) => { oldValue = v; return value; }); |
| | | 132 | | // did the value *not* change? bail out early |
| | 3 | 133 | | if (string.Equals(value, oldValue, StringComparison.Ordinal)) return false; |
| | 3 | 134 | | IAmbientSettingInfo? ps = SettingsRegistry.DefaultRegistry.TryGetSetting(key); |
| | 3 | 135 | | _typedValues[key] = (ps != null) ? ps.Convert(this, value) : value; |
| | | 136 | | } |
| | 3 | 137 | | return true; |
| | | 138 | | } |
| | | 139 | | /// <summary> |
| | | 140 | | /// Gets a string representing the settings instance. |
| | | 141 | | /// </summary> |
| | | 142 | | /// <returns></returns> |
| | | 143 | | public override string ToString() |
| | | 144 | | { |
| | 2 | 145 | | return "Settings: " + SetName; |
| | | 146 | | } |
| | | 147 | | } |