< Summary - SonghayCore

Information
Class: Songhay.Extensions.ConfigurationManagerExtensions
Assembly: SonghayCore
File(s): /home/rasx/sourceRoot/SonghayCore/SonghayCore/Extensions/ConfigurationManagerExtensions.cs
Line coverage
71%
Covered lines: 91
Uncovered lines: 36
Coverable lines: 127
Total lines: 362
Line coverage: 71.6%
Branch coverage
46%
Covered branches: 38
Total branches: 82
Branch coverage: 46.3%
Method coverage

Method coverage is only available for sponsors.

Upgrade to PRO version

Metrics

File(s)

/home/rasx/sourceRoot/SonghayCore/SonghayCore/Extensions/ConfigurationManagerExtensions.cs

#LineLine coverage
 1using System.Configuration;
 2
 3namespace 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>
 11public 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) =>
 221        collection.GetConnectionNameFromEnvironment(unqualifiedKey, environmentName,
 222            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) =>
 233        collection.GetConnectionNameFromEnvironment(unqualifiedKey, environmentName, delimiter,
 234            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)
 045    {
 046        return collection.GetConnectionNameFromEnvironment(unqualifiedKey, environmentName,
 047            DeploymentEnvironment.ConfigurationKeyDelimiter, throwConfigurationErrorsException);
 048    }
 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)
 260    {
 261        if (collection == null) return null;
 262        unqualifiedKey.ThrowWhenNullOrWhiteSpace();
 63
 264        var name1 = string.Concat(environmentName, delimiter, unqualifiedKey);
 265        var name2 = string.Concat(unqualifiedKey, delimiter, environmentName);
 66
 567        var containsKey1 = collection.OfType<ConnectionStringSettings>().Any(i => i.Name == name1);
 268        if (!containsKey1 && collection.OfType<ConnectionStringSettings>().All(i => i.Name != name2))
 069        {
 070            if (throwConfigurationErrorsException)
 071                throw new ConfigurationErrorsException($"The expected Name, “{name1}” or “{name2}”, is not here.");
 072            return null;
 73        }
 74
 275        return containsKey1 ? name1 : name2;
 276    }
 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) =>
 285        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)
 296    {
 297        if (collection == null) return null;
 298        if (string.IsNullOrWhiteSpace(connectionName)) return null;
 99
 2100        var setting = collection[connectionName];
 101
 4102        if (setting != null || !throwConfigurationErrorsException) return setting;
 103
 0104        var message = $"The expected connection settings, {connectionName}, are not here.";
 0105        throw new ConfigurationErrorsException(message);
 2106    }
 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)
 0113    {
 0114        return settings.GetEnvironmentName(environmentKey: DeploymentEnvironment.ConfigurationKey,
 0115            defaultEnvironmentName: DeploymentEnvironment.DevelopmentEnvironmentName);
 0116    }
 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)
 0124    {
 0125        return settings.GetEnvironmentName(environmentKey,
 0126            defaultEnvironmentName: DeploymentEnvironment.DevelopmentEnvironmentName);
 0127    }
 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)
 6137    {
 6138        return settings.GetEnvironmentName(environmentKey, defaultEnvironmentName,
 6139            throwConfigurationErrorsException: true);
 6140    }
 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)
 6151    {
 6152        if (settings == null) return null;
 153
 6154        var hasKey = settings.AllKeys.Contains(environmentKey);
 155
 6156        if (!hasKey && !string.IsNullOrWhiteSpace(defaultEnvironmentName))
 0157            return defaultEnvironmentName;
 158
 6159        if (hasKey || !string.IsNullOrWhiteSpace(defaultEnvironmentName))
 6160            return settings.GetSetting(environmentKey, throwConfigurationErrorsException: true);
 161
 0162        if (throwConfigurationErrorsException)
 0163            throw new ConfigurationErrorsException(
 0164                $"The expected Environment Key, `{environmentKey}`, is not here.");
 165
 0166        return null;
 6167    }
 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)
 3177    {
 3178        return settings.GetKeyWithEnvironmentName(unqualifiedKey, environmentName,
 3179            DeploymentEnvironment.ConfigurationKeyDelimiter);
 3180    }
 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)
 3191    {
 3192        return settings.GetKeyWithEnvironmentName(unqualifiedKey, environmentName, delimiter,
 3193            throwConfigurationErrorsException: true);
 3194    }
 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)
 0205    {
 0206        return settings.GetKeyWithEnvironmentName(unqualifiedKey, environmentName,
 0207            DeploymentEnvironment.ConfigurationKeyDelimiter, throwConfigurationErrorsException);
 0208    }
 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)
 3220    {
 3221        if (settings == null) return null;
 3222        unqualifiedKey.ThrowWhenNullOrWhiteSpace();
 223
 3224        var key1 = string.Concat(environmentName, delimiter, unqualifiedKey);
 3225        var key2 = string.Concat(unqualifiedKey, delimiter, environmentName);
 226
 3227        var containsKey1 = settings.AllKeys.Contains(key1);
 228
 6229        if (containsKey1 || settings.AllKeys.Contains(key2)) return containsKey1 ? key1 : key2;
 230
 0231        if (throwConfigurationErrorsException)
 0232            throw new ConfigurationErrorsException($"The expected Key, “{key1}” or “{key2}”, is not here.");
 233
 0234        return null;
 3235    }
 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) =>
 6243        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)
 12253    {
 12254        if (settings == null) return null;
 12255        if (string.IsNullOrWhiteSpace(key)) return null;
 256
 12257        var setting = settings[key];
 258
 24259        if (setting != null || !throwConfigurationErrorsException) return setting?.Value;
 260
 0261        var message = $"The expected setting, {key}, is not here.";
 0262        throw new ConfigurationErrorsException(message);
 12263    }
 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)
 0274    {
 0275        var setting = settings.GetSetting(key, throwConfigurationErrorsException);
 276
 0277        return settingModifier == null ? setting : settingModifier(setting);
 0278    }
 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)
 1286    {
 1287        if (externalConfigurationDoc == null) return null;
 288
 1289        var collection = new KeyValueConfigurationCollection();
 290
 1291        externalConfigurationDoc.Root?
 1292            .Element("appSettings")?
 1293            .Elements("add").ForEachInEnumerable(i =>
 4294                collection.Add(i.Attribute("key")?.Value, i.Attribute("value")?.Value));
 295
 1296        return collection;
 1297    }
 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)
 1306    {
 1307        if (externalConfigurationDoc == null) return null;
 308
 1309        var collection = new ConnectionStringSettingsCollection();
 310
 1311        externalConfigurationDoc.Root?
 1312            .Element("connectionStrings")?
 1313            .Elements("add").ForEachInEnumerable(i =>
 3314            {
 3315                var name = i.Attribute("name")?.Value;
 3316                var connectionString = i.Attribute("connectionString")?.Value;
 3317                var providerName = i.Attribute("providerName")?.Value;
 3318                var settings = new ConnectionStringSettings(name, connectionString, providerName);
 3319                collection.Add(settings);
 4320            });
 321
 1322        return collection;
 1323    }
 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)
 1334    {
 1335        var externalCollection = externalConfigurationDoc.ToAppSettings();
 1336        if (externalCollection == null) return null;
 337
 1338        settings?.AllKeys.ForEachInEnumerable(key =>
 5339            externalCollection.Add(key, settings.GetSetting(key)));
 340
 1341        return externalCollection;
 1342    }
 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)
 1353    {
 1354        var externalCollection = externalConfigurationDoc.ToConnectionStringSettingsCollection();
 355
 1356        if (externalCollection == null) return null;
 357
 2358        collection?.OfType<ConnectionStringSettings>().ForEachInEnumerable(i => externalCollection.Add(i));
 359
 1360        return externalCollection;
 1361    }
 362}

