Skip to content

Commit

Permalink
Updated the Add Config methods to expose the optional and reload on c…
Browse files Browse the repository at this point in the history
…hange settings.
  • Loading branch information
erikbartlow committed Jun 11, 2024
1 parent 01cdacb commit 42a4c55
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 117 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<PackageId>$(AssemblyName)</PackageId>
<Title>CodedThought Core Configuration</Title>
<AssemblyVersion>8.0.0.5</AssemblyVersion>
<FileVersion>8.0.0.5</FileVersion>
<AssemblyVersion>8.0.0.6</AssemblyVersion>
<FileVersion>8.0.0.6</FileVersion>
<Description>The CodedThought.Core.Configuration library is a custom settings provider for the CodedThought.Core library.</Description>
<RepositoryUrl>https://github.com/erikbartlow/CodedThought.Core/tree/main</RepositoryUrl>
<Version>8.0.0-beta-3</Version>
<Version>8.0.0-beta-4</Version>
<Authors>Erik Bartlow</Authors>
<Company>CodedThought</Company>
<PackageProjectUrl>https://github.com/erikbartlow/CodedThought.Core</PackageProjectUrl>
Expand Down
221 changes: 107 additions & 114 deletions CodedThought.Core.Configuration/CoreSettingsExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,121 +6,114 @@
namespace CodedThought.Core.Configuration
{

public static class CoreSettingsExtensions
{
public static class CoreSettingsExtensions
{

/// <summary>
/// Loads the default ctsettings.json file to the builder.
/// </summary>
/// <param name="builder"></param>
/// <returns></returns>
/// <remarks>The default name for the settings file is ctSettings.json</remarks>
public static IConfigurationBuilder AddCoreSettingsConfiguration(this IConfigurationBuilder builder) => builder.AddCoreSettingsConfiguration("ctSettings.json");
/// <summary>
/// Loads the default ctsettings.json file to the builder.
/// </summary>
/// <param name="builder"></param>
/// <returns></returns>
public static IConfigurationBuilder AddCoreSettingsConfiguration(this IConfigurationBuilder builder, string settingsFileName)
{
builder.AddJsonFile(settingsFileName, optional: false, reloadOnChange: true);
return builder;
}
/// <summary>
/// Loads the default ctsettings.json file and the corresponding environment instance of the ctsettings.{env}.json file.
/// </summary>
/// <param name="builder"></param>
/// <param name="env"></param>
/// <returns></returns>
/// <remarks>The naming of the settings json file is case sensitive and should be match the environment name in the ASPNETCORE_ENVIRONMENT variable.</remarks>
public static IConfigurationBuilder AddCoreSettingsConfiguration(this IConfigurationBuilder builder, IHostEnvironment env) => builder.AddCoreSettingsConfiguration(env, "ctSettings.json");
/// <summary>
/// Loads the default ctSettings.json file to the builder.
/// </summary>
/// <param name="builder"></param>
/// <returns></returns>
/// <remarks>The default name for the settings file is ctSettings.json</remarks>
public static IConfigurationBuilder AddCoreSettingsConfiguration(this IConfigurationBuilder builder, bool optional = true, bool reloadOnChange = true) => builder.AddCoreSettingsConfiguration("ctSettings.json", optional, reloadOnChange);
/// <summary>
/// Loads the CodedThought configuration json file to the builder using the passed file name.
/// </summary>
/// <param name="builder"></param>
/// <returns></returns>
public static IConfigurationBuilder AddCoreSettingsConfiguration(this IConfigurationBuilder builder, string settingsFileName, bool optional = true, bool reloadOnChange = true)
{
builder.AddJsonFile(settingsFileName, optional, reloadOnChange);
return builder;
}
/// <summary>
/// Loads the default ctsettings.json file and the corresponding environment instance of the ctsettings.{env}.json file.
/// </summary>
/// <param name="builder"></param>
/// <param name="env"></param>
/// <returns></returns>
/// <remarks>The naming of the settings json file is case sensitive and should be match the environment name in the ASPNETCORE_ENVIRONMENT variable.</remarks>
public static IConfigurationBuilder AddCoreSettingsConfiguration(this IConfigurationBuilder builder, IHostEnvironment env, bool optional = true, bool reloadOnChange = true) => builder.AddCoreSettingsConfiguration(env, "ctSettings.json", optional, reloadOnChange);

/// <summary>
/// Loads the default ctsettings.json file and the corresponding environment instance of the ctsettings.{env}.json file.
/// </summary>
/// <param name="builder"></param>
/// <param name="env"></param>
/// <returns></returns>
/// <remarks>The naming of the settings json file is case sensitive and should be match the environment name in the ASPNETCORE_ENVIRONMENT variable.</remarks>
public static IConfigurationBuilder AddCoreSettingsConfiguration(this IConfigurationBuilder builder, IHostEnvironment env, string settingsFileName)
{
builder.AddJsonFile(settingsFileName, optional: false, reloadOnChange: true);
if (env.EnvironmentName.ToLower() != "production")
{
// Remove any .json extensions.
settingsFileName = settingsFileName.Replace(".json", "");
builder.AddJsonFile($"{settingsFileName}.{env.EnvironmentName}.json", optional: true, reloadOnChange: true);
}
else
{
builder.AddJsonFile(settingsFileName, optional: true, reloadOnChange: true);
}
/// <summary>
/// Loads the CodedThought.Core settings file using the passed file name and the corresponding environment instance of the {settings file name}.{env}.json file.
/// </summary>
/// <param name="builder"></param>
/// <param name="env"></param>
/// <returns></returns>
/// <remarks>The naming of the settings json file is case sensitive and should be match the environment name in the ASPNETCORE_ENVIRONMENT variable.</remarks>
public static IConfigurationBuilder AddCoreSettingsConfiguration(this IConfigurationBuilder builder, IHostEnvironment env, string settingsFileName, bool optional = true, bool reloadOnChange = true)
{
builder.AddJsonFile(settingsFileName, optional: false, reloadOnChange: true);
// Remove any .json extensions.
settingsFileName = settingsFileName.Replace(".json", "");
builder.AddJsonFile($"{settingsFileName}.{env.EnvironmentName}.json", optional, reloadOnChange);
return builder;
}
/// <summary>
/// Loads the default appsettings.json file to the builder.
/// </summary>
/// <param name="builder"></param>
/// <returns></returns>
public static IConfigurationBuilder AddAppSettingsConfiguration(this IConfigurationBuilder builder, bool optional = false, bool reloadOnChange = true)
{
builder.AddJsonFile("appsettings.json", optional, reloadOnChange);
return builder;
}
/// <summary>
/// Loads the default appsettings.json file and the corresponding environment instance of the appsettings.{env}.json file.
/// </summary>
/// <param name="builder"></param>
/// <param name="env"></param>
/// <returns></returns>
/// <remarks>The naming of the settings json file is case sensitive and should be match the environment name in the ASPNETCORE_ENVIRONMENT variable.</remarks>
public static IConfigurationBuilder AddAppSettingsConfiguration(this IConfigurationBuilder builder, IHostEnvironment env, bool optional = true, bool reloadOnChange = true)
{
AddAppSettingsConfiguration(builder);
builder.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional, reloadOnChange);
return builder;
}
/// <summary>
/// Loads the default appsettings.json file to the builder.
/// </summary>
/// <param name="builder"></param>
/// <returns></returns>
public static IConfigurationBuilder AddAppSettingsConfiguration(this IConfigurationBuilder builder)
{
builder.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);
return builder;
}
/// <summary>
/// Loads the default appsettings.json file and the corresponding environment instance of the appsettings.{env}.json file.
/// </summary>
/// <param name="builder"></param>
/// <param name="env"></param>
/// <returns></returns>
/// <remarks>The naming of the settings json file is case sensitive and should be match the environment name in the ASPNETCORE_ENVIRONMENT variable.</remarks>
public static IConfigurationBuilder AddAppSettingsConfiguration(this IConfigurationBuilder builder, IHostEnvironment env)
{
AddAppSettingsConfiguration(builder);
builder.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true);
return builder;
}
[Obsolete("This method is obsolete as it was renamed to GetCorePrimaryConnection.")]
public static ConnectionSetting GetCorePrimaryConnectionString(this IConfiguration configuration)
{
CoreSettings options = new() { Settings = [], Connections = [] };
configuration.GetSection(nameof(CoreSettings)).Bind(options);
return options.Connections.FirstOrDefault(x => x.Primary == true);
}
/// <summary>
/// Gets a distinct list of ProviderType values from each connection in the Connections section.
/// </summary>
/// <param name="configuration"></param>
/// <returns></returns>
public static List<string> GetCoreDatabaseProviders(this IConfiguration configuration)
{
List<String> configuredProviders = [];
CoreSettings options = new() { Settings = [], Connections = [] };
configuration.GetSection(nameof(CoreSettings)).Bind(options);
configuredProviders = options.Connections.Select(cn => cn.ProviderType).Distinct().ToList();
return configuredProviders;
}
/// <summary>
/// Gets the database connection from the ctsettings file with the Primary=true setting.
/// </summary>
/// <param name="configuration"></param>
/// <returns></returns>
public static ConnectionSetting GetCorePrimaryConnection(this IConfiguration configuration)
{
CoreSettings options = new() { Settings = [], Connections = [] };
configuration.GetSection(nameof(CoreSettings)).Bind(options);
return options.Connections.FirstOrDefault(x => x.Primary == true);
}
/// <summary>
/// Gets the database connection from the ctsettings file matching the passed name.
/// </summary>
/// <param name="configuration"></param>
/// <returns></returns>
public static ConnectionSetting GetDatabaseConnection(this IConfiguration configuration, string name)
{
CoreSettings options = new() { Settings = [], Connections = [] };
configuration.GetSection(nameof(CoreSettings)).Bind(options);
return options.Connections.FirstOrDefault(x => x.Name.ToUpper() == name.ToUpper());
}
}
}
[Obsolete("This method is obsolete as it was renamed to GetCorePrimaryConnection.")]
public static ConnectionSetting GetCorePrimaryConnectionString(this IConfiguration configuration)
{
CoreSettings options = new() { Settings = [], Connections = [] };
configuration.GetSection(nameof(CoreSettings)).Bind(options);
return options.Connections.FirstOrDefault(x => x.Primary == true);
}
/// <summary>
/// Gets a distinct list of ProviderType values from each connection in the Connections section.
/// </summary>
/// <param name="configuration"></param>
/// <returns></returns>
public static List<string> GetCoreDatabaseProviders(this IConfiguration configuration)
{
List<String> configuredProviders = [];
CoreSettings options = new() { Settings = [], Connections = [] };
configuration.GetSection(nameof(CoreSettings)).Bind(options);
configuredProviders = options.Connections.Select(cn => cn.ProviderType).Distinct().ToList();
return configuredProviders;
}
/// <summary>
/// Gets the database connection from the ctsettings file with the Primary=true setting.
/// </summary>
/// <param name="configuration"></param>
/// <returns></returns>
public static ConnectionSetting GetCorePrimaryConnection(this IConfiguration configuration)
{
CoreSettings options = new() { Settings = [], Connections = [] };
configuration.GetSection(nameof(CoreSettings)).Bind(options);
return options.Connections.FirstOrDefault(x => x.Primary == true);
}
/// <summary>
/// Gets the database connection from the ctsettings file matching the passed name.
/// </summary>
/// <param name="configuration"></param>
/// <returns></returns>
public static ConnectionSetting GetDatabaseConnection(this IConfiguration configuration, string name)
{
CoreSettings options = new() { Settings = [], Connections = [] };
configuration.GetSection(nameof(CoreSettings)).Bind(options);
return options.Connections.FirstOrDefault(x => x.Name.ToUpper() == name.ToUpper());
}
}
}

0 comments on commit 42a4c55

Please sign in to comment.