initial commit

This commit is contained in:
Matthias Heil
2026-04-04 13:30:13 +02:00
commit 6bed9b284c
186 changed files with 10650 additions and 0 deletions
@@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="7.0.1" />
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="7.0.0" />
</ItemGroup>
</Project>
@@ -0,0 +1,17 @@
using Microsoft.Extensions.Configuration;
namespace Common.Core.Extensions;
public static class ConfigurationExtension
{
public static IConfigurationBuilder Initialize(this IConfigurationBuilder builder)
{
string env = Environment.GetEnvironmentVariable("DOTNET_ENVIRONMENT") ?? "Production";
return builder
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{env}.json", optional: true, reloadOnChange: true)
.AddEnvironmentVariables();
}
}
@@ -0,0 +1,18 @@
namespace Common.Core.Extensions;
public static class ServicesExtension
{
public static TModel? TryGetService<TModel>(this IServiceProvider serviceProvider) where TModel : class
{
try
{
return (TModel?)serviceProvider.GetService(typeof(TModel));
}
catch (ObjectDisposedException)
{
// ignore as we do not care...
}
return default;
}
}
@@ -0,0 +1,92 @@
using Microsoft.Extensions.Configuration;
namespace Common.Core;
public class AppSettings<TOption>
{
#region Constructors
public AppSettings(IConfigurationSection configSection, string? key = null)
{
_configSection = configSection;
// ReSharper disable once VirtualMemberCallInConstructor
GetValue(key);
}
#endregion
#region Fields
protected static AppSettings<TOption>? _appSetting;
// ReSharper disable once StaticMemberInGenericType
protected static IConfigurationSection? _configSection;
#endregion
#region Properties
public TOption? Value { get; set; }
#endregion
#region Methods
public static TOption? Current(string section, string? key = null)
{
_appSetting = GetCurrentSettings(section, key);
return _appSetting.Value;
}
public static AppSettings<TOption> GetCurrentSettings(string section, string? key = null)
{
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>();
_configSection!.Bind(Value);
return;
}
Type optionType = typeof(TOption);
if ((optionType == typeof(string) ||
optionType == typeof(int) ||
optionType == typeof(long) ||
optionType == typeof(decimal) ||
optionType == typeof(float) ||
optionType == typeof(double))
&& _configSection != null)
{
// we must be retrieving a value
Value = _configSection.GetValue<TOption>(key);
return;
}
// Could not find a supported type
throw new InvalidCastException($"Type {typeof(TOption).Name} is invalid");
}
#endregion
}