Methods/Properties

GetConnectionNameFromEnvironment(System.Configuration.ConnectionStringSettingsCollection,System.String,System.String)
GetConnectionNameFromEnvironment(System.Configuration.ConnectionStringSettingsCollection,System.String,System.String,System.String)
GetConnectionNameFromEnvironment(System.Configuration.ConnectionStringSettingsCollection,System.String,System.String,System.Boolean)
GetConnectionNameFromEnvironment(System.Configuration.ConnectionStringSettingsCollection,System.String,System.String,System.String,System.Boolean)
GetConnectionStringSettings(System.Configuration.ConnectionStringSettingsCollection,System.String)
GetConnectionStringSettings(System.Configuration.ConnectionStringSettingsCollection,System.String,System.Boolean)
GetEnvironmentName(System.Configuration.KeyValueConfigurationCollection)
GetEnvironmentName(System.Configuration.KeyValueConfigurationCollection,System.String)
GetEnvironmentName(System.Configuration.KeyValueConfigurationCollection,System.String,System.String)
GetEnvironmentName(System.Configuration.KeyValueConfigurationCollection,System.String,System.String,System.Boolean)
GetKeyWithEnvironmentName(System.Configuration.KeyValueConfigurationCollection,System.String,System.String)
GetKeyWithEnvironmentName(System.Configuration.KeyValueConfigurationCollection,System.String,System.String,System.String)
GetKeyWithEnvironmentName(System.Configuration.KeyValueConfigurationCollection,System.String,System.String,System.Boolean)
GetKeyWithEnvironmentName(System.Configuration.KeyValueConfigurationCollection,System.String,System.String,System.String,System.Boolean)
GetSetting(System.Configuration.KeyValueConfigurationCollection,System.String)
GetSetting(System.Configuration.KeyValueConfigurationCollection,System.String,System.Boolean)
GetSetting(System.Configuration.KeyValueConfigurationCollection,System.String,System.Boolean,System.Func`2<System.String,System.String>)
ToAppSettings(System.Xml.Linq.XDocument)
ToConnectionStringSettingsCollection(System.Xml.Linq.XDocument)
WithAppSettings(System.Configuration.KeyValueConfigurationCollection,System.Xml.Linq.XDocument)
WithConnectionStringSettingsCollection(System.Configuration.ConnectionStringSettingsCollection,System.Xml.Linq.XDocument)