Start to port main gut of the foundation
This commit is contained in:
@@ -0,0 +1,4 @@
|
|||||||
|
namespace Toolkit.Foundation
|
||||||
|
{
|
||||||
|
public record ConfigurationChanged<TConfiguration>(TConfiguration Configuration) where TConfiguration : class;
|
||||||
|
}
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
using CommunityToolkit.Mvvm.Messaging;
|
||||||
|
|
||||||
|
namespace Toolkit.Foundation
|
||||||
|
{
|
||||||
|
public class ConfigurationInitializer<TConfiguration> : IInitializer where TConfiguration : class, new()
|
||||||
|
{
|
||||||
|
private readonly TConfiguration configuration;
|
||||||
|
private readonly IMessenger messenger;
|
||||||
|
|
||||||
|
public ConfigurationInitializer(TConfiguration configuration,
|
||||||
|
IMessenger messenger)
|
||||||
|
{
|
||||||
|
this.configuration = configuration;
|
||||||
|
this.messenger = messenger;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task InitializeAsync()
|
||||||
|
{
|
||||||
|
messenger.Send(configuration);
|
||||||
|
await Task.CompletedTask;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,31 @@
|
|||||||
|
using Microsoft.Extensions.Configuration;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using Microsoft.Extensions.Hosting;
|
||||||
|
|
||||||
|
namespace Toolkit.Foundation
|
||||||
|
{
|
||||||
|
public class ConfigurationWriter<TConfiguration> : IConfigurationWriter<TConfiguration> where TConfiguration : class, new()
|
||||||
|
{
|
||||||
|
private readonly IConfiguration rootConfiguration;
|
||||||
|
|
||||||
|
public ConfigurationWriter(IConfiguration rootConfiguration)
|
||||||
|
{
|
||||||
|
this.rootConfiguration = rootConfiguration;
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
namespace Toolkit.Foundation
|
||||||
|
{
|
||||||
|
public interface IConfigurationWriter<TConfiguration> where TConfiguration : class
|
||||||
|
{
|
||||||
|
void Write(string section, TConfiguration args);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
namespace Toolkit.Foundation
|
||||||
|
{
|
||||||
|
public interface IWritableConfigurationProvider
|
||||||
|
{
|
||||||
|
void Write<TValue>(string section, TValue value) where TValue : class, new();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
namespace Toolkit.Foundation
|
||||||
|
{
|
||||||
|
public interface IWritableJsonConfigurationBuilder
|
||||||
|
{
|
||||||
|
Stream? DefaultFileStream { get; }
|
||||||
|
|
||||||
|
IWritableJsonConfigurationBuilder AddDefaultConfiguration<TConfiguration>(string Key) where TConfiguration : class;
|
||||||
|
|
||||||
|
IWritableJsonConfigurationBuilder AddDefaultFileStream(Stream stream);
|
||||||
|
|
||||||
|
void Build(string path);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
namespace Toolkit.Foundation
|
||||||
|
{
|
||||||
|
public interface IWritableJsonConfigurationDescriptor
|
||||||
|
{
|
||||||
|
Type ConfigurationType { get; }
|
||||||
|
|
||||||
|
string Key { get; }
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,110 @@
|
|||||||
|
using Json.Patch;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
using Newtonsoft.Json.Linq;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Collections.ObjectModel;
|
||||||
|
using System.Diagnostics.CodeAnalysis;
|
||||||
|
using System.IO;
|
||||||
|
using System.Text.Json;
|
||||||
|
using System.Text.Json.Serialization;
|
||||||
|
using JsonSerializer = System.Text.Json.JsonSerializer;
|
||||||
|
|
||||||
|
namespace Toolkit.Foundation
|
||||||
|
{
|
||||||
|
public class WritableJsonConfigurationBuilder : IWritableJsonConfigurationBuilder
|
||||||
|
{
|
||||||
|
private readonly List<IWritableJsonConfigurationDescriptor> descriptors = new();
|
||||||
|
|
||||||
|
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)
|
||||||
|
{
|
||||||
|
JObject? sourceDocument = new();
|
||||||
|
if (TryLoadSource(out string? defaultContent))
|
||||||
|
{
|
||||||
|
sourceDocument = JObject.Parse(defaultContent!);
|
||||||
|
}
|
||||||
|
|
||||||
|
JObject? targetDocument = new();
|
||||||
|
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);
|
||||||
|
|
||||||
|
object? sourcePatched = patch.Apply(source);
|
||||||
|
targetSection.Replace(JToken.Parse(JsonSerializer.Serialize(sourcePatched, new JsonSerializerOptions { DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull })));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
object? source = JsonSerializer.Deserialize(JsonConvert.SerializeObject(sourceSection), descriptor.ConfigurationType);
|
||||||
|
targetDocument.Add(descriptor.Key, JToken.Parse(JsonSerializer.Serialize(source, new JsonSerializerOptions { DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull })));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
object? configuration = Activator.CreateInstance(descriptor.ConfigurationType);
|
||||||
|
targetDocument.Add(descriptor.Key, JToken.Parse(JsonSerializer.Serialize(configuration, new JsonSerializerOptions { DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull })));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
namespace Toolkit.Foundation
|
||||||
|
{
|
||||||
|
public record WritableJsonConfigurationDescriptor(Type ConfigurationType, string Key) : IWritableJsonConfigurationDescriptor;
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,76 @@
|
|||||||
|
using Microsoft.Extensions.Configuration;
|
||||||
|
using Microsoft.Extensions.FileProviders;
|
||||||
|
|
||||||
|
namespace Toolkit.Foundation
|
||||||
|
{
|
||||||
|
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) => builder.Add(configureSource);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,150 @@
|
|||||||
|
using Microsoft.Extensions.Configuration;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
using Newtonsoft.Json.Linq;
|
||||||
|
using System.Text.Json;
|
||||||
|
|
||||||
|
namespace Toolkit.Foundation
|
||||||
|
{
|
||||||
|
internal class WritableJsonConfigurationFile
|
||||||
|
{
|
||||||
|
private readonly Dictionary<string, (JsonValueKind?, string?)> data = new(StringComparer.OrdinalIgnoreCase);
|
||||||
|
private readonly Stack<string> paths = new();
|
||||||
|
private JObject tokenCache = new();
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,48 @@
|
|||||||
|
using Microsoft.Extensions.Configuration.Json;
|
||||||
|
using Microsoft.Extensions.FileProviders;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
using Newtonsoft.Json.Linq;
|
||||||
|
|
||||||
|
namespace Toolkit.Foundation
|
||||||
|
{
|
||||||
|
public class WritableJsonConfigurationProvider : JsonConfigurationProvider, IWritableConfigurationProvider
|
||||||
|
{
|
||||||
|
public WritableJsonConfigurationProvider(JsonConfigurationSource source) : base(source)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
using Microsoft.Extensions.Configuration.Json;
|
||||||
|
using Microsoft.Extensions.Configuration;
|
||||||
|
using Microsoft.Extensions.FileProviders;
|
||||||
|
|
||||||
|
namespace Toolkit.Foundation
|
||||||
|
{
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
namespace Toolkit.Foundation
|
||||||
|
{
|
||||||
|
public record Write<TConfiguration>(string Section, Action<TConfiguration> UpdateDelegate) where TConfiguration : class;
|
||||||
|
}
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
using CommunityToolkit.Mvvm.Messaging;
|
||||||
|
|
||||||
|
namespace Toolkit.Foundation
|
||||||
|
{
|
||||||
|
public class WriteHandler<TConfiguration> : IRecipient<Write<TConfiguration>> where TConfiguration : class
|
||||||
|
{
|
||||||
|
private readonly IMessenger messenger;
|
||||||
|
private readonly TConfiguration configuration;
|
||||||
|
private readonly IConfigurationWriter<TConfiguration> writer;
|
||||||
|
|
||||||
|
public WriteHandler(TConfiguration configuration,
|
||||||
|
IConfigurationWriter<TConfiguration> writer,
|
||||||
|
IMessenger messenger)
|
||||||
|
{
|
||||||
|
this.messenger = messenger;
|
||||||
|
this.configuration = configuration;
|
||||||
|
this.writer = writer;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Receive(Write<TConfiguration> request)
|
||||||
|
{
|
||||||
|
request.UpdateDelegate.Invoke(configuration);
|
||||||
|
writer.Write(request.Section, configuration);
|
||||||
|
|
||||||
|
messenger.Send(new ConfigurationChanged<TConfiguration>(configuration));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using Microsoft.Extensions.Hosting;
|
||||||
|
|
||||||
|
namespace Toolkit.Foundation
|
||||||
|
{
|
||||||
|
public static class IHostBuilderExtensions
|
||||||
|
{
|
||||||
|
public static IHostBuilder UseContentRoot(this IHostBuilder hostBuilder, string contentRoot, bool createDirectory)
|
||||||
|
{
|
||||||
|
if (!Directory.Exists(contentRoot) && createDirectory)
|
||||||
|
{
|
||||||
|
Directory.CreateDirectory(contentRoot);
|
||||||
|
}
|
||||||
|
|
||||||
|
return hostBuilder.UseContentRoot(contentRoot);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
namespace Toolkit.Foundation
|
||||||
|
{
|
||||||
|
public static class IServiceFactoryExtensions
|
||||||
|
{
|
||||||
|
public static T? Get<T>(this IServiceFactory serviceFactory)
|
||||||
|
{
|
||||||
|
return serviceFactory.Get<T>(typeof(T));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static T Create<T>(this IServiceFactory serviceFactory, params object?[] parameters)
|
||||||
|
{
|
||||||
|
return serviceFactory.Create<T>(typeof(T), parameters);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static object? Create(this IServiceFactory serviceFactory, Type type)
|
||||||
|
{
|
||||||
|
ServiceFactoryDescriptor? descriptor = new(serviceFactory);
|
||||||
|
return descriptor.Create(type);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,29 @@
|
|||||||
|
using CommunityToolkit.Mvvm.Messaging;
|
||||||
|
using Microsoft.Extensions.Hosting;
|
||||||
|
|
||||||
|
namespace Toolkit.Foundation
|
||||||
|
{
|
||||||
|
public class AppService : IHostedService
|
||||||
|
{
|
||||||
|
private readonly IMessenger messenger;
|
||||||
|
private readonly IInitialization initialization;
|
||||||
|
|
||||||
|
public AppService(IMessenger messenger,
|
||||||
|
IInitialization initialization)
|
||||||
|
{
|
||||||
|
this.messenger = messenger;
|
||||||
|
this.initialization = initialization;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task StartAsync(CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
messenger.Send(new Initialize());
|
||||||
|
await initialization.InitializeAsync();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Task StopAsync(CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
return Task.CompletedTask;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
namespace Toolkit.Foundation
|
||||||
|
{
|
||||||
|
public interface ICache
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
namespace Toolkit.Foundation
|
||||||
|
{
|
||||||
|
public interface IInitialization
|
||||||
|
{
|
||||||
|
Task InitializeAsync();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
namespace Toolkit.Foundation
|
||||||
|
{
|
||||||
|
public interface IInitializer
|
||||||
|
{
|
||||||
|
Task InitializeAsync();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
namespace Toolkit.Foundation
|
||||||
|
{
|
||||||
|
public class Initialization : IInitialization
|
||||||
|
{
|
||||||
|
private readonly Func<IEnumerable<IInitializer?>> factory;
|
||||||
|
|
||||||
|
public Initialization(Func<IEnumerable<IInitializer?>> factory)
|
||||||
|
{
|
||||||
|
this.factory = factory;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task InitializeAsync()
|
||||||
|
{
|
||||||
|
foreach (IInitializer? initializer in factory())
|
||||||
|
{
|
||||||
|
if (initializer is not null)
|
||||||
|
{
|
||||||
|
await initializer.InitializeAsync();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
namespace Toolkit.Foundation
|
||||||
|
{
|
||||||
|
public record class Initialize;
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
namespace Toolkit.Foundation
|
||||||
|
{
|
||||||
|
public interface IServiceCreator<T>
|
||||||
|
{
|
||||||
|
object Create(Func<Type, object[], object> creator, params object[] parameters);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
namespace Toolkit.Foundation
|
||||||
|
{
|
||||||
|
public interface IServiceFactory
|
||||||
|
{
|
||||||
|
T? Get<T>(Type type);
|
||||||
|
|
||||||
|
T Create<T>(Type type, params object?[] parameters);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
namespace Toolkit.Foundation
|
||||||
|
{
|
||||||
|
public class ServiceCreator<T> : IServiceCreator<T>
|
||||||
|
{
|
||||||
|
public virtual object Create(Func<Type, object[], object> creator, params object[] parameters)
|
||||||
|
{
|
||||||
|
return creator(typeof(T), parameters);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,26 @@
|
|||||||
|
namespace Toolkit.Foundation
|
||||||
|
{
|
||||||
|
public class ServiceFactory : IServiceFactory
|
||||||
|
{
|
||||||
|
private readonly Func<Type, object?> factory;
|
||||||
|
private readonly Func<Type, object?[], object> creator;
|
||||||
|
|
||||||
|
public ServiceFactory(Func<Type, object?> factory, Func<Type, object?[], object> creator)
|
||||||
|
{
|
||||||
|
this.factory = factory;
|
||||||
|
this.creator = creator;
|
||||||
|
}
|
||||||
|
|
||||||
|
public T? Get<T>(Type type)
|
||||||
|
{
|
||||||
|
T? value = (T?)factory(type);
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public T Create<T>(Type type, params object?[] parameters)
|
||||||
|
{
|
||||||
|
dynamic? lookup = factory(typeof(IServiceCreator<>).MakeGenericType(type));
|
||||||
|
return lookup is not null ? (T)lookup.Create(creator, parameters) : (T)creator(type, parameters);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,25 @@
|
|||||||
|
using System.Reflection;
|
||||||
|
|
||||||
|
namespace Toolkit.Foundation
|
||||||
|
{
|
||||||
|
internal class ServiceFactoryDescriptor
|
||||||
|
{
|
||||||
|
private readonly IServiceFactory serviceFactory;
|
||||||
|
|
||||||
|
public ServiceFactoryDescriptor(IServiceFactory serviceFactory)
|
||||||
|
{
|
||||||
|
this.serviceFactory = serviceFactory;
|
||||||
|
}
|
||||||
|
|
||||||
|
public object? Create(Type type)
|
||||||
|
{
|
||||||
|
MethodInfo? methodInfo = typeof(ServiceFactoryDescriptor).GetMethod(nameof(Create), BindingFlags.NonPublic | BindingFlags.Instance);
|
||||||
|
return methodInfo?.MakeGenericMethod(type).Invoke(this, new object[] { type });
|
||||||
|
}
|
||||||
|
|
||||||
|
private T Create<T>(Type type)
|
||||||
|
{
|
||||||
|
return serviceFactory.Create<T>(type);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
namespace Toolkit.Foundation
|
||||||
|
{
|
||||||
|
public interface INamedDataTemplateFactory
|
||||||
|
{
|
||||||
|
object? Create(string name, params object[] parameters);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
namespace Toolkit.Foundation
|
||||||
|
{
|
||||||
|
public interface INamedTemplateFactory
|
||||||
|
{
|
||||||
|
object? Create(string name);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
|
||||||
|
namespace Toolkit.Foundation
|
||||||
|
{
|
||||||
|
public interface ITemplateBuilder
|
||||||
|
{
|
||||||
|
IReadOnlyCollection<ITemplateDescriptor> Descriptors { get; }
|
||||||
|
|
||||||
|
ITemplateBuilder Add<TViewModel, TView>(string name, ServiceLifetime lifetime = ServiceLifetime.Transient);
|
||||||
|
|
||||||
|
ITemplateBuilder Add<TViewModel, TView>(ServiceLifetime lifetime = ServiceLifetime.Transient);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
|
||||||
|
namespace Toolkit.Foundation
|
||||||
|
{
|
||||||
|
public interface ITemplateDescriptor
|
||||||
|
{
|
||||||
|
Type DataType { get; }
|
||||||
|
|
||||||
|
ServiceLifetime Lifetime { get; }
|
||||||
|
|
||||||
|
string? Name { get; }
|
||||||
|
|
||||||
|
Type TemplateType { get; }
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
namespace Toolkit.Foundation
|
||||||
|
{
|
||||||
|
|
||||||
|
public interface ITemplateDescriptorProvider
|
||||||
|
{
|
||||||
|
ITemplateDescriptor? Get(string name);
|
||||||
|
|
||||||
|
ITemplateDescriptor? Get(Type type);
|
||||||
|
|
||||||
|
ITemplateDescriptor? Get<T>();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
using System.Diagnostics.CodeAnalysis;
|
||||||
|
|
||||||
|
namespace Toolkit.Foundation
|
||||||
|
{
|
||||||
|
public interface ITemplateFactory
|
||||||
|
{
|
||||||
|
object? Create([MaybeNull] object? data);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
namespace Toolkit.Foundation
|
||||||
|
{
|
||||||
|
public interface ITemplateSelector
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
namespace Toolkit.Foundation
|
||||||
|
{
|
||||||
|
public interface ITypedDataTemplateFactory
|
||||||
|
{
|
||||||
|
object? Create(Type type, params object[] parameters);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,36 @@
|
|||||||
|
namespace Toolkit.Foundation
|
||||||
|
{
|
||||||
|
public class NamedDataTemplateFactory : INamedDataTemplateFactory
|
||||||
|
{
|
||||||
|
private readonly Dictionary<string, object> cache = new();
|
||||||
|
|
||||||
|
private readonly IReadOnlyCollection<ITemplateDescriptor> descriptors;
|
||||||
|
private readonly IServiceFactory serviceFactory;
|
||||||
|
|
||||||
|
public NamedDataTemplateFactory(IReadOnlyCollection<ITemplateDescriptor> descriptors,
|
||||||
|
IServiceFactory serviceFactory)
|
||||||
|
{
|
||||||
|
this.descriptors = descriptors;
|
||||||
|
this.serviceFactory = serviceFactory;
|
||||||
|
}
|
||||||
|
|
||||||
|
public virtual object? Create(string name, params object[] parameters)
|
||||||
|
{
|
||||||
|
if (cache.TryGetValue(name, out object? data))
|
||||||
|
{
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (descriptors.FirstOrDefault(x => x.Name == name) is ITemplateDescriptor descriptor)
|
||||||
|
{
|
||||||
|
data = parameters is { Length: > 0 } ? serviceFactory.Create<object>(descriptor.DataType, parameters) : serviceFactory.Get<object>(descriptor.DataType);
|
||||||
|
if (data is ICache cache)
|
||||||
|
{
|
||||||
|
this.cache[name] = cache;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,41 @@
|
|||||||
|
namespace Toolkit.Foundation
|
||||||
|
{
|
||||||
|
public class NamedTemplateFactory : INamedTemplateFactory
|
||||||
|
{
|
||||||
|
private readonly Dictionary<string, object> cache = new();
|
||||||
|
|
||||||
|
private readonly ITemplateDescriptorProvider provider;
|
||||||
|
private readonly IServiceFactory serviceFactory;
|
||||||
|
|
||||||
|
public NamedTemplateFactory(ITemplateDescriptorProvider provider,
|
||||||
|
IServiceFactory serviceFactory)
|
||||||
|
{
|
||||||
|
this.provider = provider;
|
||||||
|
this.serviceFactory = serviceFactory;
|
||||||
|
}
|
||||||
|
|
||||||
|
public virtual object? Create(string name)
|
||||||
|
{
|
||||||
|
if (cache.TryGetValue(name, out object? view))
|
||||||
|
{
|
||||||
|
return view;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (provider.Get(name) is ITemplateDescriptor descriptor)
|
||||||
|
{
|
||||||
|
view = serviceFactory.Get<object>(descriptor.TemplateType);
|
||||||
|
if (view is ICache cache)
|
||||||
|
{
|
||||||
|
this.cache[name] = cache;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (descriptor.GetType().GenericTypeArguments is { Length: 2 })
|
||||||
|
{
|
||||||
|
(descriptor as dynamic).ViewInvoker?.Invoke(view);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return view;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using System.Collections.ObjectModel;
|
||||||
|
|
||||||
|
namespace Toolkit.Foundation
|
||||||
|
{
|
||||||
|
public class TemplateBuilder : ITemplateBuilder
|
||||||
|
{
|
||||||
|
private readonly List<ITemplateDescriptor> descriptors = new();
|
||||||
|
|
||||||
|
public IReadOnlyCollection<ITemplateDescriptor> Descriptors => new ReadOnlyCollection<ITemplateDescriptor>(descriptors);
|
||||||
|
|
||||||
|
public ITemplateBuilder Add<TViewModel, TView>(string name, ServiceLifetime lifetime = ServiceLifetime.Transient)
|
||||||
|
{
|
||||||
|
descriptors.Add(new TemplateDescriptor(typeof(TViewModel), typeof(TView), name, lifetime));
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ITemplateBuilder Add<TViewModel, TView>(ServiceLifetime lifetime = ServiceLifetime.Transient)
|
||||||
|
{
|
||||||
|
descriptors.Add(new TemplateDescriptor(typeof(TViewModel), typeof(TView), null, lifetime));
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,26 @@
|
|||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
|
||||||
|
namespace Toolkit.Foundation
|
||||||
|
{
|
||||||
|
public class TemplateDescriptor : ITemplateDescriptor
|
||||||
|
{
|
||||||
|
public TemplateDescriptor(Type dataType,
|
||||||
|
Type templateType,
|
||||||
|
string? name = null,
|
||||||
|
ServiceLifetime lifetime = ServiceLifetime.Transient)
|
||||||
|
{
|
||||||
|
TemplateType = templateType;
|
||||||
|
DataType = dataType;
|
||||||
|
Name = name;
|
||||||
|
Lifetime = lifetime;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ServiceLifetime Lifetime { get; }
|
||||||
|
|
||||||
|
public Type TemplateType { get; }
|
||||||
|
|
||||||
|
public Type DataType { get; }
|
||||||
|
|
||||||
|
public string? Name { get; }
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,42 @@
|
|||||||
|
namespace Toolkit.Foundation
|
||||||
|
{
|
||||||
|
public class TemplateDescriptorProvider : ITemplateDescriptorProvider
|
||||||
|
{
|
||||||
|
private readonly IReadOnlyCollection<ITemplateDescriptor> descriptors;
|
||||||
|
|
||||||
|
public TemplateDescriptorProvider(IReadOnlyCollection<ITemplateDescriptor> descriptors)
|
||||||
|
{
|
||||||
|
this.descriptors = descriptors;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ITemplateDescriptor? Get(string name)
|
||||||
|
{
|
||||||
|
if (descriptors.FirstOrDefault(x => x.Name == name) is ITemplateDescriptor descriptor)
|
||||||
|
{
|
||||||
|
return descriptor;
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ITemplateDescriptor? Get(Type type)
|
||||||
|
{
|
||||||
|
if (descriptors.FirstOrDefault(x => x.DataType == type) is ITemplateDescriptor descriptor)
|
||||||
|
{
|
||||||
|
return descriptor;
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ITemplateDescriptor? Get<T>()
|
||||||
|
{
|
||||||
|
if (descriptors.FirstOrDefault(x => x.DataType == typeof(T)) is ITemplateDescriptor descriptor)
|
||||||
|
{
|
||||||
|
return descriptor;
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,43 @@
|
|||||||
|
using System.Diagnostics.CodeAnalysis;
|
||||||
|
|
||||||
|
namespace Toolkit.Foundation
|
||||||
|
{
|
||||||
|
public class TemplateFactory : ITemplateFactory
|
||||||
|
{
|
||||||
|
private readonly Dictionary<object, object> cache = new();
|
||||||
|
|
||||||
|
private readonly ITemplateDescriptorProvider provider;
|
||||||
|
private readonly IServiceFactory serviceFactory;
|
||||||
|
|
||||||
|
public TemplateFactory(ITemplateDescriptorProvider provider,
|
||||||
|
IServiceFactory serviceFactory)
|
||||||
|
{
|
||||||
|
this.provider = provider;
|
||||||
|
this.serviceFactory = serviceFactory;
|
||||||
|
}
|
||||||
|
|
||||||
|
public virtual object? Create([MaybeNull] object? data)
|
||||||
|
{
|
||||||
|
if (data is null)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (cache.TryGetValue(data, out object? template))
|
||||||
|
{
|
||||||
|
return template;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (provider.Get(data.GetType()) is ITemplateDescriptor descriptor)
|
||||||
|
{
|
||||||
|
template = serviceFactory.Get<object>(descriptor.TemplateType);
|
||||||
|
if (template is ICache cache)
|
||||||
|
{
|
||||||
|
this.cache[data] = cache;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return template;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,36 @@
|
|||||||
|
namespace Toolkit.Foundation
|
||||||
|
{
|
||||||
|
public class TypedDataTemplateFactory : ITypedDataTemplateFactory
|
||||||
|
{
|
||||||
|
private readonly Dictionary<Type, object> cache = new();
|
||||||
|
|
||||||
|
private readonly IReadOnlyCollection<ITemplateDescriptor> descriptors;
|
||||||
|
private readonly IServiceFactory serviceFactory;
|
||||||
|
|
||||||
|
public TypedDataTemplateFactory(IReadOnlyCollection<ITemplateDescriptor> descriptors,
|
||||||
|
IServiceFactory serviceFactory)
|
||||||
|
{
|
||||||
|
this.descriptors = descriptors;
|
||||||
|
this.serviceFactory = serviceFactory;
|
||||||
|
}
|
||||||
|
|
||||||
|
public virtual object? Create(Type type, params object[] parameters)
|
||||||
|
{
|
||||||
|
if (cache.TryGetValue(type, out object? data))
|
||||||
|
{
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (descriptors.FirstOrDefault(x => x.DataType == type) is ITemplateDescriptor descriptor)
|
||||||
|
{
|
||||||
|
data = parameters is { Length: > 0 } ? serviceFactory.Create<object>(descriptor.DataType, parameters) : serviceFactory.Get<object>(descriptor.DataType);
|
||||||
|
if (data is ICache cache)
|
||||||
|
{
|
||||||
|
this.cache[type] = cache;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
<Project Sdk="Microsoft.NET.Sdk">
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFramework>net7.0</TargetFramework>
|
<TargetFramework>net7.0</TargetFramework>
|
||||||
@@ -6,4 +6,14 @@
|
|||||||
<Nullable>enable</Nullable>
|
<Nullable>enable</Nullable>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="JsonPatch.Net" Version="2.0.4" />
|
||||||
|
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.1.0-preview1" />
|
||||||
|
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="7.0.0" />
|
||||||
|
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />
|
||||||
|
<PackageReference Include="Microsoft.Extensions.Hosting" Version="7.0.0" />
|
||||||
|
<PackageReference Include="Microsoft.Extensions.Options" Version="7.0.0" />
|
||||||
|
<PackageReference Include="Newtonsoft.Json" Version="13.0.2" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|||||||
Reference in New Issue
Block a user