Files

88 lines
2.7 KiB
C#
Raw Permalink Normal View History

2026-04-04 13:30:13 +02:00
using Microsoft.Extensions.Configuration;
2026-04-05 08:14:33 +02:00
using System;
using System.IO;
2026-04-04 13:30:13 +02:00
2026-04-05 08:14:33 +02:00
namespace AvaloniaSerilogNoDI.Helpers;
2026-04-04 13:30:13 +02:00
public class AppSettings<TOption>
{
2026-04-05 08:00:09 +02:00
#region Constructors
2026-04-04 13:30:13 +02:00
public AppSettings(IConfigurationSection configSection, string? key = null)
{
2026-04-05 08:00:09 +02:00
ConfigSection = configSection;
2026-04-04 13:30:13 +02:00
// ReSharper disable once VirtualMemberCallInConstructor
GetValue(key);
}
#endregion
2026-04-05 08:00:09 +02:00
#region Properties
protected static AppSettings<TOption>? AppSetting { get; private set; }
2026-04-04 13:30:13 +02:00
// ReSharper disable once StaticMemberInGenericType
2026-04-05 08:00:09 +02:00
protected static IConfigurationSection? ConfigSection { get; private set; }
2026-04-04 13:30:13 +02:00
public TOption? Value { get; set; }
#endregion
2026-04-05 08:00:09 +02:00
#region Methods
#pragma warning disable CA1000 // Do not declare static members on generic types
2026-04-04 13:30:13 +02:00
public static TOption? Current(string section, string? key = null)
{
2026-04-05 08:00:09 +02:00
AppSetting = GetCurrentSettings(section, key);
return AppSetting.Value;
2026-04-04 13:30:13 +02:00
}
2026-04-05 08:00:09 +02:00
2026-04-04 13:30:13 +02:00
public static AppSettings<TOption> GetCurrentSettings(string section, string? key = null)
2026-04-05 08:00:09 +02:00
#pragma warning restore CA1000 // Do not declare static members on generic types
2026-04-04 13:30:13 +02:00
{
string env = Environment.GetEnvironmentVariable("DOTNET_ENVIRONMENT") ?? "Production";
IConfigurationBuilder builder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{env}.json", optional: true, reloadOnChange: true)
.AddEnvironmentVariables();
IConfigurationRoot configuration = builder.Build();
if (string.IsNullOrEmpty(section))
section = "AppSettings"; // default
AppSettings<TOption> settings = new AppSettings<TOption>(configuration.GetSection(section), key);
return settings;
}
protected virtual void GetValue(string? key)
{
if (key is null)
{
// no key, so must be a class/strut object
Value = Activator.CreateInstance<TOption>();
2026-04-05 08:00:09 +02:00
ConfigSection!.Bind(Value);
2026-04-04 13:30:13 +02:00
return;
}
Type optionType = typeof(TOption);
if ((optionType == typeof(string) ||
optionType == typeof(int) ||
optionType == typeof(long) ||
optionType == typeof(decimal) ||
optionType == typeof(float) ||
optionType == typeof(double))
2026-04-05 08:00:09 +02:00
&& ConfigSection != null)
2026-04-04 13:30:13 +02:00
{
// we must be retrieving a value
2026-04-05 08:00:09 +02:00
Value = ConfigSection.GetValue<TOption>(key);
2026-04-04 13:30:13 +02:00
return;
}
// Could not find a supported type
throw new InvalidCastException($"Type {typeof(TOption).Name} is invalid");
}
#endregion
}