Add per plugin json configuration support from a single file
This commit is contained in:
@@ -0,0 +1,101 @@
|
||||
using System.Text.Encodings.Web;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace Hyperbar.Options;
|
||||
|
||||
public class ConfigurationWriter<TConfiguration>(string path,
|
||||
string section,
|
||||
JsonSerializerOptions? serializerOptions = null) :
|
||||
IConfigurationWriter<TConfiguration>
|
||||
where TConfiguration :
|
||||
class, new()
|
||||
{
|
||||
internal static Func<JsonSerializerOptions> DefaultSerializerOptions = new(() =>
|
||||
{
|
||||
return new JsonSerializerOptions
|
||||
{
|
||||
WriteIndented = true,
|
||||
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping,
|
||||
Converters = { new JsonStringEnumConverter() }
|
||||
};
|
||||
});
|
||||
|
||||
private readonly JsonSerializerOptions? serializerOptions = serializerOptions ??= DefaultSerializerOptions();
|
||||
|
||||
public void Write(Action<TConfiguration?>? updateDelegate = null)
|
||||
{
|
||||
TConfiguration? updatedValue = TryGet(out TConfiguration? value) ? value : new TConfiguration();
|
||||
|
||||
updateDelegate?.Invoke(updatedValue);
|
||||
Write(updatedValue);
|
||||
}
|
||||
|
||||
public void Write(TConfiguration? value)
|
||||
{
|
||||
if (!File.Exists(path))
|
||||
{
|
||||
string? fileDirectoryPath = Path.GetDirectoryName(path);
|
||||
if (!string.IsNullOrEmpty(fileDirectoryPath))
|
||||
{
|
||||
Directory.CreateDirectory(fileDirectoryPath);
|
||||
}
|
||||
|
||||
File.WriteAllText(path, "{}");
|
||||
}
|
||||
|
||||
byte[] jsonContent = File.ReadAllBytes(path);
|
||||
|
||||
using JsonDocument jsonDocument = JsonDocument.Parse(jsonContent);
|
||||
using FileStream stream = File.OpenWrite(path);
|
||||
Utf8JsonWriter writer = new(stream, new JsonWriterOptions()
|
||||
{
|
||||
Indented = true,
|
||||
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping
|
||||
});
|
||||
|
||||
writer.WriteStartObject();
|
||||
bool isWritten = false;
|
||||
JsonDocument optionsElement = JsonDocument.Parse(JsonSerializer.SerializeToUtf8Bytes(value, serializerOptions));
|
||||
|
||||
foreach (JsonProperty element in jsonDocument.RootElement.EnumerateObject())
|
||||
{
|
||||
if (element.Name != section)
|
||||
{
|
||||
element.WriteTo(writer);
|
||||
continue;
|
||||
}
|
||||
writer.WritePropertyName(element.Name);
|
||||
optionsElement.WriteTo(writer);
|
||||
isWritten = true;
|
||||
}
|
||||
|
||||
if (!isWritten)
|
||||
{
|
||||
writer.WritePropertyName(section);
|
||||
optionsElement.WriteTo(writer);
|
||||
}
|
||||
|
||||
writer.WriteEndObject();
|
||||
writer.Flush();
|
||||
stream.SetLength(stream.Position);
|
||||
}
|
||||
|
||||
private bool TryGet<T>(out T? 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<T>(sectionValue.ToString(), serializerOptions);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
value = default;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
|
||||
namespace Hyperbar.Options
|
||||
{
|
||||
public interface IConfigurationWriter<TConfiguration>
|
||||
where TConfiguration :
|
||||
class, new()
|
||||
{
|
||||
void Write(Action<TConfiguration?>? updateDelegate = null);
|
||||
|
||||
void Write(TConfiguration? value);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
namespace Hyperbar;
|
||||
|
||||
public interface IWritableConfiguration<out TConfiguration> :
|
||||
IOptionsSnapshot<TConfiguration>
|
||||
where TConfiguration :
|
||||
class, new()
|
||||
{
|
||||
void Update(Action<TConfiguration?> updateAction,
|
||||
bool reload = true);
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
using Hyperbar.Options;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
namespace Hyperbar;
|
||||
|
||||
public class WritableConfiguration<TConfiguration>(IConfigurationWriter<TConfiguration> writer,
|
||||
IOptionsMonitor<TConfiguration> options,
|
||||
IConfiguration configuration) :
|
||||
IWritableConfiguration<TConfiguration>
|
||||
where TConfiguration :
|
||||
class, new()
|
||||
{
|
||||
public TConfiguration Value => options.CurrentValue;
|
||||
|
||||
public TConfiguration Get(string? name) => options.Get(name);
|
||||
|
||||
public void Update(Action<TConfiguration?> updateDelegate,
|
||||
bool reload = true)
|
||||
{
|
||||
writer.Write(updateDelegate);
|
||||
if (reload && configuration is IConfigurationRoot configurationRoot)
|
||||
{
|
||||
configurationRoot.Reload();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,21 +0,0 @@
|
||||
using Microsoft.Extensions.Configuration;
|
||||
|
||||
namespace Hyperbar.Configurations;
|
||||
|
||||
public class ConfigurationWriter<TConfiguration>(IConfiguration rootConfiguration) :
|
||||
IConfigurationWriter<TConfiguration> where TConfiguration : class, new()
|
||||
{
|
||||
public void Write(string section, TConfiguration configuration)
|
||||
{
|
||||
if (rootConfiguration is IConfigurationRoot root)
|
||||
{
|
||||
foreach (IConfigurationProvider? provider in root.Providers)
|
||||
{
|
||||
if (provider is IWritableConfigurationProvider writableConfigurationProvider)
|
||||
{
|
||||
writableConfigurationProvider.Write(section, configuration);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,6 +0,0 @@
|
||||
namespace Hyperbar.Configurations;
|
||||
|
||||
public interface IConfigurationWriter<TConfiguration> where TConfiguration : class
|
||||
{
|
||||
void Write(string section, TConfiguration args);
|
||||
}
|
||||
@@ -1,6 +0,0 @@
|
||||
namespace Hyperbar.Configurations;
|
||||
|
||||
public interface IWritableConfigurationProvider
|
||||
{
|
||||
void Write<TValue>(string section, TValue value) where TValue : class, new();
|
||||
}
|
||||
@@ -1,12 +0,0 @@
|
||||
namespace Hyperbar.Configurations;
|
||||
|
||||
public interface IWritableJsonConfigurationBuilder
|
||||
{
|
||||
Stream? DefaultFileStream { get; }
|
||||
|
||||
IWritableJsonConfigurationBuilder AddDefaultConfiguration<TConfiguration>(string Key) where TConfiguration : class;
|
||||
|
||||
IWritableJsonConfigurationBuilder AddDefaultFileStream(Stream stream);
|
||||
|
||||
void Build(string path);
|
||||
}
|
||||
@@ -1,8 +0,0 @@
|
||||
namespace Hyperbar.Configurations;
|
||||
|
||||
public interface IWritableJsonConfigurationDescriptor
|
||||
{
|
||||
Type ConfigurationType { get; }
|
||||
|
||||
string Key { get; }
|
||||
}
|
||||
@@ -1,110 +0,0 @@
|
||||
using Json.Patch;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
using JsonSerializer = System.Text.Json.JsonSerializer;
|
||||
|
||||
namespace Hyperbar.Configurations;
|
||||
|
||||
public class WritableJsonConfigurationBuilder :
|
||||
IWritableJsonConfigurationBuilder
|
||||
{
|
||||
private readonly List<IWritableJsonConfigurationDescriptor> descriptors = [];
|
||||
|
||||
public Stream? DefaultFileStream { get; private set; }
|
||||
|
||||
public IReadOnlyCollection<IWritableJsonConfigurationDescriptor> Descriptors => new ReadOnlyCollection<IWritableJsonConfigurationDescriptor>(descriptors);
|
||||
|
||||
public IWritableJsonConfigurationBuilder AddDefaultConfiguration<TConfiguration>(string Key) where TConfiguration : class
|
||||
{
|
||||
descriptors.Add(new WritableJsonConfigurationDescriptor(typeof(TConfiguration), Key));
|
||||
return this;
|
||||
}
|
||||
|
||||
public IWritableJsonConfigurationBuilder AddDefaultFileStream(Stream stream)
|
||||
{
|
||||
DefaultFileStream = stream;
|
||||
return this;
|
||||
}
|
||||
|
||||
public void Build(string path)
|
||||
{
|
||||
JsonSerializerOptions options = new() { DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull };
|
||||
|
||||
JObject? sourceDocument = [];
|
||||
if (TryLoadSource(out string? defaultContent))
|
||||
{
|
||||
sourceDocument = JObject.Parse(defaultContent!);
|
||||
}
|
||||
|
||||
JObject? targetDocument = [];
|
||||
if (TryLoadTarget(path, out string? targetContent))
|
||||
{
|
||||
targetDocument = JObject.Parse(targetContent!);
|
||||
}
|
||||
|
||||
foreach (IWritableJsonConfigurationDescriptor? descriptor in descriptors)
|
||||
{
|
||||
if (sourceDocument.SelectToken($"$.{descriptor.Key}") is JToken sourceSection)
|
||||
{
|
||||
if (targetDocument.SelectToken($"$.{descriptor.Key}") is JToken targetSection)
|
||||
{
|
||||
object? source = JsonSerializer.Deserialize(JsonConvert.SerializeObject(sourceSection), descriptor.ConfigurationType);
|
||||
object? target = JsonSerializer.Deserialize(JsonConvert.SerializeObject(targetSection), descriptor.ConfigurationType);
|
||||
|
||||
JsonPatch? patch = source.CreatePatch(target);
|
||||
if (patch.Apply(source) is object sourcePatched)
|
||||
{
|
||||
targetSection.Replace(JToken.Parse(JsonSerializer.Serialize(sourcePatched, options)));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
object? source = JsonSerializer.Deserialize(JsonConvert.SerializeObject(sourceSection), descriptor.ConfigurationType);
|
||||
targetDocument.Add(descriptor.Key, JToken.Parse(JsonSerializer.Serialize(source, options)));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
object? configuration = Activator.CreateInstance(descriptor.ConfigurationType);
|
||||
targetDocument.Add(descriptor.Key, JToken.Parse(JsonSerializer.Serialize(configuration, options)));
|
||||
}
|
||||
}
|
||||
|
||||
using FileStream? fileStream = new(path, FileMode.Create, FileAccess.Write);
|
||||
using StreamWriter streamWriter = new(fileStream);
|
||||
using JsonTextWriter writer = new(streamWriter) { Formatting = Formatting.Indented };
|
||||
targetDocument.WriteTo(writer);
|
||||
}
|
||||
|
||||
private static bool TryLoadTarget(string path, [MaybeNull] out string? content)
|
||||
{
|
||||
if (File.Exists(path))
|
||||
{
|
||||
using FileStream? fileStream = new(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
|
||||
using StreamReader? streamReader = new(fileStream);
|
||||
content = streamReader.ReadToEnd();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
content = null;
|
||||
return false;
|
||||
}
|
||||
|
||||
private bool TryLoadSource([MaybeNull] out string? content)
|
||||
{
|
||||
if (DefaultFileStream is Stream fileStream)
|
||||
{
|
||||
using StreamReader? streamReader = new(fileStream);
|
||||
content = streamReader.ReadToEnd();
|
||||
return true;
|
||||
}
|
||||
|
||||
content = null;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -1,3 +0,0 @@
|
||||
namespace Hyperbar.Configurations;
|
||||
|
||||
public record WritableJsonConfigurationDescriptor(Type ConfigurationType, string Key) : IWritableJsonConfigurationDescriptor;
|
||||
@@ -1,147 +0,0 @@
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace Hyperbar.Configurations;
|
||||
public class WritableJsonConfigurationFile
|
||||
{
|
||||
private readonly Dictionary<string, (JsonValueKind?, string?)> data = new(StringComparer.OrdinalIgnoreCase);
|
||||
private readonly Stack<string> paths = new();
|
||||
private JObject tokenCache = [];
|
||||
private bool isParsing;
|
||||
|
||||
public IDictionary<string, string?> Parse(Stream input)
|
||||
{
|
||||
return ParseStream(input);
|
||||
}
|
||||
|
||||
public void Write(string key, string value, Stream output)
|
||||
{
|
||||
if (isParsing)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (key[^1] == ':')
|
||||
{
|
||||
key = key[0..^1];
|
||||
}
|
||||
|
||||
string? tokenPath = $"$.{key.Replace(":", ".")}";
|
||||
key = key.Replace("[", "").Replace("]", "");
|
||||
|
||||
if (tokenCache.SelectToken(tokenPath) is JToken token && data.ContainsKey(key))
|
||||
{
|
||||
(JsonValueKind? kind, string _) = data[key];
|
||||
object? newValue = ConvertValue(kind, value);
|
||||
|
||||
data[key] = new(kind, value);
|
||||
token.Replace(JToken.FromObject(newValue));
|
||||
}
|
||||
|
||||
using StreamWriter streamWriter = new(output);
|
||||
using JsonTextWriter writer = new(streamWriter) { Formatting = Formatting.Indented };
|
||||
tokenCache.WriteTo(writer);
|
||||
|
||||
output.SetLength(output.Position);
|
||||
}
|
||||
|
||||
private static object ConvertValue(JsonValueKind? kind, string value)
|
||||
{
|
||||
return kind is JsonValueKind.True or JsonValueKind.False ? bool.Parse(value) : value;
|
||||
}
|
||||
|
||||
private void EnterContext(string context)
|
||||
{
|
||||
paths.Push(paths.Count > 0 ? paths.Peek() + ConfigurationPath.KeyDelimiter + context : context);
|
||||
}
|
||||
|
||||
private void ExitContext()
|
||||
{
|
||||
_ = paths.Pop();
|
||||
}
|
||||
|
||||
private IDictionary<string, string?> ParseStream(Stream input)
|
||||
{
|
||||
data.Clear();
|
||||
|
||||
JsonDocumentOptions jsonDocumentOptions = new()
|
||||
{
|
||||
CommentHandling = JsonCommentHandling.Skip,
|
||||
AllowTrailingCommas = true,
|
||||
};
|
||||
|
||||
isParsing = true;
|
||||
using (StreamReader? reader = new(input))
|
||||
{
|
||||
string? content = reader.ReadToEnd();
|
||||
tokenCache = JObject.Parse(content);
|
||||
|
||||
using JsonDocument? doc = JsonDocument.Parse(content, jsonDocumentOptions);
|
||||
VisitElement(doc.RootElement);
|
||||
}
|
||||
isParsing = false;
|
||||
|
||||
return data.ToDictionary(k => k.Key, v => v.Value.Item2?.ToString() ?? null);
|
||||
}
|
||||
|
||||
private void VisitElement(JsonElement element)
|
||||
{
|
||||
bool isEmpty = true;
|
||||
|
||||
foreach (JsonProperty property in element.EnumerateObject())
|
||||
{
|
||||
isEmpty = false;
|
||||
|
||||
EnterContext(property.Name);
|
||||
VisitValue(property.Value);
|
||||
ExitContext();
|
||||
}
|
||||
|
||||
if (isEmpty && paths.Count > 0)
|
||||
{
|
||||
data[paths.Peek()] = (JsonValueKind.Null, null);
|
||||
}
|
||||
}
|
||||
|
||||
private void VisitValue(JsonElement value)
|
||||
{
|
||||
switch (value.ValueKind)
|
||||
{
|
||||
case JsonValueKind.Object:
|
||||
VisitElement(value);
|
||||
break;
|
||||
|
||||
case JsonValueKind.Array:
|
||||
int index = 0;
|
||||
foreach (JsonElement arrayElement in value.EnumerateArray())
|
||||
{
|
||||
EnterContext(index.ToString());
|
||||
VisitValue(arrayElement);
|
||||
ExitContext();
|
||||
index++;
|
||||
}
|
||||
break;
|
||||
|
||||
case JsonValueKind.Number:
|
||||
case JsonValueKind.String:
|
||||
case JsonValueKind.True:
|
||||
case JsonValueKind.False:
|
||||
case JsonValueKind.Null:
|
||||
string key = paths.Peek();
|
||||
if (data.ContainsKey(key))
|
||||
{
|
||||
throw new FormatException();
|
||||
}
|
||||
data[key] = new(value.ValueKind, value.ToString());
|
||||
break;
|
||||
|
||||
case JsonValueKind.Undefined:
|
||||
break;
|
||||
|
||||
default:
|
||||
throw new FormatException();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,43 +0,0 @@
|
||||
using Microsoft.Extensions.Configuration.Json;
|
||||
using Microsoft.Extensions.FileProviders;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
|
||||
namespace Hyperbar.Configurations;
|
||||
|
||||
public class WritableJsonConfigurationProvider(JsonConfigurationSource source) :
|
||||
JsonConfigurationProvider(source),
|
||||
IWritableConfigurationProvider
|
||||
{
|
||||
public void Write<TValue>(string section, TValue value) where TValue : class, new()
|
||||
{
|
||||
IFileInfo? file = Source.FileProvider?.GetFileInfo(Source.Path ?? string.Empty);
|
||||
static Stream OpenRead(IFileInfo fileInfo)
|
||||
{
|
||||
return fileInfo.PhysicalPath is not null
|
||||
? new FileStream(fileInfo.PhysicalPath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite)
|
||||
: fileInfo.CreateReadStream();
|
||||
}
|
||||
|
||||
if (file is null)
|
||||
{
|
||||
throw new FileNotFoundException();
|
||||
}
|
||||
|
||||
using Stream stream = OpenRead(file);
|
||||
using StreamReader? reader = new(stream);
|
||||
|
||||
string? content = reader.ReadToEnd();
|
||||
JObject? document = JObject.Parse(content);
|
||||
|
||||
if (document.SelectToken($"$.{section}") is JToken sectionToken)
|
||||
{
|
||||
sectionToken.Replace(JToken.Parse(JsonConvert.SerializeObject(value)));
|
||||
stream.SetLength(0);
|
||||
|
||||
using StreamWriter streamWriter = new(stream);
|
||||
using JsonTextWriter writer = new(streamWriter) { Formatting = Formatting.Indented };
|
||||
document.WriteTo(writer);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,28 +0,0 @@
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.Configuration.Json;
|
||||
using Microsoft.Extensions.FileProviders;
|
||||
|
||||
namespace Hyperbar.Configurations;
|
||||
|
||||
public class WritableJsonConfigurationSource :
|
||||
JsonConfigurationSource
|
||||
{
|
||||
public IWritableJsonConfigurationBuilder? Factory { get; set; }
|
||||
|
||||
public override IConfigurationProvider Build(IConfigurationBuilder builder)
|
||||
{
|
||||
EnsureDefaultsWithSteam(builder);
|
||||
return new WritableJsonConfigurationProvider(this);
|
||||
}
|
||||
|
||||
private void EnsureDefaultsWithSteam(IConfigurationBuilder builder)
|
||||
{
|
||||
EnsureDefaults(builder);
|
||||
|
||||
if (FileProvider is PhysicalFileProvider physicalFileProvider)
|
||||
{
|
||||
string? outputFile = System.IO.Path.Combine(physicalFileProvider.Root, Path);
|
||||
Factory?.Build(outputFile);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,10 +1,62 @@
|
||||
using Hyperbar.Lifecycles;
|
||||
using Hyperbar.Options;
|
||||
using Hyperbar.Templates;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
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 ConfigureWritableOptions<TConfiguration>(this IServiceCollection services,
|
||||
string path = "Settings.json",
|
||||
Func<JsonSerializerOptions>? defaultSerializerOptions = null)
|
||||
where TConfiguration :
|
||||
class, new()
|
||||
{
|
||||
return services.ConfigureWritableOptions<TConfiguration>(typeof(TConfiguration).Name, path);
|
||||
}
|
||||
|
||||
public static IServiceCollection ConfigureWritableOptions<TConfiguration>(this IServiceCollection services,
|
||||
string section,
|
||||
string path = "Settings.json",
|
||||
Action<JsonSerializerOptions>? serializerDelegate = null)
|
||||
where TConfiguration :
|
||||
class, new()
|
||||
{
|
||||
services.AddOptions();
|
||||
services.AddSingleton<IConfigureOptions<TConfiguration>>(new ConfigureNamedOptions<TConfiguration>("", args => { }));
|
||||
|
||||
services.AddTransient<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? defaultSerializerOptions = null;
|
||||
if (serializerDelegate is not null)
|
||||
{
|
||||
defaultSerializerOptions = new JsonSerializerOptions();
|
||||
serializerDelegate.Invoke(defaultSerializerOptions);
|
||||
}
|
||||
|
||||
return new ConfigurationWriter<TConfiguration>(jsonFilePath, section, defaultSerializerOptions);
|
||||
});
|
||||
|
||||
services.AddTransient<IWritableConfiguration<TConfiguration>, WritableConfiguration<TConfiguration>>();
|
||||
return services;
|
||||
}
|
||||
|
||||
public static IServiceCollection AddCommandTemplate<TCommand, TCommandTemplate>(this IServiceCollection services)
|
||||
where TCommand :
|
||||
ICommandViewModel
|
||||
@@ -13,14 +65,13 @@ public static class IServiceCollectionExtensions
|
||||
Type templateType = typeof(TCommandTemplate);
|
||||
|
||||
string key = dataType.Name;
|
||||
|
||||
_ = services.AddTransient(typeof(ICommandViewModel), dataType);
|
||||
_ = services.AddTransient(templateType);
|
||||
|
||||
_ = services.AddKeyedTransient(typeof(ICommandViewModel), key, dataType);
|
||||
_ = services.AddKeyedTransient(templateType, key);
|
||||
|
||||
_ = services.AddTransient<IDataTemplateDescriptor>(provider => new DataTemplateDescriptor
|
||||
|
||||
services.AddTransient(typeof(ICommandViewModel), dataType);
|
||||
services.AddTransient(templateType);
|
||||
services.AddKeyedTransient(typeof(ICommandViewModel), key, dataType);
|
||||
services.AddKeyedTransient(templateType, key);
|
||||
|
||||
services.AddTransient<IDataTemplateDescriptor>(provider => new DataTemplateDescriptor
|
||||
{
|
||||
DataType = dataType,
|
||||
TemplateType = templateType,
|
||||
@@ -38,10 +89,10 @@ public static class IServiceCollectionExtensions
|
||||
|
||||
key ??= dataType.Name;
|
||||
|
||||
_ = services.AddKeyedTransient(dataType, key);
|
||||
_ = services.AddKeyedTransient(templateType, key);
|
||||
|
||||
_ = services.AddTransient<IDataTemplateDescriptor>(provider => new DataTemplateDescriptor
|
||||
services.AddKeyedTransient(dataType, key);
|
||||
services.AddKeyedTransient(templateType, key);
|
||||
|
||||
services.AddTransient<IDataTemplateDescriptor>(provider => new DataTemplateDescriptor
|
||||
{
|
||||
DataType = dataType,
|
||||
TemplateType = templateType,
|
||||
|
||||
@@ -1,78 +0,0 @@
|
||||
using Hyperbar.Configurations;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.FileProviders;
|
||||
|
||||
namespace Hyperbar.Extensions;
|
||||
|
||||
public static class WritableJsonConfigurationExtensions
|
||||
{
|
||||
public static IConfigurationBuilder AddWritableJsonFile(this IConfigurationBuilder builder,
|
||||
string path)
|
||||
{
|
||||
return builder.AddWritableJsonFile(null, path, false, false, null);
|
||||
}
|
||||
|
||||
public static IConfigurationBuilder AddWritableJsonFile(this IConfigurationBuilder builder,
|
||||
string path,
|
||||
Action<IWritableJsonConfigurationBuilder>? factoryDelegate)
|
||||
{
|
||||
return builder.AddWritableJsonFile(null, path, false, false, factoryDelegate);
|
||||
}
|
||||
|
||||
public static IConfigurationBuilder AddWritableJsonFile(this IConfigurationBuilder builder,
|
||||
string path,
|
||||
bool optional)
|
||||
{
|
||||
return builder.AddWritableJsonFile(null, path, optional, false, null);
|
||||
}
|
||||
|
||||
public static IConfigurationBuilder AddWritableJsonFile(this IConfigurationBuilder builder,
|
||||
string path,
|
||||
bool optional,
|
||||
Action<IWritableJsonConfigurationBuilder>? factoryDelegate)
|
||||
{
|
||||
return builder.AddWritableJsonFile(null, path, optional, false, factoryDelegate);
|
||||
}
|
||||
|
||||
public static IConfigurationBuilder AddWritableJsonFile(this IConfigurationBuilder builder,
|
||||
string path,
|
||||
bool optional,
|
||||
bool reloadOnChange)
|
||||
{
|
||||
return builder.AddWritableJsonFile(null, path, optional, reloadOnChange, null);
|
||||
}
|
||||
|
||||
public static IConfigurationBuilder AddWritableJsonFile(this IConfigurationBuilder builder,
|
||||
string path,
|
||||
bool optional,
|
||||
bool reloadOnChange,
|
||||
Action<IWritableJsonConfigurationBuilder>? factoryDelegate)
|
||||
{
|
||||
return builder.AddWritableJsonFile(null, path, optional, reloadOnChange, factoryDelegate);
|
||||
}
|
||||
|
||||
public static IConfigurationBuilder AddWritableJsonFile(this IConfigurationBuilder builder,
|
||||
IFileProvider? provider,
|
||||
string path,
|
||||
bool optional,
|
||||
bool reloadOnChange, Action<IWritableJsonConfigurationBuilder>? writableJsonConfigurationDelegate)
|
||||
{
|
||||
IWritableJsonConfigurationBuilder writableJsonConfigurationBuilder = new WritableJsonConfigurationBuilder();
|
||||
writableJsonConfigurationDelegate?.Invoke(writableJsonConfigurationBuilder);
|
||||
|
||||
return builder.AddWritableJsonFile(configuration =>
|
||||
{
|
||||
configuration.FileProvider = provider;
|
||||
configuration.Path = path;
|
||||
configuration.Optional = optional;
|
||||
configuration.ReloadOnChange = reloadOnChange;
|
||||
configuration.Factory = writableJsonConfigurationBuilder;
|
||||
configuration.ResolveFileProvider();
|
||||
});
|
||||
}
|
||||
|
||||
public static IConfigurationBuilder AddWritableJsonFile(this IConfigurationBuilder builder, Action<WritableJsonConfigurationSource> configureSource)
|
||||
{
|
||||
return builder.Add(configureSource);
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,7 @@
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
<UseRidGraph>true</UseRidGraph>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0" />
|
||||
|
||||
@@ -1,14 +1,7 @@
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using System.Collections.ObjectModel;
|
||||
|
||||
namespace Hyperbar.Lifecycles;
|
||||
|
||||
public class ObservableCollectionViewModel :
|
||||
ObservableCollection<object>
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public class AppService(IEnumerable<IInitializer> initializers) :
|
||||
IHostedService
|
||||
{
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
using System.Collections.ObjectModel;
|
||||
|
||||
namespace Hyperbar.Lifecycles;
|
||||
|
||||
public class ObservableCollectionViewModel :
|
||||
ObservableCollection<object>
|
||||
{
|
||||
public void AddRange(IEnumerable<object> collection)
|
||||
{
|
||||
foreach (var item in collection)
|
||||
{
|
||||
Add(item);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user