| | 1 | | using System.Configuration; |
| | 2 | |
|
| | 3 | | namespace Songhay.Extensions; |
| | 4 | |
|
| | 5 | | /// <summary> |
| | 6 | | /// Extensions of <see cref="Microsoft.Extensions.Configuration.ConfigurationManager"/> |
| | 7 | | /// </summary> |
| | 8 | | /// <remarks> |
| | 9 | | /// Several members in this class depend on <see cref="DeploymentEnvironment"/> constants. |
| | 10 | | /// </remarks> |
| | 11 | | public static class ConfigurationManagerExtensions |
| | 12 | | { |
| | 13 | | /// <summary> |
| | 14 | | /// Gets the connection name from environment. |
| | 15 | | /// </summary> |
| | 16 | | /// <param name="collection">The collection.</param> |
| | 17 | | /// <param name="unqualifiedKey">The unqualified key.</param> |
| | 18 | | /// <param name="environmentName">Name of the environment.</param> |
| | 19 | | public static string? GetConnectionNameFromEnvironment(this ConnectionStringSettingsCollection? collection, |
| | 20 | | string? unqualifiedKey, string? environmentName) => |
| 2 | 21 | | collection.GetConnectionNameFromEnvironment(unqualifiedKey, environmentName, |
| 2 | 22 | | DeploymentEnvironment.ConfigurationKeyDelimiter); |
| | 23 | |
|
| | 24 | | /// <summary> |
| | 25 | | /// Gets the connection name from environment. |
| | 26 | | /// </summary> |
| | 27 | | /// <param name="collection">The collection.</param> |
| | 28 | | /// <param name="unqualifiedKey">The unqualified key.</param> |
| | 29 | | /// <param name="environmentName">Name of the environment.</param> |
| | 30 | | /// <param name="delimiter">The delimiter.</param> |
| | 31 | | public static string? GetConnectionNameFromEnvironment(this ConnectionStringSettingsCollection? collection, |
| | 32 | | string? unqualifiedKey, string? environmentName, string? delimiter) => |
| 2 | 33 | | collection.GetConnectionNameFromEnvironment(unqualifiedKey, environmentName, delimiter, |
| 2 | 34 | | throwConfigurationErrorsException: true); |
| | 35 | |
|
| | 36 | | /// <summary> |
| | 37 | | /// Gets the connection name from environment. |
| | 38 | | /// </summary> |
| | 39 | | /// <param name="collection">The collection.</param> |
| | 40 | | /// <param name="unqualifiedKey">The unqualified key.</param> |
| | 41 | | /// <param name="environmentName">Name of the environment.</param> |
| | 42 | | /// <param name="throwConfigurationErrorsException">if set to <c>true</c> throw configuration errors exception.</par |
| | 43 | | public static string? GetConnectionNameFromEnvironment(this ConnectionStringSettingsCollection? collection, |
| | 44 | | string? unqualifiedKey, string? environmentName, bool throwConfigurationErrorsException) |
| 0 | 45 | | { |
| 0 | 46 | | return collection.GetConnectionNameFromEnvironment(unqualifiedKey, environmentName, |
| 0 | 47 | | DeploymentEnvironment.ConfigurationKeyDelimiter, throwConfigurationErrorsException); |
| 0 | 48 | | } |
| | 49 | |
|
| | 50 | | /// <summary> |
| | 51 | | /// Gets the connection name from environment. |
| | 52 | | /// </summary> |
| | 53 | | /// <param name="collection">The collection.</param> |
| | 54 | | /// <param name="unqualifiedKey">The unqualified key.</param> |
| | 55 | | /// <param name="environmentName">Name of the environment.</param> |
| | 56 | | /// <param name="delimiter">The delimiter.</param> |
| | 57 | | /// <param name="throwConfigurationErrorsException">if set to <c>true</c> throw configuration errors exception.</par |
| | 58 | | public static string? GetConnectionNameFromEnvironment(this ConnectionStringSettingsCollection? collection, |
| | 59 | | string? unqualifiedKey, string? environmentName, string? delimiter, bool throwConfigurationErrorsException) |
| 2 | 60 | | { |
| 2 | 61 | | if (collection == null) return null; |
| 2 | 62 | | unqualifiedKey.ThrowWhenNullOrWhiteSpace(); |
| | 63 | |
|
| 2 | 64 | | var name1 = string.Concat(environmentName, delimiter, unqualifiedKey); |
| 2 | 65 | | var name2 = string.Concat(unqualifiedKey, delimiter, environmentName); |
| | 66 | |
|
| 5 | 67 | | var containsKey1 = collection.OfType<ConnectionStringSettings>().Any(i => i.Name == name1); |
| 2 | 68 | | if (!containsKey1 && collection.OfType<ConnectionStringSettings>().All(i => i.Name != name2)) |
| 0 | 69 | | { |
| 0 | 70 | | if (throwConfigurationErrorsException) |
| 0 | 71 | | throw new ConfigurationErrorsException($"The expected Name, “{name1}” or “{name2}”, is not here."); |
| 0 | 72 | | return null; |
| | 73 | | } |
| | 74 | |
|
| 2 | 75 | | return containsKey1 ? name1 : name2; |
| 2 | 76 | | } |
| | 77 | |
|
| | 78 | | /// <summary> |
| | 79 | | /// Gets the connection string settings. |
| | 80 | | /// </summary> |
| | 81 | | /// <param name="collection">The collection.</param> |
| | 82 | | /// <param name="connectionName">Name of the connection.</param> |
| | 83 | | public static ConnectionStringSettings? GetConnectionStringSettings( |
| | 84 | | this ConnectionStringSettingsCollection? collection, string? connectionName) => |
| 2 | 85 | | collection.GetConnectionStringSettings(connectionName, throwConfigurationErrorsException: false); |
| | 86 | |
|
| | 87 | | /// <summary> |
| | 88 | | /// Gets the connection string settings. |
| | 89 | | /// </summary> |
| | 90 | | /// <param name="collection">The collection.</param> |
| | 91 | | /// <param name="connectionName">Name of the connection.</param> |
| | 92 | | /// <param name="throwConfigurationErrorsException">if set to <c>true</c> throw configuration errors exception.</par |
| | 93 | | public static ConnectionStringSettings? GetConnectionStringSettings( |
| | 94 | | this ConnectionStringSettingsCollection? collection, string? connectionName, |
| | 95 | | bool throwConfigurationErrorsException) |
| 2 | 96 | | { |
| 2 | 97 | | if (collection == null) return null; |
| 2 | 98 | | if (string.IsNullOrWhiteSpace(connectionName)) return null; |
| | 99 | |
|
| 2 | 100 | | var setting = collection[connectionName]; |
| | 101 | |
|
| 4 | 102 | | if (setting != null || !throwConfigurationErrorsException) return setting; |
| | 103 | |
|
| 0 | 104 | | var message = $"The expected connection settings, {connectionName}, are not here."; |
| 0 | 105 | | throw new ConfigurationErrorsException(message); |
| 2 | 106 | | } |
| | 107 | |
|
| | 108 | | /// <summary> |
| | 109 | | /// Gets the name of the conventional deployment environment. |
| | 110 | | /// </summary> |
| | 111 | | /// <param name="settings">The settings.</param> |
| | 112 | | public static string? GetEnvironmentName(this KeyValueConfigurationCollection? settings) |
| 0 | 113 | | { |
| 0 | 114 | | return settings.GetEnvironmentName(environmentKey: DeploymentEnvironment.ConfigurationKey, |
| 0 | 115 | | defaultEnvironmentName: DeploymentEnvironment.DevelopmentEnvironmentName); |
| 0 | 116 | | } |
| | 117 | |
|
| | 118 | | /// <summary> |
| | 119 | | /// Gets the name of the conventional deployment environment. |
| | 120 | | /// </summary> |
| | 121 | | /// <param name="settings">The settings.</param> |
| | 122 | | /// <param name="environmentKey">The environment key.</param> |
| | 123 | | public static string? GetEnvironmentName(this KeyValueConfigurationCollection? settings, string? environmentKey) |
| 0 | 124 | | { |
| 0 | 125 | | return settings.GetEnvironmentName(environmentKey, |
| 0 | 126 | | defaultEnvironmentName: DeploymentEnvironment.DevelopmentEnvironmentName); |
| 0 | 127 | | } |
| | 128 | |
|
| | 129 | | /// <summary> |
| | 130 | | /// Gets the name of the conventional deployment environment. |
| | 131 | | /// </summary> |
| | 132 | | /// <param name="settings">The settings.</param> |
| | 133 | | /// <param name="environmentKey">The environment key.</param> |
| | 134 | | /// <param name="defaultEnvironmentName">Default name of the environment.</param> |
| | 135 | | public static string? GetEnvironmentName(this KeyValueConfigurationCollection? settings, string? environmentKey, |
| | 136 | | string defaultEnvironmentName) |
| 6 | 137 | | { |
| 6 | 138 | | return settings.GetEnvironmentName(environmentKey, defaultEnvironmentName, |
| 6 | 139 | | throwConfigurationErrorsException: true); |
| 6 | 140 | | } |
| | 141 | |
|
| | 142 | | /// <summary> |
| | 143 | | /// Gets the name of the conventional deployment environment. |
| | 144 | | /// </summary> |
| | 145 | | /// <param name="settings">The settings.</param> |
| | 146 | | /// <param name="environmentKey">The environment key.</param> |
| | 147 | | /// <param name="defaultEnvironmentName">Default name of the environment.</param> |
| | 148 | | /// <param name="throwConfigurationErrorsException">if set to <c>true</c> throw configuration errors exception.</par |
| | 149 | | public static string? GetEnvironmentName(this KeyValueConfigurationCollection? settings, string? environmentKey, |
| | 150 | | string defaultEnvironmentName, bool throwConfigurationErrorsException) |
| 6 | 151 | | { |
| 6 | 152 | | if (settings == null) return null; |
| | 153 | |
|
| 6 | 154 | | var hasKey = settings.AllKeys.Contains(environmentKey); |
| | 155 | |
|
| 6 | 156 | | if (!hasKey && !string.IsNullOrWhiteSpace(defaultEnvironmentName)) |
| 0 | 157 | | return defaultEnvironmentName; |
| | 158 | |
|
| 6 | 159 | | if (hasKey || !string.IsNullOrWhiteSpace(defaultEnvironmentName)) |
| 6 | 160 | | return settings.GetSetting(environmentKey, throwConfigurationErrorsException: true); |
| | 161 | |
|
| 0 | 162 | | if (throwConfigurationErrorsException) |
| 0 | 163 | | throw new ConfigurationErrorsException( |
| 0 | 164 | | $"The expected Environment Key, `{environmentKey}`, is not here."); |
| | 165 | |
|
| 0 | 166 | | return null; |
| 6 | 167 | | } |
| | 168 | |
|
| | 169 | | /// <summary> |
| | 170 | | /// Gets the name of the key with environment. |
| | 171 | | /// </summary> |
| | 172 | | /// <param name="settings">The settings.</param> |
| | 173 | | /// <param name="unqualifiedKey">The unqualified key.</param> |
| | 174 | | /// <param name="environmentName">Name of the environment.</param> |
| | 175 | | public static string? GetKeyWithEnvironmentName(this KeyValueConfigurationCollection? settings, |
| | 176 | | string? unqualifiedKey, string? environmentName) |
| 3 | 177 | | { |
| 3 | 178 | | return settings.GetKeyWithEnvironmentName(unqualifiedKey, environmentName, |
| 3 | 179 | | DeploymentEnvironment.ConfigurationKeyDelimiter); |
| 3 | 180 | | } |
| | 181 | |
|
| | 182 | | /// <summary> |
| | 183 | | /// Gets the key with environment name. |
| | 184 | | /// </summary> |
| | 185 | | /// <param name="settings">The settings.</param> |
| | 186 | | /// <param name="unqualifiedKey">The unqualified key.</param> |
| | 187 | | /// <param name="environmentName">Name of the environment.</param> |
| | 188 | | /// <param name="delimiter">The delimiter.</param> |
| | 189 | | public static string? GetKeyWithEnvironmentName(this KeyValueConfigurationCollection? settings, |
| | 190 | | string? unqualifiedKey, string? environmentName, string? delimiter) |
| 3 | 191 | | { |
| 3 | 192 | | return settings.GetKeyWithEnvironmentName(unqualifiedKey, environmentName, delimiter, |
| 3 | 193 | | throwConfigurationErrorsException: true); |
| 3 | 194 | | } |
| | 195 | |
|
| | 196 | | /// <summary> |
| | 197 | | /// Gets the name of the key with environment. |
| | 198 | | /// </summary> |
| | 199 | | /// <param name="settings">The settings.</param> |
| | 200 | | /// <param name="unqualifiedKey">The unqualified key.</param> |
| | 201 | | /// <param name="environmentName">Name of the environment.</param> |
| | 202 | | /// <param name="throwConfigurationErrorsException">if set to <c>true</c> [throw configuration errors exception].</p |
| | 203 | | public static string? GetKeyWithEnvironmentName(this KeyValueConfigurationCollection? settings, |
| | 204 | | string? unqualifiedKey, string? environmentName, bool throwConfigurationErrorsException) |
| 0 | 205 | | { |
| 0 | 206 | | return settings.GetKeyWithEnvironmentName(unqualifiedKey, environmentName, |
| 0 | 207 | | DeploymentEnvironment.ConfigurationKeyDelimiter, throwConfigurationErrorsException); |
| 0 | 208 | | } |
| | 209 | |
|
| | 210 | | /// <summary> |
| | 211 | | /// Gets the key with environment name. |
| | 212 | | /// </summary> |
| | 213 | | /// <param name="settings">The settings.</param> |
| | 214 | | /// <param name="unqualifiedKey">The unqualified key.</param> |
| | 215 | | /// <param name="environmentName">Name of the environment.</param> |
| | 216 | | /// <param name="delimiter">The delimiter.</param> |
| | 217 | | /// <param name="throwConfigurationErrorsException">if set to <c>true</c> throw configuration errors exception.</par |
| | 218 | | public static string? GetKeyWithEnvironmentName(this KeyValueConfigurationCollection? settings, |
| | 219 | | string? unqualifiedKey, string? environmentName, string? delimiter, bool throwConfigurationErrorsException) |
| 3 | 220 | | { |
| 3 | 221 | | if (settings == null) return null; |
| 3 | 222 | | unqualifiedKey.ThrowWhenNullOrWhiteSpace(); |
| | 223 | |
|
| 3 | 224 | | var key1 = string.Concat(environmentName, delimiter, unqualifiedKey); |
| 3 | 225 | | var key2 = string.Concat(unqualifiedKey, delimiter, environmentName); |
| | 226 | |
|
| 3 | 227 | | var containsKey1 = settings.AllKeys.Contains(key1); |
| | 228 | |
|
| 6 | 229 | | if (containsKey1 || settings.AllKeys.Contains(key2)) return containsKey1 ? key1 : key2; |
| | 230 | |
|
| 0 | 231 | | if (throwConfigurationErrorsException) |
| 0 | 232 | | throw new ConfigurationErrorsException($"The expected Key, “{key1}” or “{key2}”, is not here."); |
| | 233 | |
|
| 0 | 234 | | return null; |
| 3 | 235 | | } |
| | 236 | |
|
| | 237 | | /// <summary> |
| | 238 | | /// Gets the setting from the current <see cref="KeyValueConfigurationCollection"/>. |
| | 239 | | /// </summary> |
| | 240 | | /// <param name="settings">The settings.</param> |
| | 241 | | /// <param name="key">The key.</param> |
| | 242 | | public static string? GetSetting(this KeyValueConfigurationCollection? settings, string? key) => |
| 6 | 243 | | settings.GetSetting(key, throwConfigurationErrorsException: false); |
| | 244 | |
|
| | 245 | | /// <summary> |
| | 246 | | /// Gets the setting from the current <see cref="KeyValueConfigurationCollection"/>. |
| | 247 | | /// </summary> |
| | 248 | | /// <param name="settings">The settings.</param> |
| | 249 | | /// <param name="key">The key.</param> |
| | 250 | | /// <param name="throwConfigurationErrorsException">if set to <c>true</c> throw configuration errors exception.</par |
| | 251 | | public static string? GetSetting(this KeyValueConfigurationCollection? settings, string? key, |
| | 252 | | bool throwConfigurationErrorsException) |
| 12 | 253 | | { |
| 12 | 254 | | if (settings == null) return null; |
| 12 | 255 | | if (string.IsNullOrWhiteSpace(key)) return null; |
| | 256 | |
|
| 12 | 257 | | var setting = settings[key]; |
| | 258 | |
|
| 24 | 259 | | if (setting != null || !throwConfigurationErrorsException) return setting?.Value; |
| | 260 | |
|
| 0 | 261 | | var message = $"The expected setting, {key}, is not here."; |
| 0 | 262 | | throw new ConfigurationErrorsException(message); |
| 12 | 263 | | } |
| | 264 | |
|
| | 265 | | /// <summary> |
| | 266 | | /// Gets the setting from the current <see cref="KeyValueConfigurationCollection"/>. |
| | 267 | | /// </summary> |
| | 268 | | /// <param name="settings">The settings.</param> |
| | 269 | | /// <param name="key">The key.</param> |
| | 270 | | /// <param name="throwConfigurationErrorsException">if set to <c>true</c> throw configuration errors exception.</par |
| | 271 | | /// <param name="settingModifier">The setting modifier.</param> |
| | 272 | | public static string? GetSetting(this KeyValueConfigurationCollection? settings, string? key, |
| | 273 | | bool throwConfigurationErrorsException, Func<string?, string>? settingModifier) |
| 0 | 274 | | { |
| 0 | 275 | | var setting = settings.GetSetting(key, throwConfigurationErrorsException); |
| | 276 | |
|
| 0 | 277 | | return settingModifier == null ? setting : settingModifier(setting); |
| 0 | 278 | | } |
| | 279 | |
|
| | 280 | | /// <summary> |
| | 281 | | /// Converts the external configuration file |
| | 282 | | /// to the application settings <see cref="KeyValueConfigurationCollection"/>. |
| | 283 | | /// </summary> |
| | 284 | | /// <param name="externalConfigurationDoc">The external configuration document.</param> |
| | 285 | | public static KeyValueConfigurationCollection? ToAppSettings(this XDocument? externalConfigurationDoc) |
| 1 | 286 | | { |
| 1 | 287 | | if (externalConfigurationDoc == null) return null; |
| | 288 | |
|
| 1 | 289 | | var collection = new KeyValueConfigurationCollection(); |
| | 290 | |
|
| 1 | 291 | | externalConfigurationDoc.Root? |
| 1 | 292 | | .Element("appSettings")? |
| 1 | 293 | | .Elements("add").ForEachInEnumerable(i => |
| 4 | 294 | | collection.Add(i.Attribute("key")?.Value, i.Attribute("value")?.Value)); |
| | 295 | |
|
| 1 | 296 | | return collection; |
| 1 | 297 | | } |
| | 298 | |
|
| | 299 | | /// <summary> |
| | 300 | | /// Converts the external configuration file |
| | 301 | | /// to the application settings <see cref="ConnectionStringSettingsCollection"/>. |
| | 302 | | /// </summary> |
| | 303 | | /// <param name="externalConfigurationDoc">The external configuration document.</param> |
| | 304 | | public static ConnectionStringSettingsCollection? ToConnectionStringSettingsCollection( |
| | 305 | | this XDocument? externalConfigurationDoc) |
| 1 | 306 | | { |
| 1 | 307 | | if (externalConfigurationDoc == null) return null; |
| | 308 | |
|
| 1 | 309 | | var collection = new ConnectionStringSettingsCollection(); |
| | 310 | |
|
| 1 | 311 | | externalConfigurationDoc.Root? |
| 1 | 312 | | .Element("connectionStrings")? |
| 1 | 313 | | .Elements("add").ForEachInEnumerable(i => |
| 3 | 314 | | { |
| 3 | 315 | | var name = i.Attribute("name")?.Value; |
| 3 | 316 | | var connectionString = i.Attribute("connectionString")?.Value; |
| 3 | 317 | | var providerName = i.Attribute("providerName")?.Value; |
| 3 | 318 | | var settings = new ConnectionStringSettings(name, connectionString, providerName); |
| 3 | 319 | | collection.Add(settings); |
| 4 | 320 | | }); |
| | 321 | |
|
| 1 | 322 | | return collection; |
| 1 | 323 | | } |
| | 324 | |
|
| | 325 | | /// <summary> |
| | 326 | | /// Returns <see cref="KeyValueConfigurationCollection" /> |
| | 327 | | /// with the application settings |
| | 328 | | /// of the specified external configuration <see cref="XDocument" />. |
| | 329 | | /// </summary> |
| | 330 | | /// <param name="settings">The settings.</param> |
| | 331 | | /// <param name="externalConfigurationDoc">The external configuration document.</param> |
| | 332 | | public static KeyValueConfigurationCollection? WithAppSettings(this KeyValueConfigurationCollection? settings, |
| | 333 | | XDocument? externalConfigurationDoc) |
| 1 | 334 | | { |
| 1 | 335 | | var externalCollection = externalConfigurationDoc.ToAppSettings(); |
| 1 | 336 | | if (externalCollection == null) return null; |
| | 337 | |
|
| 1 | 338 | | settings?.AllKeys.ForEachInEnumerable(key => |
| 5 | 339 | | externalCollection.Add(key, settings.GetSetting(key))); |
| | 340 | |
|
| 1 | 341 | | return externalCollection; |
| 1 | 342 | | } |
| | 343 | |
|
| | 344 | | /// <summary> |
| | 345 | | /// Returns <see cref="ConnectionStringSettingsCollection" /> |
| | 346 | | /// with the application settings |
| | 347 | | /// of the specified external configuration <see cref="XDocument" />. |
| | 348 | | /// </summary> |
| | 349 | | /// <param name="collection">The collection.</param> |
| | 350 | | /// <param name="externalConfigurationDoc">The external configuration document.</param> |
| | 351 | | public static ConnectionStringSettingsCollection? WithConnectionStringSettingsCollection( |
| | 352 | | this ConnectionStringSettingsCollection? collection, XDocument? externalConfigurationDoc) |
| 1 | 353 | | { |
| 1 | 354 | | var externalCollection = externalConfigurationDoc.ToConnectionStringSettingsCollection(); |
| | 355 | |
|
| 1 | 356 | | if (externalCollection == null) return null; |
| | 357 | |
|
| 2 | 358 | | collection?.OfType<ConnectionStringSettings>().ForEachInEnumerable(i => externalCollection.Add(i)); |
| | 359 | |
|
| 1 | 360 | | return externalCollection; |
| 1 | 361 | | } |
| | 362 | | } |