Adds comments

This commit is contained in:
Janus C. H. Knudsen 2025-02-11 23:32:28 +01:00
parent f4f2fc47b1
commit e3340b7c98
5 changed files with 52 additions and 9 deletions

View file

@ -6,6 +6,14 @@ namespace Core.Configurations.JsonConfigProvider
{ {
public static class JsonConfigExtension public static class JsonConfigExtension
{ {
/// <summary>
/// Adds a JSON configuration source to the configuration builder.
/// </summary>
/// <param name="builder">The configuration builder to add to</param>
/// <param name="configurationFilePath">Path to the JSON configuration file. Defaults to "appconfiguration.json"</param>
/// <param name="optional">If true, the configuration file is optional. Defaults to true</param>
/// <param name="reloadOnChange">If true, the configuration will be reloaded when the file changes. Defaults to false</param>
/// <returns>The configuration builder</returns>
public static IConfigurationBuilder AddJsonFile(this IConfigurationBuilder builder, string configurationFilePath = "appconfiguration.json", bool? optional = true, bool? reloadOnChange = false) public static IConfigurationBuilder AddJsonFile(this IConfigurationBuilder builder, string configurationFilePath = "appconfiguration.json", bool? optional = true, bool? reloadOnChange = false)
{ {
return builder.AddProvider(new JsonConfigProvider(builder, configurationFilePath, optional ?? true, reloadOnChange ?? false)); return builder.AddProvider(new JsonConfigProvider(builder, configurationFilePath, optional ?? true, reloadOnChange ?? false));

View file

@ -1,18 +1,27 @@
namespace Core.Configurations.SmartConfig namespace Core.Configurations.SmartConfig
{ {
/// <summary>
/// Extension methods for adding smart configuration providers to IConfigurationBuilder.
/// </summary>
public static class SmartConfigExtension public static class SmartConfigExtension
{ {
/// <summary> /// <summary>
/// /// Adds a smart configuration provider using a connection string from appsettings.
/// </summary> /// </summary>
/// <param name="builder"></param> /// <param name="builder">The configuration builder to add to</param>
/// <param name="configKey">Name of key</param> /// <param name="configKey">The key to find the connection string in the ConnectionStrings section. Defaults to "DefaultConnection"</param>
/// <param name="path">If this is different than the default ConnectionStrings-element, then configure it.</param> /// <param name="path">Optional path to configuration file if different from default appsettings location</param>
/// <returns></returns> /// <returns>The configuration builder</returns>
public static IConfigurationBuilder AddSmartConfig(this IConfigurationBuilder builder, string configKey = "DefaultConnection", string path = null) public static IConfigurationBuilder AddSmartConfig(this IConfigurationBuilder builder, string configKey = "DefaultConnection", string path = null)
{ {
return builder.AddProvider(new SmartConfigProvider(builder, configKey, path)); return builder.AddProvider(new SmartConfigProvider(builder, configKey, path));
} }
/// <summary>
/// Adds a smart configuration provider with custom configuration options.
/// </summary>
/// <param name="builder">The configuration builder to add to</param>
/// <param name="setupAction">Action to configure the smart configuration options</param>
/// <returns>The configuration builder</returns>
public static IConfigurationBuilder AddSmartConfig(this IConfigurationBuilder builder, Action<SmartConfigOptions> setupAction) public static IConfigurationBuilder AddSmartConfig(this IConfigurationBuilder builder, Action<SmartConfigOptions> setupAction)
{ {
var options = new SmartConfigOptions(); var options = new SmartConfigOptions();

View file

@ -1,22 +1,39 @@
namespace Core.Configurations.SmartConfig namespace Core.Configurations.SmartConfig
{ {
/// <summary>
/// Configuration options for setting up smart configuration providers.
/// Provides fluent configuration methods for specifying the repository type and settings.
/// </summary>
public class SmartConfigOptions public class SmartConfigOptions
{ {
private SmartConfiguration.IConfigurationRepository _repository; private SmartConfiguration.IConfigurationRepository _repository;
internal string _configKey; internal string _configKey;
/// <summary>
/// Configures the smart configuration to use PostgreSQL as the configuration store.
/// </summary>
/// <param name="configKey">The configuration key used to find the connection string</param>
/// <returns>The configuration options instance for method chaining</returns>
public SmartConfigOptions UsePostgres(string configKey) public SmartConfigOptions UsePostgres(string configKey)
{ {
_configKey = configKey; _configKey = configKey;
_repository = new Configurations.SmartConfigProvider.Repositories.PostgresConfigurationRepository(); _repository = new Configurations.SmartConfigProvider.Repositories.PostgresConfigurationRepository();
return this; return this;
} }
/// <summary>
/// Configures the smart configuration to use SQL Server as the configuration store.
/// </summary>
/// <returns>The configuration options instance for method chaining</returns>
/// <exception cref="NotImplementedException">This feature is not yet implemented</exception>
public SmartConfigOptions UseSqlServer() public SmartConfigOptions UseSqlServer()
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }
/// <summary>
/// Configures the smart configuration to use a custom configuration repository.
/// </summary>
/// <param name="repository">The configuration repository to use</param>
/// <returns>The configuration options instance for method chaining</returns>
public SmartConfigOptions UseRepository(SmartConfiguration.IConfigurationRepository repository) public SmartConfigOptions UseRepository(SmartConfiguration.IConfigurationRepository repository)
{ {
_repository = repository; _repository = repository;

View file

@ -5,6 +5,16 @@ using Newtonsoft.Json.Linq;
namespace Core.Configurations.SmartConfig namespace Core.Configurations.SmartConfig
{ {
/// <summary>
/// Configuration provider that loads configuration from a smart configuration source (e.g. database).
/// The provider reads connection details from a JSON file and uses them to connect to a configuration repository.
/// </summary>
/// <remarks>
/// The provider supports multiple initialization methods:
/// - Through SmartConfigOptions for flexible repository configuration
/// - Through direct configuration key and file path
/// Configuration is loaded from the repository during Build() and converted to a JSON structure.
/// </remarks>
public class SmartConfigProvider : IConfigurationProvider public class SmartConfigProvider : IConfigurationProvider
{ {
string _configKey; string _configKey;

View file

@ -1,13 +1,11 @@
using Core.Configurations.SmartConfig; using Core.Configurations.SmartConfig;
using Core.Configurations; using Core.Configurations;
using FluentAssertions; using FluentAssertions;
using Newtonsoft.Json.Linq;
using Core.Configurations.JsonConfigProvider; using Core.Configurations.JsonConfigProvider;
using Autofac; using Autofac;
using System.Data; using System.Data;
using Insight.Database; using Insight.Database;
using Npgsql; using Npgsql;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace Tests.ConfigurationTests namespace Tests.ConfigurationTests
{ {
@ -26,6 +24,7 @@ namespace Tests.ConfigurationTests
var actualFeature = config.Get<bool>("Database:UseSSL"); var actualFeature = config.Get<bool>("Database:UseSSL");
} }
[TestMethod] [TestMethod]
public void Get_ShouldReturnCorrectValueAsBool() public void Get_ShouldReturnCorrectValueAsBool()
{ {