/// <summary>
        /// Recursively searches for a connection string by name. Allows for linking connection strings to eachother by name
        /// </summary>
        /// <param name="provider">The provider to use</param>
        /// <param name="toTest">The name (or connection string) to return</param>
        /// <returns>The furthest resolvable value representing the connection string</returns>
        public static string FindConnectionString(this IProvideConfigurations provider, string toTest = "DefaultConnectionString")
        {
            if (provider is null)
            {
                throw new ArgumentNullException(nameof(provider));
            }

            string ConnectionString;

            if (provider.GetConnectionString(toTest) != null)
            {
                ConnectionString = provider.GetConnectionString(toTest);
            }
            else if (!string.IsNullOrWhiteSpace(provider.GetConfiguration(toTest)))
            {
                ConnectionString = provider.FindConnectionString(provider.GetConfiguration(toTest));
            }
            else
            {
                ConnectionString = toTest;
            }

            if (ConnectionString is null)
            {
                throw new Exception("Can not test for null connection string. How did we get here?");
            }

            if (ConnectionString.StartsWith("name=", StringComparison.OrdinalIgnoreCase))
            {
                ConnectionString = ConnectionString.Replace("name=", "");
                ConnectionString = provider.FindConnectionString(ConnectionString);
            }

            return(ConnectionString);
        }
        /// <summary>
        /// Checks connection strings first, then configurations
        /// </summary>
        /// <param name="provider">The IConfigurationProvider</param>
        /// <param name="Name">The name of the connection string/configuration</param>
        /// <returns>The value, if any is found, or null</returns>
        public static string ConnectionStringOrConfiguration(this IProvideConfigurations provider, string Name)
        {
            if (provider is null)
            {
                throw new ArgumentNullException(nameof(provider));
            }

            return(provider.GetConnectionString(Name) ?? provider.GetConfiguration(Name));
        }
        /// <summary>
        /// Gets a configuration value as a bool
        /// </summary>
        /// <param name="provider"></param>
        /// <param name="Name">the name of the configuration value to get</param>
        /// <returns>The configuration value, or false if null</returns>
        public static bool GetBool(this IProvideConfigurations provider, string Name)
        {
            if (provider is null)
            {
                throw new ArgumentNullException(nameof(provider));
            }

            string toReturn = provider.GetConfiguration(Name);

            return(toReturn != null && bool.Parse(toReturn));
        }
        /// <summary>
        /// Gets a configuration as an int
        /// </summary>
        /// <param name="provider">The provider to use</param>
        /// <param name="Name">The name of the configuration to get</param>
        /// <returns>the int representation (or 0 if null)</returns>
        public static int GetInt(this IProvideConfigurations provider, string Name)
        {
            if (provider is null)
            {
                throw new ArgumentNullException(nameof(provider));
            }

            string toReturn = provider.GetConfiguration(Name);

            return(toReturn == null ? 0 : int.Parse(toReturn, NumberStyles.Integer, CultureInfo.CurrentCulture));
        }
示例#5
0
        /// <summary>
        /// Returns a string value from an IProvideConfigurations as a Dictionary, assuming its delimited in Key=Value; notation
        /// </summary>
        /// <param name="provider">The provider to use as a source</param>
        /// <param name="key">The Key to search for</param>
        /// <returns>A dictionary representing the key value pairs found in the configuration value</returns>
        public static Dictionary <string, string> GetDictionary(this IProvideConfigurations provider, string key)
        {
            Contract.Requires(provider != null);

            string v = provider.GetConfiguration(key);

            if (v is null)
            {
                return(null);
            }
            else
            {
                return(v.ToDictionary());
            }
        }
        /// <summary>
        /// Returns a string value from an IProvideConfigurations as a Dictionary, assuming its delimited in Key=Value; notation
        /// </summary>
        /// <param name="provider">The provider to use as a source</param>
        /// <param name="key">The Key to search for</param>
        /// <returns>A dictionary representing the key value pairs found in the configuration value</returns>
        public static Dictionary <string, string> GetDictionary(this IProvideConfigurations provider, string key)
        {
            if (provider is null)
            {
                throw new ArgumentNullException(nameof(provider));
            }

            string v = provider.GetConfiguration(key);

            if (v is null)
            {
                return(null);
            }
            else
            {
                return(v.ToDictionary());
            }
        }