WIP on Config Handling
This commit is contained in:
parent
4dc03f2cbf
commit
8e6492e979
17 changed files with 314 additions and 288 deletions
|
|
@ -1,14 +0,0 @@
|
||||||
namespace Configuration.Core;
|
|
||||||
public class AppConfiguration
|
|
||||||
{
|
|
||||||
public long Id { get; set; }
|
|
||||||
public string Key { get; set; }
|
|
||||||
public string Value { get; set; }
|
|
||||||
public string Label { get; set; }
|
|
||||||
public string ContentType { get; set; }
|
|
||||||
public DateTime? ValidFrom { get; set; }
|
|
||||||
public DateTime? ExpiresAt { get; set; }
|
|
||||||
public DateTime? CreatedAt { get; set; }
|
|
||||||
public DateTime? ModifiedAt { get; set; }
|
|
||||||
public Guid? Etag { get; set; }
|
|
||||||
}
|
|
||||||
|
|
@ -1,28 +0,0 @@
|
||||||
using Configuration.Core;
|
|
||||||
using Microsoft.Extensions.Configuration;
|
|
||||||
|
|
||||||
namespace Core.Configurations.ConfigurationManager
|
|
||||||
{
|
|
||||||
public static class ConfigurationExtensions
|
|
||||||
{
|
|
||||||
public static T Get<T>(this IConfigurationSection section) where T : class
|
|
||||||
{
|
|
||||||
if (section is JsonConfigurationSection jsonSection)
|
|
||||||
{
|
|
||||||
var token = jsonSection.GetToken();
|
|
||||||
return token?.ToObject<T>();
|
|
||||||
}
|
|
||||||
throw new InvalidOperationException("Section is not a JsonConfigurationSection");
|
|
||||||
}
|
|
||||||
|
|
||||||
public static T GetValue<T>(this IConfigurationSection section, string key)
|
|
||||||
{
|
|
||||||
if (section is JsonConfigurationSection jsonSection)
|
|
||||||
{
|
|
||||||
var token = jsonSection.GetToken().SelectToken(key.Replace(":", "."));
|
|
||||||
return token.ToObject<T>();
|
|
||||||
}
|
|
||||||
throw new InvalidOperationException("Section is not a JsonConfigurationSection");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,24 +0,0 @@
|
||||||
using System.Data;
|
|
||||||
using Insight.Database;
|
|
||||||
|
|
||||||
namespace Configuration.Core;
|
|
||||||
public class ConfigurationRepository : IConfigurationRepository
|
|
||||||
{
|
|
||||||
private readonly IDbConnection _connection;
|
|
||||||
|
|
||||||
public ConfigurationRepository(IDbConnection connection)
|
|
||||||
{
|
|
||||||
_connection = connection;
|
|
||||||
}
|
|
||||||
|
|
||||||
public async Task<IEnumerable<AppConfiguration>> GetActiveConfigurations()
|
|
||||||
{
|
|
||||||
const string sql = @"
|
|
||||||
SELECT id, key, value, label, content_type, valid_from, expires_at, created_at, modified_at, etag
|
|
||||||
FROM prod.app_configuration
|
|
||||||
WHERE (expires_at IS NULL OR expires_at <= CURRENT_TIMESTAMP)
|
|
||||||
AND (valid_from IS NULL OR valid_from >= CURRENT_TIMESTAMP)";
|
|
||||||
|
|
||||||
return await _connection.QueryAsync<AppConfiguration>(sql);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,5 +0,0 @@
|
||||||
namespace Configuration.Core;
|
|
||||||
public interface IConfigurationRepository
|
|
||||||
{
|
|
||||||
Task<IEnumerable<AppConfiguration>> GetActiveConfigurations();
|
|
||||||
}
|
|
||||||
|
|
@ -1,31 +0,0 @@
|
||||||
using Newtonsoft.Json.Linq;
|
|
||||||
using Microsoft.Extensions.Configuration;
|
|
||||||
using Microsoft.Extensions.Primitives;
|
|
||||||
using System.Data;
|
|
||||||
|
|
||||||
namespace Configuration.Core;
|
|
||||||
public class JsonConfiguration : IConfiguration
|
|
||||||
{
|
|
||||||
private readonly JObject _data;
|
|
||||||
private readonly IChangeToken _changeToken;
|
|
||||||
|
|
||||||
public JsonConfiguration(JObject data, IChangeToken changeToken)
|
|
||||||
{
|
|
||||||
_data = data;
|
|
||||||
_changeToken = changeToken;
|
|
||||||
}
|
|
||||||
|
|
||||||
public string this[string key]
|
|
||||||
{
|
|
||||||
get => _data.SelectToken(key.Replace(":", "."))?.ToString();
|
|
||||||
set => throw new NotImplementedException();
|
|
||||||
}
|
|
||||||
|
|
||||||
public IConfigurationSection GetSection(string key) =>
|
|
||||||
new JsonConfigurationSection(_data, key);
|
|
||||||
|
|
||||||
public IEnumerable<IConfigurationSection> GetChildren() =>
|
|
||||||
_data.Properties().Select(p => new JsonConfigurationSection(_data, p.Name));
|
|
||||||
|
|
||||||
public IChangeToken GetReloadToken() => _changeToken;
|
|
||||||
}
|
|
||||||
|
|
@ -1,80 +0,0 @@
|
||||||
using Newtonsoft.Json.Linq;
|
|
||||||
using Microsoft.Extensions.Configuration;
|
|
||||||
using Microsoft.Extensions.Primitives;
|
|
||||||
|
|
||||||
namespace Configuration.Core
|
|
||||||
{
|
|
||||||
public class JsonConfigurationSection : IConfigurationSection
|
|
||||||
{
|
|
||||||
private readonly JObject _data;
|
|
||||||
private readonly string _path;
|
|
||||||
private readonly string _normalizedPath;
|
|
||||||
|
|
||||||
public JsonConfigurationSection(JObject data, string path)
|
|
||||||
{
|
|
||||||
_data = data ?? throw new ArgumentNullException(nameof(data));
|
|
||||||
_path = path ?? throw new ArgumentNullException(nameof(path));
|
|
||||||
_normalizedPath = NormalizePath(_path);
|
|
||||||
}
|
|
||||||
|
|
||||||
public string this[string key]
|
|
||||||
{
|
|
||||||
get
|
|
||||||
{
|
|
||||||
if (string.IsNullOrEmpty(key))
|
|
||||||
throw new ArgumentNullException(nameof(key));
|
|
||||||
|
|
||||||
var token = _data.SelectToken($"{_normalizedPath}.{NormalizePath(key)}");
|
|
||||||
return token?.ToString();
|
|
||||||
}
|
|
||||||
set => throw new NotImplementedException("Setting values is not supported.");
|
|
||||||
}
|
|
||||||
|
|
||||||
public string Key => _path.Split(':').Last();
|
|
||||||
public string Path => _path;
|
|
||||||
|
|
||||||
public string Value
|
|
||||||
{
|
|
||||||
get
|
|
||||||
{
|
|
||||||
var token = _data.SelectToken(_normalizedPath);
|
|
||||||
return token?.ToString();
|
|
||||||
}
|
|
||||||
set => throw new NotImplementedException("Setting values is not supported.");
|
|
||||||
}
|
|
||||||
|
|
||||||
public IConfigurationSection GetSection(string key)
|
|
||||||
{
|
|
||||||
if (string.IsNullOrEmpty(key))
|
|
||||||
throw new ArgumentNullException(nameof(key));
|
|
||||||
|
|
||||||
return new JsonConfigurationSection(_data, string.IsNullOrEmpty(_path) ? key : $"{_path}:{key}");
|
|
||||||
}
|
|
||||||
|
|
||||||
public JToken GetToken() => _data.SelectToken(_normalizedPath);
|
|
||||||
|
|
||||||
public IEnumerable<IConfigurationSection> GetChildren()
|
|
||||||
{
|
|
||||||
var token = _data.SelectToken(_normalizedPath);
|
|
||||||
if (token is JObject obj)
|
|
||||||
{
|
|
||||||
return obj.Properties()
|
|
||||||
.Select(p => new JsonConfigurationSection(_data, string.IsNullOrEmpty(_path) ? p.Name : $"{_path}:{p.Name}"));
|
|
||||||
}
|
|
||||||
return Enumerable.Empty<IConfigurationSection>();
|
|
||||||
}
|
|
||||||
|
|
||||||
public T Get<T>() where T : class
|
|
||||||
{
|
|
||||||
var token = _data.SelectToken(_normalizedPath);
|
|
||||||
return token?.ToObject<T>();
|
|
||||||
}
|
|
||||||
|
|
||||||
public IChangeToken GetReloadToken() => new ConfigurationReloadToken();
|
|
||||||
|
|
||||||
private static string NormalizePath(string path)
|
|
||||||
{
|
|
||||||
return path?.Replace(":", ".", StringComparison.Ordinal) ?? string.Empty;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,103 +0,0 @@
|
||||||
using Newtonsoft.Json;
|
|
||||||
using Newtonsoft.Json.Linq;
|
|
||||||
using Microsoft.Extensions.Configuration;
|
|
||||||
|
|
||||||
namespace Configuration.Core
|
|
||||||
{
|
|
||||||
public class KeyValueConfigurationBuilder
|
|
||||||
{
|
|
||||||
private readonly IConfigurationRepository _repository;
|
|
||||||
private readonly JObject _rootObject = new();
|
|
||||||
private ConfigurationReloadToken _reloadToken = new();
|
|
||||||
private IConfiguration _configuration;
|
|
||||||
private readonly object _configurationLock = new();
|
|
||||||
|
|
||||||
public KeyValueConfigurationBuilder(IConfigurationRepository repository)
|
|
||||||
{
|
|
||||||
_repository = repository ?? throw new ArgumentNullException(nameof(repository));
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Loads configurations from the repository and builds the configuration tree.
|
|
||||||
/// </summary>
|
|
||||||
public async Task LoadConfiguration()
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
var configurations = await _repository.GetActiveConfigurations();
|
|
||||||
foreach (var config in configurations)
|
|
||||||
{
|
|
||||||
AddKeyValue(config.Key, config.Value);
|
|
||||||
}
|
|
||||||
OnReload();
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
// Log the exception or handle it as needed
|
|
||||||
throw new InvalidOperationException("Failed to load configurations.", ex);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Adds a key-value pair to the configuration tree.
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="key">The key to add.</param>
|
|
||||||
/// <param name="jsonValue">The JSON value to add.</param>
|
|
||||||
public void AddKeyValue(string key, string jsonValue)
|
|
||||||
{
|
|
||||||
if (string.IsNullOrEmpty(key))
|
|
||||||
throw new ArgumentNullException(nameof(key));
|
|
||||||
if (string.IsNullOrEmpty(jsonValue))
|
|
||||||
throw new ArgumentNullException(nameof(jsonValue));
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
var valueObject = JsonConvert.DeserializeObject<JObject>(jsonValue);
|
|
||||||
var parts = key.Split(':');
|
|
||||||
|
|
||||||
JObject current = _rootObject;
|
|
||||||
for (int i = 0; i < parts.Length - 1; i++)
|
|
||||||
{
|
|
||||||
var part = parts[i];
|
|
||||||
if (!current.ContainsKey(part))
|
|
||||||
{
|
|
||||||
current[part] = new JObject();
|
|
||||||
}
|
|
||||||
current = (JObject)current[part];
|
|
||||||
}
|
|
||||||
|
|
||||||
current[parts[^1]] = valueObject;
|
|
||||||
}
|
|
||||||
catch (JsonException ex)
|
|
||||||
{
|
|
||||||
throw new ArgumentException("Invalid JSON value.", nameof(jsonValue), ex);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void OnReload()
|
|
||||||
{
|
|
||||||
var previousToken = Interlocked.Exchange(ref _reloadToken, new ConfigurationReloadToken());
|
|
||||||
previousToken.OnReload();
|
|
||||||
_configuration = null; // Reset the configuration to force a rebuild
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Builds the configuration instance.
|
|
||||||
/// </summary>
|
|
||||||
/// <returns>The built <see cref="IConfiguration"/> instance.</returns>
|
|
||||||
public IConfiguration Build()
|
|
||||||
{
|
|
||||||
if (_configuration == null)
|
|
||||||
{
|
|
||||||
lock (_configurationLock)
|
|
||||||
{
|
|
||||||
if (_configuration == null)
|
|
||||||
{
|
|
||||||
_configuration = new JsonConfiguration(_rootObject, _reloadToken);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return _configuration;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
14
Core/Configurations/SmartConfiguration/AppConfiguration.cs
Normal file
14
Core/Configurations/SmartConfiguration/AppConfiguration.cs
Normal file
|
|
@ -0,0 +1,14 @@
|
||||||
|
namespace Core.Configurations.SmartConfiguration;
|
||||||
|
public class AppConfiguration
|
||||||
|
{
|
||||||
|
public long Id { get; set; }
|
||||||
|
public string Key { get; set; }
|
||||||
|
public string Value { get; set; }
|
||||||
|
public string Label { get; set; }
|
||||||
|
public string ContentType { get; set; }
|
||||||
|
public DateTime? ValidFrom { get; set; }
|
||||||
|
public DateTime? ExpiresAt { get; set; }
|
||||||
|
public DateTime? CreatedAt { get; set; }
|
||||||
|
public DateTime? ModifiedAt { get; set; }
|
||||||
|
public Guid? Etag { get; set; }
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,27 @@
|
||||||
|
using Microsoft.Extensions.Configuration;
|
||||||
|
|
||||||
|
namespace Core.Configurations.SmartConfiguration
|
||||||
|
{
|
||||||
|
public static class ConfigurationExtensions
|
||||||
|
{
|
||||||
|
public static T Get<T>(this IConfigurationSection section) where T : class
|
||||||
|
{
|
||||||
|
if (section is JsonConfigurationSection jsonSection)
|
||||||
|
{
|
||||||
|
var token = jsonSection.GetToken();
|
||||||
|
return token?.ToObject<T>();
|
||||||
|
}
|
||||||
|
throw new InvalidOperationException("Section is not a JsonConfigurationSection");
|
||||||
|
}
|
||||||
|
|
||||||
|
public static T GetValue<T>(this IConfigurationSection section, string key)
|
||||||
|
{
|
||||||
|
if (section is JsonConfigurationSection jsonSection)
|
||||||
|
{
|
||||||
|
var token = jsonSection.GetToken().SelectToken(key.Replace(":", "."));
|
||||||
|
return token.ToObject<T>();
|
||||||
|
}
|
||||||
|
throw new InvalidOperationException("Section is not a JsonConfigurationSection");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,24 @@
|
||||||
|
using System.Data;
|
||||||
|
using Insight.Database;
|
||||||
|
|
||||||
|
namespace Core.Configurations.SmartConfiguration;
|
||||||
|
public class ConfigurationRepository : IConfigurationRepository
|
||||||
|
{
|
||||||
|
private readonly IDbConnection _connection;
|
||||||
|
|
||||||
|
public ConfigurationRepository(IDbConnection connection)
|
||||||
|
{
|
||||||
|
_connection = connection;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<IEnumerable<AppConfiguration>> GetActiveConfigurations()
|
||||||
|
{
|
||||||
|
const string sql = @"
|
||||||
|
SELECT id, key, value, label, content_type, valid_from, expires_at, created_at, modified_at, etag
|
||||||
|
FROM prod.app_configuration
|
||||||
|
WHERE (expires_at IS NULL OR expires_at > CURRENT_TIMESTAMP)
|
||||||
|
AND (valid_from IS NULL OR valid_from < CURRENT_TIMESTAMP)";
|
||||||
|
|
||||||
|
return await _connection.QueryAsync<AppConfiguration>(sql);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,5 @@
|
||||||
|
namespace Core.Configurations.SmartConfiguration;
|
||||||
|
public interface IConfigurationRepository
|
||||||
|
{
|
||||||
|
Task<IEnumerable<AppConfiguration>> GetActiveConfigurations();
|
||||||
|
}
|
||||||
29
Core/Configurations/SmartConfiguration/JsonConfiguration.cs
Normal file
29
Core/Configurations/SmartConfiguration/JsonConfiguration.cs
Normal file
|
|
@ -0,0 +1,29 @@
|
||||||
|
using Newtonsoft.Json.Linq;
|
||||||
|
using Microsoft.Extensions.Configuration;
|
||||||
|
using Microsoft.Extensions.Primitives;
|
||||||
|
using System.Data;
|
||||||
|
|
||||||
|
namespace Core.Configurations.SmartConfiguration;
|
||||||
|
public class JsonConfiguration : IConfiguration
|
||||||
|
{
|
||||||
|
private readonly JObject _data;
|
||||||
|
|
||||||
|
public JsonConfiguration(JObject data)
|
||||||
|
{
|
||||||
|
_data = data;
|
||||||
|
}
|
||||||
|
|
||||||
|
public string this[string key]
|
||||||
|
{
|
||||||
|
get => _data.SelectToken(key.Replace(":", "."))?.ToString();
|
||||||
|
set => throw new NotImplementedException();
|
||||||
|
}
|
||||||
|
|
||||||
|
public IConfigurationSection GetSection(string key) =>
|
||||||
|
new JsonConfigurationSection(_data, key);
|
||||||
|
|
||||||
|
public IEnumerable<IConfigurationSection> GetChildren() =>
|
||||||
|
_data.Properties().Select(p => new JsonConfigurationSection(_data, p.Name));
|
||||||
|
|
||||||
|
public IChangeToken GetReloadToken() => throw new NotImplementedException();
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,80 @@
|
||||||
|
using Newtonsoft.Json.Linq;
|
||||||
|
using Microsoft.Extensions.Configuration;
|
||||||
|
using Microsoft.Extensions.Primitives;
|
||||||
|
|
||||||
|
namespace Core.Configurations.SmartConfiguration
|
||||||
|
{
|
||||||
|
public class JsonConfigurationSection : IConfigurationSection
|
||||||
|
{
|
||||||
|
private readonly JObject _data;
|
||||||
|
private readonly string _path;
|
||||||
|
private readonly string _normalizedPath;
|
||||||
|
|
||||||
|
public JsonConfigurationSection(JObject data, string path)
|
||||||
|
{
|
||||||
|
_data = data ?? throw new ArgumentNullException(nameof(data));
|
||||||
|
_path = path ?? throw new ArgumentNullException(nameof(path));
|
||||||
|
_normalizedPath = NormalizePath(_path);
|
||||||
|
}
|
||||||
|
|
||||||
|
public string this[string key]
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
if (string.IsNullOrEmpty(key))
|
||||||
|
throw new ArgumentNullException(nameof(key));
|
||||||
|
|
||||||
|
var token = _data.SelectToken($"{_normalizedPath}.{NormalizePath(key)}");
|
||||||
|
return token?.ToString();
|
||||||
|
}
|
||||||
|
set => throw new NotImplementedException("Setting values is not supported.");
|
||||||
|
}
|
||||||
|
|
||||||
|
public string Key => _path.Split(':').Last();
|
||||||
|
public string Path => _path;
|
||||||
|
|
||||||
|
public string Value
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
var token = _data.SelectToken(_normalizedPath);
|
||||||
|
return token?.ToString();
|
||||||
|
}
|
||||||
|
set => throw new NotImplementedException("Setting values is not supported.");
|
||||||
|
}
|
||||||
|
|
||||||
|
public IConfigurationSection GetSection(string key)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrEmpty(key))
|
||||||
|
throw new ArgumentNullException(nameof(key));
|
||||||
|
|
||||||
|
return new JsonConfigurationSection(_data, string.IsNullOrEmpty(_path) ? key : $"{_path}:{key}");
|
||||||
|
}
|
||||||
|
|
||||||
|
public JToken GetToken() => _data.SelectToken(_normalizedPath);
|
||||||
|
|
||||||
|
public IEnumerable<IConfigurationSection> GetChildren()
|
||||||
|
{
|
||||||
|
var token = _data.SelectToken(_normalizedPath);
|
||||||
|
if (token is JObject obj)
|
||||||
|
{
|
||||||
|
return obj.Properties()
|
||||||
|
.Select(p => new JsonConfigurationSection(_data, string.IsNullOrEmpty(_path) ? p.Name : $"{_path}:{p.Name}"));
|
||||||
|
}
|
||||||
|
return Enumerable.Empty<IConfigurationSection>();
|
||||||
|
}
|
||||||
|
|
||||||
|
public T Get<T>() where T : class
|
||||||
|
{
|
||||||
|
var token = _data.SelectToken(_normalizedPath);
|
||||||
|
return token?.ToObject<T>();
|
||||||
|
}
|
||||||
|
|
||||||
|
public IChangeToken GetReloadToken() => new ConfigurationReloadToken();
|
||||||
|
|
||||||
|
private static string NormalizePath(string path)
|
||||||
|
{
|
||||||
|
return path?.Replace(":", ".", StringComparison.Ordinal) ?? string.Empty;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,83 @@
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
using Newtonsoft.Json.Linq;
|
||||||
|
using Microsoft.Extensions.Configuration;
|
||||||
|
|
||||||
|
namespace Core.Configurations.SmartConfiguration
|
||||||
|
{
|
||||||
|
public class KeyValueConfigurationBuilder
|
||||||
|
{
|
||||||
|
private readonly IConfigurationRepository _repository;
|
||||||
|
private readonly JObject _rootObject = new();
|
||||||
|
private IConfiguration _configuration;
|
||||||
|
private readonly object _configurationLock = new();
|
||||||
|
|
||||||
|
public KeyValueConfigurationBuilder(IConfigurationRepository repository)
|
||||||
|
{
|
||||||
|
_repository = repository ?? throw new ArgumentNullException(nameof(repository));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Loads configurations from the repository and builds the configuration tree.
|
||||||
|
/// </summary>
|
||||||
|
public async Task LoadConfiguration()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var configurations = await _repository.GetActiveConfigurations();
|
||||||
|
foreach (var config in configurations)
|
||||||
|
AddKeyValue(config.Key, config.Value);
|
||||||
|
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
// Log the exception or handle it as needed
|
||||||
|
throw new InvalidOperationException("Failed to load configurations.", ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Adds a key-value pair to the configuration tree.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="key">The key to add.</param>
|
||||||
|
/// <param name="jsonValue">The JSON value to add.</param>
|
||||||
|
public void AddKeyValue(string key, string jsonValue)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrEmpty(key))
|
||||||
|
throw new ArgumentNullException(nameof(key));
|
||||||
|
if (string.IsNullOrEmpty(jsonValue))
|
||||||
|
throw new ArgumentNullException(nameof(jsonValue));
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var valueObject = JsonConvert.DeserializeObject<JObject>(jsonValue);
|
||||||
|
var parts = key.Split(':');
|
||||||
|
|
||||||
|
JObject current = _rootObject;
|
||||||
|
for (int i = 0; i < parts.Length - 1; i++)
|
||||||
|
{
|
||||||
|
var part = parts[i];
|
||||||
|
if (!current.ContainsKey(part))
|
||||||
|
{
|
||||||
|
current[part] = new JObject();
|
||||||
|
}
|
||||||
|
current = (JObject)current[part];
|
||||||
|
}
|
||||||
|
|
||||||
|
current[parts[^1]] = valueObject;
|
||||||
|
}
|
||||||
|
catch (JsonException ex)
|
||||||
|
{
|
||||||
|
throw new ArgumentException("Invalid JSON value.", nameof(jsonValue), ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/// <summary>
|
||||||
|
/// Builds the configuration instance.
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>The built <see cref="IConfiguration"/> instance.</returns>
|
||||||
|
public IConfiguration Build()
|
||||||
|
{
|
||||||
|
_configuration = new JsonConfiguration(_rootObject);
|
||||||
|
return _configuration;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
48
Core/Configurations/SmartConfiguration/SmartConfigManager.cs
Normal file
48
Core/Configurations/SmartConfiguration/SmartConfigManager.cs
Normal file
|
|
@ -0,0 +1,48 @@
|
||||||
|
|
||||||
|
using Microsoft.Extensions.Configuration;
|
||||||
|
|
||||||
|
namespace Core.Configurations
|
||||||
|
{
|
||||||
|
public class SmartConfigManager
|
||||||
|
{
|
||||||
|
private static IConfigurationBuilder _configurationBuilder;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// This AppConfigBuilder assumes that AppConfigEndpoint and AppConfigLabelFilter are configured as Settings on Azure for the Application that needs them.
|
||||||
|
/// AppConfigEndpoint would look like this: Endpoint=https://config-dec-test.azconfig.io;Id=0-l9-s0:foo;Secret=somesecret/bar
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="localSettingsFile">
|
||||||
|
/// Path relative to the base path stored in Microsoft.Extensions.Configuration.IConfigurationBuilder.Properties of builder.
|
||||||
|
/// </param>
|
||||||
|
public static IConfigurationBuilder AddSmartConfigBuilder(string localSettingsFile)
|
||||||
|
{
|
||||||
|
if (_configurationBuilder == null)
|
||||||
|
{
|
||||||
|
var envConfiguration = new ConfigurationBuilder()
|
||||||
|
.AddEnvironmentVariables()
|
||||||
|
.Build();
|
||||||
|
|
||||||
|
var appConfigEndpoint = envConfiguration["AppConfigEndpoint"];
|
||||||
|
var appConfigLabel = envConfiguration["AppConfigLabelFilter"];
|
||||||
|
|
||||||
|
_configurationBuilder = new ConfigurationBuilder();
|
||||||
|
if (!string.IsNullOrEmpty(appConfigEndpoint))
|
||||||
|
{
|
||||||
|
_configurationBuilder
|
||||||
|
.AddAzureAppConfiguration(options =>
|
||||||
|
{
|
||||||
|
options.Connect(appConfigEndpoint);
|
||||||
|
options.Select(keyFilter: "*", labelFilter: appConfigLabel);
|
||||||
|
})
|
||||||
|
.AddEnvironmentVariables();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
_configurationBuilder.SetBasePath(Directory.GetCurrentDirectory());
|
||||||
|
_configurationBuilder.AddJsonFile(localSettingsFile, optional: false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return _configurationBuilder;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -5,7 +5,8 @@ using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Tests;
|
using Tests;
|
||||||
using Core.Configurations.ConfigurationManager;
|
using Microsoft.Extensions.Configuration;
|
||||||
|
using Core.Configurations.SmartConfiguration;
|
||||||
|
|
||||||
namespace Configuration.Core.Tests;
|
namespace Configuration.Core.Tests;
|
||||||
|
|
||||||
|
|
@ -141,7 +142,7 @@ public class ConfigurationTests : TestFixture
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
var configuration = new JsonConfiguration(configData, new Microsoft.Extensions.Configuration.ConfigurationReloadToken());
|
IConfiguration configuration = new JsonConfiguration(configData, new Microsoft.Extensions.Configuration.ConfigurationReloadToken());
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
var welcomeConfig = configuration.GetSection("Email:Templates:Welcome").Get<WelcomeEmailConfig>();
|
var welcomeConfig = configuration.GetSection("Email:Templates:Welcome").Get<WelcomeEmailConfig>();
|
||||||
|
|
|
||||||
|
|
@ -33,7 +33,7 @@ namespace Tests
|
||||||
public virtual IConfigurationRoot Configuration()
|
public virtual IConfigurationRoot Configuration()
|
||||||
{
|
{
|
||||||
|
|
||||||
IConfigurationBuilder configBuilder = Core.Configurations.AzureConfigurationManager.AppConfigBuilder("appsettings.dev.json");
|
IConfigurationBuilder configBuilder = Core.Configurations.SmartConfigManager.AppConfigBuilder("appsettings.dev.json");
|
||||||
IConfigurationRoot configuration = configBuilder.Build();
|
IConfigurationRoot configuration = configBuilder.Build();
|
||||||
|
|
||||||
return configuration;
|
return configuration;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue