Rip out Microsoft's configuration and replace because it simply doesn't support polymorphism.... we are getting advanced boys!!!
This commit is contained in:
+1
-1
@@ -1,5 +1,5 @@
|
||||
namespace Hyperbar;
|
||||
|
||||
public record KeyAcceleratorCommand(VirtualKey Key,
|
||||
public record KeyAcceleratorRequest(VirtualKey Key,
|
||||
VirtualKey[]? Modifiers = null) :
|
||||
IRequest;
|
||||
@@ -0,0 +1,3 @@
|
||||
namespace Hyperbar;
|
||||
|
||||
public record ProcessRequest(string Process) : IRequest;
|
||||
@@ -4,7 +4,62 @@ using System.Text.Json.Serialization;
|
||||
|
||||
namespace Hyperbar;
|
||||
|
||||
public class ConfigurationWriter<TConfiguration>(string path,
|
||||
public interface IConfigurationReader<TConfiguration>
|
||||
where TConfiguration :
|
||||
class, new()
|
||||
{
|
||||
TConfiguration Read();
|
||||
}
|
||||
|
||||
public class ConfigurationReader<TConfiguration>(string path,
|
||||
string section,
|
||||
JsonSerializerOptions? serializerOptions = null) :
|
||||
IConfigurationReader<TConfiguration>
|
||||
where TConfiguration :
|
||||
class, new()
|
||||
{
|
||||
private static readonly Func<JsonSerializerOptions> defaultSerializerOptions = new(() =>
|
||||
{
|
||||
return new JsonSerializerOptions
|
||||
{
|
||||
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping,
|
||||
Converters =
|
||||
{
|
||||
new JsonStringEnumConverter()
|
||||
}
|
||||
};
|
||||
});
|
||||
|
||||
public TConfiguration Read()
|
||||
{
|
||||
if ((TryGet(out TConfiguration? value) ? value : new TConfiguration()) is TConfiguration configuration)
|
||||
{
|
||||
return configuration;
|
||||
}
|
||||
|
||||
return new TConfiguration();
|
||||
}
|
||||
|
||||
private bool TryGet(out TConfiguration? value)
|
||||
{
|
||||
if (File.Exists(path))
|
||||
{
|
||||
byte[] jsonContent = File.ReadAllBytes(path);
|
||||
|
||||
using JsonDocument jsonDocument = JsonDocument.Parse(jsonContent);
|
||||
if (jsonDocument.RootElement.TryGetProperty(section, out JsonElement sectionValue))
|
||||
{
|
||||
value = JsonSerializer.Deserialize<TConfiguration>(sectionValue.ToString(), serializerOptions ?? defaultSerializerOptions());
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
value = default;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public class ConfigurationWriter<TConfiguration>(string path,
|
||||
string section,
|
||||
JsonSerializerOptions? serializerOptions = null) :
|
||||
IConfigurationWriter<TConfiguration>
|
||||
|
||||
@@ -3,15 +3,113 @@ using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.DependencyInjection.Extensions;
|
||||
using Microsoft.Extensions.FileProviders;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Options;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace Hyperbar;
|
||||
|
||||
public static class IServiceCollectionExtensions
|
||||
{
|
||||
public static IServiceCollection AddHandler<THandler>(this IServiceCollection services,
|
||||
ServiceLifetime lifetime = ServiceLifetime.Transient)
|
||||
public static IServiceCollection AddConfiguration<TConfiguration>(this IServiceCollection services)
|
||||
where TConfiguration :
|
||||
class, new()
|
||||
{
|
||||
return services.AddConfiguration<TConfiguration>(typeof(TConfiguration).Name, "Settings.json", null);
|
||||
}
|
||||
|
||||
public static IServiceCollection AddConfiguration<TConfiguration>(this IServiceCollection services,
|
||||
IConfiguration configuration,
|
||||
TConfiguration? defaults = null)
|
||||
where TConfiguration :
|
||||
class, new()
|
||||
{
|
||||
return services.AddConfiguration(typeof(TConfiguration).Name, "Settings.json", defaults);
|
||||
}
|
||||
|
||||
public static IServiceCollection AddConfiguration<TConfiguration>(this IServiceCollection services,
|
||||
string section,
|
||||
string path = "Settings.json",
|
||||
TConfiguration? defaults = null,
|
||||
Action<JsonSerializerOptions>? serializerDelegate = null)
|
||||
where TConfiguration :
|
||||
class, new()
|
||||
{
|
||||
services.AddTransient(provider => provider.GetRequiredService<IConfigurationReader<TConfiguration>>().Read());
|
||||
|
||||
services.AddSingleton<IConfigurationReader<TConfiguration>>(provider =>
|
||||
{
|
||||
string? jsonFilePath = null;
|
||||
if (provider.GetService<IHostEnvironment>() is IHostEnvironment hostEnvironment)
|
||||
{
|
||||
IFileProvider fileProvider = hostEnvironment.ContentRootFileProvider;
|
||||
IFileInfo fileInfo = fileProvider.GetFileInfo(path);
|
||||
|
||||
jsonFilePath = fileInfo.PhysicalPath;
|
||||
}
|
||||
|
||||
jsonFilePath ??= Path.Combine(AppDomain.CurrentDomain.BaseDirectory, path);
|
||||
|
||||
JsonSerializerOptions? defaultSerializer = null;
|
||||
if (serializerDelegate is not null)
|
||||
{
|
||||
defaultSerializer = new JsonSerializerOptions();
|
||||
serializerDelegate.Invoke(defaultSerializer);
|
||||
}
|
||||
|
||||
return new ConfigurationReader<TConfiguration>(jsonFilePath, section, defaultSerializer);
|
||||
});
|
||||
|
||||
services.AddSingleton<IConfigurationWriter<TConfiguration>>(provider =>
|
||||
{
|
||||
string? jsonFilePath = null;
|
||||
if (provider.GetService<IHostEnvironment>() is IHostEnvironment hostEnvironment)
|
||||
{
|
||||
IFileProvider fileProvider = hostEnvironment.ContentRootFileProvider;
|
||||
IFileInfo fileInfo = fileProvider.GetFileInfo(path);
|
||||
|
||||
jsonFilePath = fileInfo.PhysicalPath;
|
||||
}
|
||||
|
||||
jsonFilePath ??= Path.Combine(AppDomain.CurrentDomain.BaseDirectory, path);
|
||||
|
||||
JsonSerializerOptions? defaultSerializer = null;
|
||||
if (serializerDelegate is not null)
|
||||
{
|
||||
defaultSerializer = new JsonSerializerOptions();
|
||||
serializerDelegate.Invoke(defaultSerializer);
|
||||
}
|
||||
|
||||
return new ConfigurationWriter<TConfiguration>(jsonFilePath, section, defaultSerializer);
|
||||
});
|
||||
|
||||
services.AddTransient(provider => new DefaultConfiguration<TConfiguration>(defaults));
|
||||
services.AddTransient<IInitializer, ConfigurationInitializer<TConfiguration>>();
|
||||
|
||||
services.AddTransient<IWritableConfiguration<TConfiguration>, WritableConfiguration<TConfiguration>>();
|
||||
|
||||
return services;
|
||||
}
|
||||
|
||||
public static IServiceCollection AddContentTemplate<TContent, TTemplate>(this IServiceCollection services,
|
||||
object? key = null)
|
||||
{
|
||||
Type contentType = typeof(TContent);
|
||||
Type templateType = typeof(TTemplate);
|
||||
|
||||
key ??= contentType.Name;
|
||||
|
||||
services.AddTransient(contentType);
|
||||
services.TryAddTransient(templateType);
|
||||
|
||||
services.AddKeyedTransient(contentType, key);
|
||||
services.AddKeyedTransient(templateType, key);
|
||||
|
||||
services.AddTransient<IContentTemplateDescriptor>(provider => new ContentTemplateDescriptor { ContentType = contentType, TemplateType = templateType, Key = key });
|
||||
|
||||
return services;
|
||||
}
|
||||
|
||||
public static IServiceCollection AddHandler<THandler>(this IServiceCollection services,
|
||||
ServiceLifetime lifetime = ServiceLifetime.Transient)
|
||||
where THandler :
|
||||
IHandler
|
||||
{
|
||||
@@ -62,71 +160,6 @@ public static class IServiceCollectionExtensions
|
||||
|
||||
return services;
|
||||
}
|
||||
|
||||
public static IServiceCollection AddConfiguration<TConfiguration>(this IServiceCollection services,
|
||||
IConfiguration configuration)
|
||||
where TConfiguration :
|
||||
class, new()
|
||||
{
|
||||
return services.AddConfiguration<TConfiguration>(configuration, typeof(TConfiguration).Name, "Settings.json", null);
|
||||
}
|
||||
|
||||
public static IServiceCollection AddConfiguration<TConfiguration>(this IServiceCollection services,
|
||||
IConfiguration configuration,
|
||||
TConfiguration? defaults = null)
|
||||
where TConfiguration :
|
||||
class, new()
|
||||
{
|
||||
return services.AddConfiguration(configuration, typeof(TConfiguration).Name, "Settings.json", defaults);
|
||||
}
|
||||
|
||||
public static IServiceCollection AddConfiguration<TConfiguration>(this IServiceCollection services,
|
||||
IConfiguration configuration,
|
||||
string section,
|
||||
string path = "Settings.json",
|
||||
TConfiguration? defaults = null,
|
||||
Action<JsonSerializerOptions>? serializerDelegate = null)
|
||||
where TConfiguration :
|
||||
class, new()
|
||||
{
|
||||
services.Configure<TConfiguration>(configuration);
|
||||
services.AddSingleton<IConfigureOptions<TConfiguration>>(new ConfigureNamedOptions<TConfiguration>("", args => { }));
|
||||
services.AddTransient(provider => provider.GetService<IOptionsMonitor<TConfiguration>>()!.CurrentValue);
|
||||
|
||||
services.AddSingleton<IConfigurationWriter<TConfiguration>>(provider =>
|
||||
{
|
||||
string? jsonFilePath = null;
|
||||
if (provider.GetService<IHostEnvironment>() is IHostEnvironment hostEnvironment)
|
||||
{
|
||||
IFileProvider fileProvider = hostEnvironment.ContentRootFileProvider;
|
||||
IFileInfo fileInfo = fileProvider.GetFileInfo(path);
|
||||
|
||||
jsonFilePath = fileInfo.PhysicalPath;
|
||||
}
|
||||
|
||||
jsonFilePath ??= Path.Combine(AppDomain.CurrentDomain.BaseDirectory, path);
|
||||
|
||||
JsonSerializerOptions? defaultSerializer = null;
|
||||
if (serializerDelegate is not null)
|
||||
{
|
||||
defaultSerializer = new JsonSerializerOptions();
|
||||
serializerDelegate.Invoke(defaultSerializer);
|
||||
}
|
||||
|
||||
return new ConfigurationWriter<TConfiguration>(jsonFilePath, section, defaultSerializer);
|
||||
});
|
||||
|
||||
if (defaults is not null)
|
||||
{
|
||||
}
|
||||
|
||||
services.AddTransient(provider => new DefaultConfiguration<TConfiguration>(defaults));
|
||||
services.AddTransient<IInitializer, ConfigurationInitializer<TConfiguration>>();
|
||||
|
||||
services.AddTransient<IWritableConfiguration<TConfiguration>, WritableConfiguration<TConfiguration>>();
|
||||
return services;
|
||||
}
|
||||
|
||||
public static IServiceCollection AddWidgetTemplate<TWidgetContent>(this IServiceCollection services)
|
||||
where TWidgetContent :
|
||||
IWidgetViewModel
|
||||
@@ -166,23 +199,4 @@ public static class IServiceCollectionExtensions
|
||||
|
||||
return services;
|
||||
}
|
||||
|
||||
public static IServiceCollection AddContentTemplate<TContent, TTemplate>(this IServiceCollection services,
|
||||
object? key = null)
|
||||
{
|
||||
Type contentType = typeof(TContent);
|
||||
Type templateType = typeof(TTemplate);
|
||||
|
||||
key ??= contentType.Name;
|
||||
|
||||
services.AddTransient(contentType);
|
||||
services.TryAddTransient(templateType);
|
||||
|
||||
services.AddKeyedTransient(contentType, key);
|
||||
services.AddKeyedTransient(templateType, key);
|
||||
|
||||
services.AddTransient<IContentTemplateDescriptor>(provider => new ContentTemplateDescriptor { ContentType = contentType, TemplateType = templateType, Key = key });
|
||||
|
||||
return services;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user