/// <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)); }