Add foundation

This commit is contained in:
TheXamlGuy
2024-04-13 11:29:32 +01:00
parent 6f31aa8513
commit 053d8a851e
264 changed files with 3428 additions and 4683 deletions
+20
View File
@@ -0,0 +1,20 @@
using Microsoft.Extensions.Hosting;
namespace Toolkit.Foundation;
public class AppService(IEnumerable<IInitializer> initializers,
IPublisher publisher) :
IHostedService
{
public async Task StartAsync(CancellationToken cancellationToken)
{
foreach (IInitializer initializer in initializers)
{
await initializer.Initialize();
}
await publisher.Publish<Started>(cancellationToken);
}
public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask;
}
+23
View File
@@ -0,0 +1,23 @@
using System.Runtime.CompilerServices;
namespace Toolkit.Foundation;
public class AsyncLock(int initial = 1,
int maximum = 1) :
IDisposable
{
private readonly SemaphoreSlim semaphore = new(initial, maximum);
public void Dispose()
{
semaphore.Release();
}
public TaskAwaiter<AsyncLock> GetAwaiter() => LockAsync().GetAwaiter();
private async Task<AsyncLock> LockAsync()
{
await semaphore.WaitAsync();
return this;
}
}
+68
View File
@@ -0,0 +1,68 @@
using HyperX;
using System.Collections;
using System.Collections.Concurrent;
using System.Reactive.Disposables;
namespace Toolkit.Foundation;
public class Cache<TValue>(IDisposer disposer) :
ICache<TValue>
{
private readonly List<TValue> cache = [];
public void Add(TValue value)
{
if (value is null)
{
return;
}
disposer.Add(value, Disposable.Create(() => Remove(value)));
cache.Add(value);
}
public void Clear() => cache.Clear();
public IEnumerator<TValue> GetEnumerator() => cache.GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
public bool Remove(TValue value) => cache.Remove(value);
}
public class Cache<TKey, TValue> :
ICache<TKey, TValue>
where TKey :
notnull
where TValue :
notnull
{
private readonly ConcurrentDictionary<TKey, TValue> cache = new();
public void Add(TKey key,
TValue value)
{
cache.TryAdd(key, value);
}
public void Clear() => cache.Clear();
public bool ContainsKey(TKey key) => cache.ContainsKey(key);
public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator() => cache.GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
public bool Remove(TKey key) => cache.Remove(key, out _);
public bool TryGetValue(TKey key, out TValue? value)
{
if (cache.TryGetValue(key, out value))
{
return true;
}
value = default;
return false;
}
}
+3
View File
@@ -0,0 +1,3 @@
namespace Toolkit.Foundation;
public record Changed<TValue>(TValue? Value = default) : INotification;
@@ -0,0 +1,17 @@
using CommunityToolkit.Mvvm.Input;
namespace Toolkit.Foundation;
public partial class CommandValueViewModel<TValue>(IServiceProvider serviceProvider,
IServiceFactory serviceFactory,
IPublisher publisher,
ISubscriber subscriber,
IDisposer disposer) :
ValueViewModel<TValue>(serviceProvider, serviceFactory, publisher, subscriber, disposer)
{
public IRelayCommand InvokeCommand =>
new AsyncRelayCommand(InvokeAsync);
protected virtual Task InvokeAsync() =>
Task.CompletedTask;
}
+17
View File
@@ -0,0 +1,17 @@
using CommunityToolkit.Mvvm.Input;
namespace Toolkit.Foundation;
public partial class CommandViewModel(IServiceProvider serviceProvider,
IServiceFactory serviceFactory,
IPublisher publisher,
ISubscriber subscriber,
IDisposer disposer) :
ObservableViewModel(serviceProvider, serviceFactory, publisher, subscriber, disposer)
{
public IRelayCommand InvokeCommand =>
new AsyncRelayCommand(InvokeAsync);
protected virtual Task InvokeAsync() =>
Task.CompletedTask;
}
+91
View File
@@ -0,0 +1,91 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
namespace Toolkit.Foundation;
public class ComponentBuilder :
IComponentBuilder
{
private readonly IHostBuilder hostBuilder;
private bool configurationRegistered;
private ComponentBuilder()
{
hostBuilder = new HostBuilder()
.UseContentRoot("Local", true)
.ConfigureAppConfiguration(config =>
{
config.AddJsonFile("Settings.json", true, true);
})
.ConfigureServices((context, services) =>
{
services.AddScoped<IComponentHost, ComponentHost>();
services.AddScoped<IServiceFactory>(provider =>
new ServiceFactory((type, parameters) =>
ActivatorUtilities.CreateInstance(provider, type, parameters!)));
services.AddScoped<SubscriptionCollection>();
services.AddScoped<ISubscriptionManager, SubscriptionManager>();
services.AddTransient<ISubscriber, Subscriber>();
services.AddTransient<IPublisher, Publisher>();
services.AddTransient<IMediator, Mediator>();
services.AddScoped<IDisposer, Disposer>();
services.AddTransient<INavigationScope, NavigationScope>();
services.AddTransient<INavigationProvider, NavigationProvider>();
services.AddScoped<INavigationContextCollection, NavigationContextCollection>();
services.AddTransient<INavigationContextProvider, NavigationContextProvider>();
services.AddHandler<NavigateHandler>();
services.AddHandler<NavigateBackHandler>();
});
}
public static IComponentBuilder Create() =>
new ComponentBuilder();
public IComponentBuilder AddConfiguration<TConfiguration>(Action<TConfiguration> configurationDelegate)
where TConfiguration : ComponentConfiguration, new()
{
if (configurationRegistered)
{
return this;
}
configurationRegistered = true;
TConfiguration configuration = new();
if (configurationDelegate is not null)
{
configurationDelegate(configuration);
}
hostBuilder.ConfigureServices(services =>
{
services.AddConfiguration<ComponentConfiguration>(section: configuration.GetType().Name,
configuration: configuration);
services.AddConfiguration(configuration);
});
return this;
}
public IComponentHost Build()
{
IHost host = hostBuilder.Build();
return host.Services.GetRequiredService<IComponentHost>();
}
public IComponentBuilder AddServices(Action<IServiceCollection> configureDelegate)
{
hostBuilder.ConfigureServices(configureDelegate);
return this;
}
}
@@ -0,0 +1,13 @@
using System.Text.Json.Serialization;
namespace Toolkit.Foundation;
public record ComponentConfiguration
{
public string? Description { get; set; }
public string? Name { get; set; }
[JsonInclude]
internal Guid Id { get; set; } = Guid.NewGuid();
}
@@ -0,0 +1,111 @@
using CommunityToolkit.Mvvm.ComponentModel;
namespace Toolkit.Foundation;
public partial class ComponentConfigurationViewModel<TConfiguration, TValue, THeader, TDescription, TAction> :
ValueViewModel<TValue>,
IComponentConfigurationViewModel,
INotificationHandler<Changed<TConfiguration>>
where TConfiguration : class
{
public ComponentConfigurationViewModel(IServiceProvider serviceProvider,
IServiceFactory serviceFactory,
IPublisher publisher,
ISubscriber subscriber,
IDisposer disposer,
THeader header,
TDescription description,
TAction action) : base(serviceProvider, serviceFactory, publisher, subscriber, disposer)
{
}
public Task Handle(Changed<TConfiguration> args,
CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
}
}
public partial class ComponentConfigurationViewModel<TConfiguration, TValue, TAction>(IServiceProvider serviceProvider,
IServiceFactory serviceFactory,
IPublisher publisher,
ISubscriber subscriber,
IDisposer disposer,
TAction action,
TConfiguration configuration,
Func<TConfiguration, TValue> valueDelegate,
object header,
object description) :
ValueViewModel<TValue>(serviceProvider, serviceFactory, publisher, subscriber, disposer),
IComponentConfigurationViewModel,
INotificationHandler<Changed<TConfiguration>>
where TConfiguration : class
{
[ObservableProperty]
private TAction action = action;
[ObservableProperty]
private object header = header;
[ObservableProperty]
private object description = description;
public override Task Activated()
{
Value = valueDelegate.Invoke(configuration);
return base.Activated();
}
public Task Handle(Changed<TConfiguration> args,
CancellationToken cancellationToken = default)
{
if (args.Value is TConfiguration configuration)
{
Value = valueDelegate.Invoke(configuration);
}
return Task.CompletedTask;
}
}
public partial class ComponentConfigurationViewModel<TConfiguration, TValue, TDescription, TAction>(IServiceProvider serviceProvider,
IServiceFactory serviceFactory,
IPublisher publisher,
ISubscriber subscriber,
IDisposer disposer,
TAction action,
TDescription description,
TConfiguration configuration,
Func<TConfiguration, TValue> valueDelegate,
object header) :
ValueViewModel<TValue>(serviceProvider, serviceFactory, publisher, subscriber, disposer),
IComponentConfigurationViewModel,
INotificationHandler<Changed<TConfiguration>>
where TConfiguration : class
{
[ObservableProperty]
private TAction action = action;
[ObservableProperty]
private object header = header;
[ObservableProperty]
private TDescription description = description;
public override Task Activated()
{
Value = valueDelegate.Invoke(configuration);
return base.Activated();
}
public Task Handle(Changed<TConfiguration> args,
CancellationToken cancellationToken = default)
{
if (args.Value is TConfiguration configuration)
{
Value = valueDelegate.Invoke(configuration);
}
return Task.CompletedTask;
}
}
+41
View File
@@ -0,0 +1,41 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
namespace Toolkit.Foundation;
public sealed class ComponentHost(IServiceProvider services,
IEnumerable<IInitializer> initializers,
IEnumerable<IHostedService> hostedServices) :
IComponentHost
{
public IServiceProvider Services => services;
public ComponentConfiguration? Configuration =>
Services.GetService<ComponentConfiguration>();
public void Dispose()
{
}
public async Task StartAsync(CancellationToken cancellationToken = default)
{
foreach (IInitializer initializer in initializers)
{
await initializer.Initialize();
}
foreach (IHostedService service in hostedServices)
{
await service.StartAsync(cancellationToken);
}
}
public async Task StopAsync(CancellationToken cancellationToken = default)
{
foreach (IHostedService service in hostedServices)
{
await service.StopAsync(cancellationToken);
}
}
}
@@ -0,0 +1,21 @@
using System.Collections;
namespace Toolkit.Foundation;
public class ComponentHostCollection :
IComponentHostCollection
{
private readonly List<IComponentHost> hosts = [];
public void Add(IComponentHost host)
{
hosts.Add(host);
}
public IEnumerator<IComponentHost> GetEnumerator() =>
hosts.GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() =>
hosts.GetEnumerator();
}
@@ -0,0 +1,49 @@
using Microsoft.Extensions.DependencyInjection;
namespace Toolkit.Foundation;
public class ComponentInitializer(IEnumerable<IComponent> components,
IProxyServiceCollection<IComponentBuilder> typedServices,
IComponentHostCollection hosts,
IComponentScopeCollection scopes,
IServiceProvider provider) :
IInitializer
{
public async Task Initialize()
{
foreach (IComponent component in components)
{
IComponentBuilder builder = component.Create();
builder.AddServices(services =>
{
services.AddTransient(_ =>
provider.GetRequiredService<IProxyService<IPublisher>>());
services.AddTransient(_ =>
provider.GetRequiredService<IProxyService<IComponentHostCollection>>());
services.AddScoped(_ =>
provider.GetRequiredService<INavigationContextCollection>());
services.AddScoped(_ =>
provider.GetRequiredService<INavigationContextProvider>());
services.AddScoped(_ =>
provider.GetRequiredService<IComponentScopeCollection>());
services.AddTransient(_ =>
provider.GetRequiredService<IComponentScopeProvider>());
services.AddRange(typedServices.Services);
});
IComponentHost host = builder.Build();
scopes.Add(component.GetType().Name,
host.Services.GetRequiredService<IServiceProvider>());
hosts.Add(host);
await host.StartAsync();
}
}
}
@@ -0,0 +1,4 @@
namespace Toolkit.Foundation;
public class ComponentScopeCollection : Dictionary<string, IServiceProvider>,
IComponentScopeCollection;
@@ -0,0 +1,12 @@
namespace Toolkit.Foundation;
public class ComponentScopeProvider(IComponentScopeCollection scopes) :
IComponentScopeProvider
{
public IServiceProvider? Get(string name)
{
return scopes.TryGetValue(name,
out IServiceProvider? scope) ? scope : default;
}
}
+9
View File
@@ -0,0 +1,9 @@
namespace Toolkit.Foundation;
public class Configuration<TConfiguration>(IConfigurationReader<TConfiguration> reader) :
IConfiguration<TConfiguration>
where TConfiguration :
class
{
public TConfiguration Value => reader.Read();
}
@@ -0,0 +1,21 @@
namespace Toolkit.Foundation;
public class ConfigurationChangedHandler<TConfiguration, TValue>(ConfigurationValue<TConfiguration, TValue> configurationValue) :
INotificationHandler<Changed<TConfiguration>>
where TValue :
class, new()
{
public Task Handle(Changed<TConfiguration> args,
CancellationToken cancellationToken = default)
{
if (args.Value is TConfiguration configuration)
{
if (configurationValue.TryUpdate(configuration, out TValue value))
{
}
}
return Task.CompletedTask;
}
}
@@ -0,0 +1,9 @@
namespace Toolkit.Foundation;
public class ConfigurationFactory<TConfiguration>(Func<TConfiguration> factory) :
IConfigurationFactory<TConfiguration>
where TConfiguration :
class
{
public object Create() => factory.Invoke();
}
+11
View File
@@ -0,0 +1,11 @@
using Microsoft.Extensions.FileProviders;
namespace Toolkit.Foundation;
public class ConfigurationFile<TConfiguration>(IFileInfo fileInfo) :
IConfigurationFile<TConfiguration>
where TConfiguration :
class
{
public IFileInfo FileInfo => fileInfo;
}
@@ -0,0 +1,25 @@
namespace Toolkit.Foundation;
public class ConfigurationInitializer<TConfiguration>(IPublisher publisher,
IConfigurationReader<TConfiguration> reader,
IConfigurationWriter<TConfiguration> writer,
IConfigurationFactory<TConfiguration> factory) :
IConfigurationInitializer<TConfiguration>,
IInitializer
where TConfiguration :
class
{
public async Task Initialize()
{
if (!reader.TryRead(out TConfiguration? configuration))
{
if (factory.Create() is object defaultConfiguration)
{
configuration = (TConfiguration?)defaultConfiguration;
writer.Write(defaultConfiguration);
}
}
await publisher.PublishUI(new Changed<TConfiguration>(configuration));
}
}
@@ -0,0 +1,46 @@
namespace Toolkit.Foundation;
public class ConfigurationMonitor<TConfiguration>(IConfigurationFile<TConfiguration> file,
IConfigurationReader<TConfiguration> reader,
IPublisher publisher) :
IConfigurationMonitor<TConfiguration>
where TConfiguration :
class
{
private FileSystemWatcher? watcher;
public Task StartAsync(CancellationToken cancellationToken)
{
async void ChangedHandler(object sender,
FileSystemEventArgs args)
{
if (reader.Read() is { } configuration)
{
await publisher.PublishUI(new Changed<TConfiguration>(configuration));
}
}
if (file.FileInfo.PhysicalPath is { } path)
{
string fileName = Path.GetFileName(path);
watcher = new FileSystemWatcher
{
NotifyFilter = NotifyFilters.LastWrite,
Path = path.Replace(fileName, ""),
Filter = fileName,
EnableRaisingEvents = true
};
watcher.Changed += ChangedHandler;
}
return Task.CompletedTask;
}
public Task StopAsync(CancellationToken cancellationToken)
{
watcher?.Dispose();
return Task.CompletedTask;
}
}
+33
View File
@@ -0,0 +1,33 @@
namespace Toolkit.Foundation;
public class ConfigurationReader<TConfiguration>(IConfigurationSource<TConfiguration> source,
IConfigurationFactory<TConfiguration> factory) :
IConfigurationReader<TConfiguration>
where TConfiguration :
class
{
public TConfiguration Read()
{
if (source.TryGet(out TConfiguration? value))
{
if (value is TConfiguration configuration)
{
return configuration;
}
}
return (TConfiguration)factory.Create();
}
public bool TryRead(out TConfiguration? configuration)
{
if (source.TryGet(out TConfiguration? value) && value is not null)
{
configuration = value;
return true;
}
configuration = default;
return false;
}
}
+132
View File
@@ -0,0 +1,132 @@
using Microsoft.Extensions.FileProviders;
using System.Text.Encodings.Web;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace Toolkit.Foundation;
public class ConfigurationSource<TConfiguration>(IConfigurationFile<TConfiguration> configurationFile,
string section,
JsonSerializerOptions? serializerOptions = null) :
IConfigurationSource<TConfiguration>
where TConfiguration :
class
{
private readonly object lockingObject = new();
private static readonly Func<JsonSerializerOptions> defaultSerializerOptions = new(() =>
{
return new JsonSerializerOptions
{
WriteIndented = true,
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping,
Converters =
{
new JsonStringEnumConverter(),
new DictionaryStringObjectJsonConverter()
}
};
});
public void Set(TConfiguration value) => Set((object)value);
public void Set(object value)
{
lock (lockingObject)
{
IFileInfo fileInfo = configurationFile.FileInfo;
if (!File.Exists(fileInfo.PhysicalPath))
{
string? fileDirectoryPath = Path.GetDirectoryName(fileInfo.PhysicalPath);
if (!string.IsNullOrEmpty(fileDirectoryPath))
{
Directory.CreateDirectory(fileDirectoryPath);
}
File.WriteAllText(fileInfo.PhysicalPath!, "{}");
}
static Stream OpenReadWrite(IFileInfo fileInfo)
{
return fileInfo.PhysicalPath is not null
? new FileStream(fileInfo.PhysicalPath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite)
: fileInfo.CreateReadStream();
}
using Stream stream = OpenReadWrite(fileInfo);
using StreamReader? reader = new(stream);
string? content = reader.ReadToEnd();
using JsonDocument jsonDocument = JsonDocument.Parse(content);
using Stream stream2 = OpenReadWrite(fileInfo);
Utf8JsonWriter writer = new(stream2, new JsonWriterOptions()
{
Indented = true,
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping
});
writer.WriteStartObject();
bool isWritten = false;
JsonDocument optionsElement = JsonDocument.Parse(JsonSerializer.SerializeToUtf8Bytes(value,
serializerOptions ?? defaultSerializerOptions()));
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();
stream2.SetLength(stream2.Position);
}
}
public bool TryGet(out TConfiguration? value)
{
lock (lockingObject)
{
IFileInfo fileInfo = configurationFile.FileInfo;
if (File.Exists(fileInfo.PhysicalPath))
{
static Stream OpenRead(IFileInfo fileInfo)
{
return fileInfo.PhysicalPath is not null
? new FileStream(fileInfo.PhysicalPath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite)
: fileInfo.CreateReadStream();
}
using Stream stream = OpenRead(fileInfo);
using StreamReader? reader = new(stream);
string? content = reader.ReadToEnd();
using JsonDocument jsonDocument = JsonDocument.Parse(content);
if (jsonDocument.RootElement.TryGetProperty(section, out JsonElement sectionValue))
{
value = JsonSerializer.Deserialize<TConfiguration>(sectionValue.ToString(), serializerOptions ?? defaultSerializerOptions());
return true;
}
}
value = default;
return false;
}
}
}
+25
View File
@@ -0,0 +1,25 @@
namespace Toolkit.Foundation;
public class ConfigurationValue<TConfiguration, TValue>(Func<TConfiguration, Action<TValue>> changed)
where TValue :
class, new()
{
private TValue? currentValue;
public bool TryUpdate(TConfiguration configuration,
out TValue value)
{
TValue newValue = new();
changed(configuration).Invoke(newValue);
if (!EqualityComparer<TValue>.Default.Equals(currentValue, newValue))
{
value = newValue;
currentValue = newValue;
return true;
}
value = currentValue!;
return false;
}
}
+23
View File
@@ -0,0 +1,23 @@
namespace Toolkit.Foundation;
public class ConfigurationWriter<TConfiguration>(IConfigurationSource<TConfiguration> source) :
IConfigurationWriter<TConfiguration>
where TConfiguration :
class
{
public void Write(Action<TConfiguration> updateDelegate)
{
if (source.TryGet(out TConfiguration? value))
{
if (value is not null)
{
updateDelegate?.Invoke(value);
Write(value);
}
}
}
public void Write(object value) => source.Set(value);
public void Write(TConfiguration value) => source.Set(value);
}
@@ -0,0 +1,16 @@
namespace Toolkit.Foundation;
public class ContentTemplateDescriptor(object key,
Type viewModelType,
Type viewType,
params object[]? parameters) :
IContentTemplateDescriptor
{
public object Key => key;
public object[]? Parameters => parameters;
public Type ContentType => viewModelType;
public Type TemplateType => viewType;
}
@@ -0,0 +1,18 @@
using System.Xml.Linq;
namespace Toolkit.Foundation;
public class ContentTemplateDescriptorProvider(IEnumerable<IContentTemplateDescriptor> contentTemplates) :
IContentTemplateDescriptorProvider
{
public IContentTemplateDescriptor? Get(object key)
{
if (contentTemplates.FirstOrDefault(x => x.Key.Equals(key) || x.Key.Equals($"{key}".Replace("ViewModel", "")))
is IContentTemplateDescriptor viewModelTemplate)
{
return viewModelTemplate;
}
return default;
}
}
+5
View File
@@ -0,0 +1,5 @@
namespace Toolkit.Foundation;
public record Create<TValue>(TValue Value) :
INotification;
+68
View File
@@ -0,0 +1,68 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Toolkit.Foundation;
namespace Toolkit.Foundation;
public class DefaultBuilder :
HostBuilder
{
public static IHostBuilder Create()
{
return new HostBuilder()
.UseContentRoot("Local", true)
.ConfigureAppConfiguration(config =>
{
config.AddJsonFile("Settings.json", true, true);
})
.ConfigureServices((context, services) =>
{
services.AddScoped<IServiceFactory>(provider =>
new ServiceFactory((type, parameters) => ActivatorUtilities.CreateInstance(provider, type, parameters!)));
services.AddSingleton<IComponentHostCollection,
ComponentHostCollection>();
services.AddScoped<SubscriptionCollection>();
services.AddScoped<ISubscriptionManager, SubscriptionManager>();
services.AddTransient<ISubscriber, Subscriber>();
services.AddTransient<IPublisher, Publisher>();
services.AddScoped<IMediator, Mediator>();
services.AddScoped<IProxyService<IPublisher>>(provider =>
new ProxyService<IPublisher>(provider.GetRequiredService<IPublisher>()));
services.AddScoped<IProxyService<INavigationContextProvider>>(provider =>
new ProxyService<INavigationContextProvider>(provider.GetRequiredService<INavigationContextProvider>()));
services.AddScoped<IProxyService<IComponentHostCollection>>(provider =>
new ProxyService<IComponentHostCollection>(provider.GetRequiredService<IComponentHostCollection>()));
services.AddScoped<IDisposer, Disposer>();
services.AddTransient<IContentTemplateDescriptorProvider, ContentTemplateDescriptorProvider>();
services.AddTransient<INavigationProvider, NavigationProvider>();
services.AddScoped<INavigationContextCollection, NavigationContextCollection>();
services.AddTransient<INavigationContextProvider, NavigationContextProvider>();
services.AddTransient<INavigationScope, NavigationScope>();
services.AddScoped<IComponentScopeCollection, ComponentScopeCollection>(provider => new ComponentScopeCollection
{
{ "Default", provider.GetRequiredService<IServiceProvider>() }
});
services.AddTransient<IComponentScopeProvider, ComponentScopeProvider>();
services.AddHandler<NavigateHandler>();
services.AddHandler<NavigateBackHandler>();
services.AddInitializer<ComponentInitializer>();
services.AddHostedService<AppService>();
});
}
}
@@ -0,0 +1,84 @@
using System.Text.Json;
using System.Text.Json.Serialization;
namespace Toolkit.Foundation;
public class DictionaryStringObjectJsonConverter :
JsonConverter<Dictionary<string, object?>>
{
public override bool CanConvert(Type typeToConvert) =>
typeToConvert == typeof(Dictionary<string, object>) ||
typeToConvert == typeof(Dictionary<string, object?>);
public override Dictionary<string, object?> Read(ref Utf8JsonReader reader,
Type typeToConvert,
JsonSerializerOptions options)
{
Dictionary<string, object?> dictionary = [];
if (reader.TokenType == JsonTokenType.StartObject)
{
while (reader.Read())
{
if (reader.TokenType == JsonTokenType.EndObject)
{
return dictionary;
}
if (reader.TokenType is JsonTokenType.PropertyName)
{
string? propertyName = reader.GetString();
if (!string.IsNullOrWhiteSpace(propertyName))
{
reader.Read();
dictionary.Add(propertyName!, ExtractValue(ref reader, options));
}
}
}
}
return dictionary;
}
public override void Write(Utf8JsonWriter writer,
Dictionary<string, object?> value,
JsonSerializerOptions options) =>
JsonSerializer.Serialize(writer, (IDictionary<string, object?>)value, options);
private object? ExtractValue(ref Utf8JsonReader reader,
JsonSerializerOptions options)
{
switch (reader.TokenType)
{
case JsonTokenType.String:
if (reader.TryGetDateTime(out var date))
{
return date;
}
return reader.GetString();
case JsonTokenType.False:
return false;
case JsonTokenType.True:
return true;
case JsonTokenType.Null:
return null;
case JsonTokenType.Number:
if (reader.TryGetInt64(out var result))
{
return result;
}
return reader.GetDecimal();
case JsonTokenType.StartObject:
return Read(ref reader, null!, options);
case JsonTokenType.StartArray:
List<object?> list = [];
while (reader.Read() && reader.TokenType != JsonTokenType.EndArray)
{
list.Add(ExtractValue(ref reader, options));
}
return list;
default:
return default;
}
}
}
+81
View File
@@ -0,0 +1,81 @@
using System.Reactive.Disposables;
using System.Collections;
using System.Collections.Concurrent;
namespace Toolkit.Foundation;
public class Disposer :
IDisposer
{
private readonly ConcurrentDictionary<object, CompositeDisposable> subjects = [];
public void Add(object subject,
params object[] objects)
{
CompositeDisposable disposables = subjects.GetOrAdd(subject, args => new CompositeDisposable());
foreach (IDisposable disposable in objects.OfType<IDisposable>())
{
disposables.Add(disposable);
}
foreach (object notDisposable in objects.Where(x => x is not IDisposable))
{
disposables.Add(Disposable.Create(() => FromNotDisposable(notDisposable)));
}
}
private void FromNotDisposable(object target)
{
if (target is IList collection && collection is { Count: > 0 })
{
foreach (object? item in collection)
{
FromNotDisposable(item);
}
}
if (target is IDisposable disposable)
{
disposable.Dispose();
}
if (target is not IDisposable)
{
Dispose(target);
}
}
public TDisposable Replace<TDisposable>(object subject,
IDisposable disposer,
TDisposable replacement)
where TDisposable :
IDisposable
{
CompositeDisposable disposables = subjects.GetOrAdd(subject, args => new CompositeDisposable());
if (disposer is not null)
{
disposables.Remove(disposer);
}
disposables.Add(replacement);
return replacement;
}
public void Remove(object subject,
IDisposable disposer)
{
CompositeDisposable disposables = subjects.GetOrAdd(subject, args => new CompositeDisposable());
if (disposer is not null)
{
disposables.Remove(disposer);
}
}
public void Dispose(object subject)
{
if (subjects.TryGetValue(subject, out CompositeDisposable? disposables))
{
disposables?.Dispose();
}
}
}
+3
View File
@@ -0,0 +1,3 @@
namespace Toolkit.Foundation;
public record Enumerate<TValue>(object? Key = null) : INotification;
+5
View File
@@ -0,0 +1,5 @@
namespace Toolkit.Foundation;
public delegate Task<TResponse> HandlerDelegate<TMessage, TResponse>(TMessage message,
CancellationToken cancellationToken)
where TMessage : IMessage;
+25
View File
@@ -0,0 +1,25 @@
namespace Toolkit.Foundation;
public class HandlerWrapper<TMessage, TReply>(IHandler<TMessage, TReply> handler,
IEnumerable<IPipelineBehavior<TMessage, TReply>> pipelineBehaviours)
where TMessage : class, IRequest<TReply>
{
private readonly IEnumerable<IPipelineBehavior<TMessage, TReply>> pipelineBehaviours =
pipelineBehaviours.Reverse();
public async Task<TReply> Handle(TMessage message,
CancellationToken cancellationToken)
{
HandlerDelegate<TMessage, TReply> currentHandler = handler.Handle;
foreach (IPipelineBehavior<TMessage, TReply> behavior in pipelineBehaviours)
{
HandlerDelegate<TMessage, TReply> previousHandler = currentHandler;
currentHandler = async (args, token) =>
{
return await behavior.Handle(args, previousHandler, token);
};
}
return await currentHandler(message, cancellationToken);
}
}
+11
View File
@@ -0,0 +1,11 @@
namespace Toolkit.Foundation;
public interface IActivated
{
Task Activated();
}
public interface IActivated<TResult>
{
Task Activated(TResult result);
}
+30
View File
@@ -0,0 +1,30 @@
namespace Toolkit.Foundation;
public interface ICache<TValue> :
IEnumerable<TValue>
{
void Add(TValue value);
void Clear();
bool Remove(TValue value);
}
public interface ICache<TKey, TValue> :
IEnumerable<KeyValuePair<TKey, TValue>>
where TKey :
notnull
where TValue :
notnull
{
void Add(TKey key,
TValue value);
void Clear();
bool ContainsKey(TKey key);
bool Remove(TKey key);
bool TryGetValue(TKey key, out TValue? value);
}
+6
View File
@@ -0,0 +1,6 @@
namespace Toolkit.Foundation;
public interface IComponent
{
IComponentBuilder Create();
}
+13
View File
@@ -0,0 +1,13 @@
using Microsoft.Extensions.DependencyInjection;
namespace Toolkit.Foundation;
public interface IComponentBuilder
{
IComponentBuilder AddConfiguration<TConfiguration>(Action<TConfiguration> configurationDelegate)
where TConfiguration : ComponentConfiguration, new();
IComponentHost Build();
IComponentBuilder AddServices(Action<IServiceCollection> configureDelegate);
}
@@ -0,0 +1,6 @@
namespace Toolkit.Foundation;
public interface IComponentConfigurationViewModel
{
}
+9
View File
@@ -0,0 +1,9 @@
using Microsoft.Extensions.Hosting;
namespace Toolkit.Foundation;
public interface IComponentHost :
IHost
{
ComponentConfiguration? Configuration { get; }
}
@@ -0,0 +1,7 @@
namespace Toolkit.Foundation;
public interface IComponentHostCollection :
IEnumerable<IComponentHost>
{
void Add(IComponentHost host);
}
@@ -0,0 +1,5 @@
namespace Toolkit.Foundation;
public interface IComponentScopeCollection :
IDictionary<string, IServiceProvider>;
@@ -0,0 +1,7 @@
namespace Toolkit.Foundation;
public interface IComponentScopeProvider
{
IServiceProvider? Get(string name);
}
+8
View File
@@ -0,0 +1,8 @@
namespace Toolkit.Foundation;
public interface IConfiguration<out TConfiguration>
where TConfiguration :
class
{
TConfiguration Value { get; }
}
@@ -0,0 +1,4 @@
namespace Toolkit.Foundation;
public interface IConfigurationChanged<TConfiguration> :
IInitializer;
@@ -0,0 +1,8 @@
namespace Toolkit.Foundation;
public interface IConfigurationFactory<TConfiguration>
where TConfiguration :
class
{
object Create();
}
+10
View File
@@ -0,0 +1,10 @@
using Microsoft.Extensions.FileProviders;
namespace Toolkit.Foundation;
public interface IConfigurationFile<TConfiguration>
where TConfiguration :
class
{
IFileInfo FileInfo { get; }
}
@@ -0,0 +1,8 @@
namespace Toolkit.Foundation;
public interface IConfigurationInitializer<TConfiguration>
where TConfiguration :
class
{
Task Initialize();
}
@@ -0,0 +1,9 @@
using Microsoft.Extensions.Hosting;
namespace Toolkit.Foundation;
public interface IConfigurationMonitor<TConfiguration> :
IHostedService
where TConfiguration :
class;
@@ -0,0 +1,10 @@
namespace Toolkit.Foundation;
public interface IConfigurationReader<TConfiguration>
where TConfiguration :
class
{
bool TryRead(out TConfiguration? configuration);
TConfiguration Read();
}
@@ -0,0 +1,12 @@
namespace Toolkit.Foundation;
public interface IConfigurationSource<TConfiguration>
where TConfiguration :
class
{
bool TryGet(out TConfiguration? value);
void Set(TConfiguration value);
void Set(object value);
}
@@ -0,0 +1,12 @@
namespace Toolkit.Foundation;
public interface IConfigurationWriter<TConfiguration>
where TConfiguration :
class
{
void Write(Action<TConfiguration> updateDelegate);
void Write(object value);
void Write(TConfiguration value);
}
+6
View File
@@ -0,0 +1,6 @@
namespace Toolkit.Foundation;
public interface IContentTemplate
{
}
@@ -0,0 +1,10 @@
namespace Toolkit.Foundation;
public interface IContentTemplateDescriptor
{
object Key { get; }
Type ContentType { get; }
Type TemplateType { get; }
}
@@ -0,0 +1,6 @@
namespace Toolkit.Foundation;
public interface IContentTemplateDescriptorProvider
{
IContentTemplateDescriptor? Get(object key);
}
+8
View File
@@ -0,0 +1,8 @@
namespace Toolkit.Foundation;
public interface IDeactivatable
{
public event EventHandler? DeactivateHandler;
public Task Deactivate();
}
+6
View File
@@ -0,0 +1,6 @@
namespace Toolkit.Foundation;
public interface IDeactivated
{
Task Deactivated();
}
+11
View File
@@ -0,0 +1,11 @@
namespace Toolkit.Foundation;
public interface IDeactivating
{
Task Deactivating();
}
public interface IDeactivating<TResult>
{
Task<TResult> Deactivating();
}
+6
View File
@@ -0,0 +1,6 @@
namespace Toolkit.Foundation;
public interface IDispatcher
{
Task InvokeAsync(Action action);
}
+16
View File
@@ -0,0 +1,16 @@
namespace Toolkit.Foundation;
public interface IDisposer
{
void Add(object subject,
params object[] objects);
TDisposable Replace<TDisposable>(object subject,
IDisposable disposer,
TDisposable replacement) where TDisposable : IDisposable;
void Remove(object subject,
IDisposable disposer);
void Dispose(object subject);
}
+12
View File
@@ -0,0 +1,12 @@
namespace Toolkit.Foundation;
public interface IFactory<TParameter, TService>
{
TService? Create(TParameter value);
}
public interface IFactory<TService>
{
TService? Create();
}
+17
View File
@@ -0,0 +1,17 @@
namespace Toolkit.Foundation;
public interface IHandler;
public interface IHandler<in TRequest, TResponse> :
IHandler
where TRequest :
IRequest<TResponse>
{
Task<TResponse> Handle(TRequest args,
CancellationToken cancellationToken);
}
public interface IHandler<in TRequest> :
IHandler<TRequest, Unit>
where TRequest :
IRequest<Unit>;
@@ -0,0 +1,19 @@
using Microsoft.Extensions.Hosting;
namespace Toolkit.Foundation;
public static class IHostBuilderExtensions
{
public static IHostBuilder UseContentRoot(this IHostBuilder hostBuilder,
string contentRoot,
bool createDirectory)
{
if (createDirectory)
{
Directory.CreateDirectory(contentRoot);
}
hostBuilder.UseContentRoot(contentRoot);
return hostBuilder;
}
}
+6
View File
@@ -0,0 +1,6 @@
namespace Toolkit.Foundation;
public interface IInitializer
{
Task Initialize();
}
+10
View File
@@ -0,0 +1,10 @@
namespace Toolkit.Foundation;
public interface IMediator
{
Task<TResponse?> SendAsync<TResponse>(IRequest<TResponse> request,
CancellationToken cancellationToken = default);
Task<object?> SendAsync(object message, CancellationToken
cancellationToken = default);
}
+3
View File
@@ -0,0 +1,3 @@
namespace Toolkit.Foundation;
public interface IMessage;
@@ -0,0 +1,5 @@
namespace Toolkit.Foundation;
public interface INavigateBackHandler<TNavigation> :
INotificationHandler<NavigateBack<TNavigation>>,
INavigateHandler;
+7
View File
@@ -0,0 +1,7 @@
namespace Toolkit.Foundation;
public interface INavigateHandler;
public interface INavigateHandler<TNavigation> :
INotificationHandler<Navigate<TNavigation>>,
INavigateHandler;
+7
View File
@@ -0,0 +1,7 @@
namespace Toolkit.Foundation;
public interface INavigation
{
Type Type { get; set; }
}
@@ -0,0 +1,4 @@
namespace Toolkit.Foundation;
public interface INavigationContextCollection :
IDictionary<object, object?>;
@@ -0,0 +1,9 @@
namespace Toolkit.Foundation;
public interface INavigationContextProvider
{
object? Get(object key);
bool TryGet(object key,
out object? value);
}
@@ -0,0 +1,6 @@
namespace Toolkit.Foundation;
public interface INavigationProvider
{
INavigation? Get(Type type);
}
+10
View File
@@ -0,0 +1,10 @@
namespace Toolkit.Foundation;
public interface INavigationScope
{
Task NavigateAsync(string route, object? sender = null, object? context = null,
EventHandler? navigated = null, object[]? parameters = null, CancellationToken cancellationToken = default);
Task NavigateBackAsync(object? context, CancellationToken cancellationToken = default);
}
@@ -0,0 +1,7 @@
namespace Toolkit.Foundation;
public interface INavigationViewModel :
IObservableViewModel
{
string Text { get; set; }
}
+4
View File
@@ -0,0 +1,4 @@
namespace Toolkit.Foundation;
public interface INotification :
IMessage;
@@ -0,0 +1,10 @@
namespace Toolkit.Foundation;
public interface INotificationHandler<in TNotification> :
IHandler
where TNotification :
INotification
{
Task Handle(TNotification args,
CancellationToken cancellationToken = default);
}
@@ -0,0 +1,4 @@
namespace Toolkit.Foundation;
public interface IObservableCollectionViewModel<TItem> :
IObservableViewModel;
@@ -0,0 +1,13 @@
namespace Toolkit.Foundation;
public interface IObservableViewModel :
IDisposable
{
public IDisposer Disposer { get; }
public IPublisher Publisher { get; }
public IServiceFactory ServiceFactory { get; }
public IServiceProvider ServiceProvider { get; }
}
+17
View File
@@ -0,0 +1,17 @@
namespace Toolkit.Foundation;
public interface IPipelineBehavior<TMessage, TResponse>
where TMessage : IMessage
{
Task<TResponse> Handle(TMessage message,
HandlerDelegate<TMessage, TResponse> next,
CancellationToken cancellationToken = default);
}
public interface IPipelineBehavior<TNotification>
where TNotification : INotification
{
Task Handle(TNotification notification,
NotificationHandlerDelegate<TNotification> next,
CancellationToken cancellationToken = default);
}
@@ -0,0 +1,12 @@
namespace Toolkit.Foundation;
public interface IPrimaryConfirmation
{
Task<bool> Confirm();
}
public interface IConfirmation
{
Task<bool> Confirm();
}
+11
View File
@@ -0,0 +1,11 @@
namespace Toolkit.Foundation;
public interface IProvider<TParameter, TService>
{
TService? Get(TParameter value);
}
public interface IProvider<TService>
{
TService? Get();
}
+6
View File
@@ -0,0 +1,6 @@
namespace Toolkit.Foundation;
public interface IProxyService<TService>
{
TService Proxy { get; }
}
@@ -0,0 +1,8 @@
using Microsoft.Extensions.DependencyInjection;
namespace Toolkit.Foundation;
public interface IProxyServiceCollection<T>
{
IServiceCollection Services { get; }
}
+58
View File
@@ -0,0 +1,58 @@
namespace Toolkit.Foundation;
public interface IPublisher
{
public Task Publish<TNotification>(object key,
CancellationToken cancellationToken = default)
where TNotification :
INotification,
new();
public Task Publish<TNotification>(TNotification notification,
CancellationToken cancellationToken = default)
where TNotification :
INotification;
public Task Publish<TNotification>(TNotification notification,
object key,
CancellationToken cancellationToken = default)
where TNotification :
INotification;
Task PublishUI<TNotification>(TNotification notification,
object key,
CancellationToken cancellationToken = default)
where TNotification :
INotification;
Task PublishUI<TNotification>(object key,
CancellationToken cancellationToken = default)
where TNotification :
INotification,
new();
Task PublishUI<TNotification>(TNotification notification,
CancellationToken cancellationToken = default)
where TNotification :
INotification;
Task PublishUI(object notification,
CancellationToken cancellationToken = default);
Task Publish(object notification,
Func<Func<Task>, Task> marshal,
object? key = null,
CancellationToken cancellationToken = default);
Task PublishUIAsync<TNotification>(CancellationToken cancellationToken = default)
where TNotification :
INotification,
new();
Task Publish<TNotification>(CancellationToken cancellationToken = default)
where TNotification :
INotification,
new();
public Task Publish(object notification, CancellationToken cancellationToken = default);
}
+6
View File
@@ -0,0 +1,6 @@
namespace Toolkit.Foundation;
public interface IRequest<out TResponse> :
IMessage;
public interface IRequest : IRequest<Unit>;
@@ -0,0 +1,6 @@
namespace Toolkit.Foundation;
public interface ISecondaryConfirmation
{
Task<bool> Confirm();
}
@@ -0,0 +1,244 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.FileProviders;
using Microsoft.Extensions.FileProviders.Physical;
using Microsoft.Extensions.Hosting;
using System;
using System.Text.Json;
using Toolkit.Foundation;
namespace Toolkit.Foundation;
public static class IServiceCollectionExtensions
{
public static IServiceCollection AddCache<TKey, TValue>(this IServiceCollection services)
where TKey : notnull
where TValue : notnull
{
services.AddScoped<ICache<TKey, TValue>, Cache<TKey, TValue>>();
services.AddTransient(provider => provider.GetService<ICache<TKey, TValue>>()!.Select(x => x.Value));
return services;
}
public static IServiceCollection AddCache<TValue>(this IServiceCollection services)
{
services.AddSingleton<ICache<TValue>, Cache<TValue>>();
services.AddTransient(provider => provider.GetService<ICache<TValue>>()!.Select(x => x));
return services;
}
public static IServiceCollection AddComponent<TComponent>(this IServiceCollection services)
where TComponent : class,
IComponent
{
services.AddTransient<IComponent, TComponent>();
return services;
}
public static IServiceCollection AddConfiguration<TConfiguration,
TValue>(this IServiceCollection services,
Func<TConfiguration, Action<TValue>> changed)
where TConfiguration : class
where TValue : class, new()
{
services.AddSingleton(new ConfigurationValue<TConfiguration, TValue>(changed));
services.AddHandler<ConfigurationChangedHandler<TConfiguration, TValue>>();
return services;
}
public static IServiceCollection AddConfiguration<TConfiguration>(this IServiceCollection services)
where TConfiguration : class =>
services.AddConfiguration<TConfiguration>(typeof(TConfiguration).Name, "Settings.json", null);
public static IServiceCollection AddConfiguration<TConfiguration>(this IServiceCollection services,
Action<TConfiguration> configurationDelegate)
where TConfiguration : class, new()
{
TConfiguration configuration = new();
configurationDelegate.Invoke(configuration);
return services.AddConfiguration(typeof(TConfiguration).Name, "Settings.json", configuration);
}
public static IServiceCollection AddConfiguration<TConfiguration>(this IServiceCollection services,
TConfiguration configuration)
where TConfiguration : class =>
services.AddConfiguration(configuration.GetType().Name, "Settings.json", configuration);
public static IServiceCollection AddConfiguration<TConfiguration>(this IServiceCollection services,
object configuration)
where TConfiguration : class =>
services.AddConfiguration(configuration.GetType().Name,
"Settings.json", (TConfiguration?)configuration);
public static IServiceCollection AddConfiguration<TConfiguration>(this IServiceCollection services,
string section,
string path = "Settings.json",
TConfiguration? configuration = null,
Action<JsonSerializerOptions>? serializerDelegate = null)
where TConfiguration : class
{
services.AddSingleton<IConfigurationSource<TConfiguration>>(provider =>
{
JsonSerializerOptions? defaultSerializer = null;
if (serializerDelegate is not null)
{
defaultSerializer = new JsonSerializerOptions();
serializerDelegate.Invoke(defaultSerializer);
}
return new ConfigurationSource<TConfiguration>(provider.GetRequiredService<IConfigurationFile<TConfiguration>>(),
section, defaultSerializer);
});
services.AddSingleton<IConfigurationFile<TConfiguration>>(provider =>
{
IFileInfo? fileInfo = null;
if (provider.GetService<IHostEnvironment>() is IHostEnvironment hostEnvironment)
{
IFileProvider fileProvider = hostEnvironment.ContentRootFileProvider;
fileInfo = fileProvider.GetFileInfo(path);
}
fileInfo ??= new PhysicalFileInfo(new FileInfo(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, path)));
return new ConfigurationFile<TConfiguration>(fileInfo);
});
services.AddHostedService<ConfigurationMonitor<TConfiguration>>();
services.AddSingleton<IConfigurationReader<TConfiguration>, ConfigurationReader<TConfiguration>>();
services.AddSingleton<IConfigurationWriter<TConfiguration>, ConfigurationWriter<TConfiguration>>();
services.AddTransient<IConfigurationFactory<TConfiguration>>(provider => new ConfigurationFactory<TConfiguration>(() =>
configuration ?? provider.GetRequiredService<TConfiguration>()));
services.AddTransient<IInitializer, ConfigurationInitializer<TConfiguration>>();
services.AddTransient<IConfigurationInitializer<TConfiguration>, ConfigurationInitializer<TConfiguration>>();
services.AddTransient<IWritableConfiguration<TConfiguration>, WritableConfiguration<TConfiguration>>();
services.AddTransient<IConfiguration<TConfiguration>, Configuration<TConfiguration>>();
services.AddTransient(provider => provider.GetRequiredService<IConfiguration<TConfiguration>>().Value);
return services;
}
public static IServiceCollection AddTemplate<TViewModel, TView>(this IServiceCollection services,
object? key = null,
params object[]? parameters)
{
Type viewModelType = typeof(TViewModel);
Type viewType = typeof(TView);
key ??= viewModelType.Name.Replace("ViewModel", "");
services.AddTransient(viewModelType, provider =>
provider.GetRequiredService<IServiceFactory>().Create<TViewModel>(parameters)!);
services.AddTransient(viewType);
services.AddKeyedTransient(viewModelType, key, (provider, key) =>
provider.GetRequiredService<IServiceFactory>().Create<TViewModel>(parameters)!);
services.AddKeyedTransient(viewType, key);
services.AddTransient<IContentTemplateDescriptor>(provider =>
new ContentTemplateDescriptor(key, viewModelType, viewType, parameters));
return services;
}
public static IServiceCollection AddHandler<THandler>(this IServiceCollection services,
ServiceLifetime lifetime = ServiceLifetime.Transient)
where THandler : IHandler
{
if (typeof(THandler).GetInterfaces() is Type[] contracts)
{
foreach (Type contract in contracts)
{
if (contract.Name == typeof(INotificationHandler<>).Name &&
contract.GetGenericArguments() is { Length: 1 } notificationHandlerArguments)
{
Type notificationType = notificationHandlerArguments[0];
Type wrapperType = typeof(NotificationHandlerWrapper<>)
.MakeGenericType(notificationType);
services.TryAdd(new ServiceDescriptor(typeof(INotificationHandler<>)
.MakeGenericType(notificationType), typeof(THandler), lifetime));
services.Add(new ServiceDescriptor(wrapperType, provider =>
provider.GetService<IServiceFactory>()?.Create(wrapperType,
provider.GetRequiredService(typeof(INotificationHandler<>).MakeGenericType(notificationType)),
provider.GetServices(typeof(IPipelineBehavior<>)
.MakeGenericType(notificationType)))!, lifetime));
}
if (contract.Name == typeof(IHandler<,>).Name &&
contract.GetGenericArguments() is { Length: 2 } handlerArguments)
{
Type requestType = handlerArguments[0];
Type responseType = handlerArguments[1];
Type wrapperType = typeof(HandlerWrapper<,>)
.MakeGenericType(requestType, responseType);
services.TryAdd(new ServiceDescriptor(typeof(THandler),
typeof(THandler), lifetime));
services.Add(new ServiceDescriptor(wrapperType, provider =>
provider.GetService<IServiceFactory>()?.Create(wrapperType,
provider.GetRequiredService<THandler>(),
provider.GetServices(typeof(IPipelineBehavior<,>)
.MakeGenericType(requestType, responseType)))!, lifetime));
}
}
return services;
}
return services;
}
public static IServiceCollection AddInitializer<TInitializer>(this IServiceCollection services)
where TInitializer : class,
IInitializer
{
services.AddTransient<IInitializer, TInitializer>();
return services;
}
public static IServiceCollection AddNavigateHandler<THandler>(this IServiceCollection services)
where THandler : INavigateHandler,
IHandler
{
IEnumerable<Type> contracts = typeof(THandler).GetInterfaces()
.Where(x => x.Name == typeof(INavigateHandler<>).Name || x.Name == typeof(INavigateBackHandler<>).Name);
foreach (Type contract in contracts)
{
if (contract.GetGenericArguments() is { Length: 1 } arguments)
{
services.AddTransient<INavigation>(provider => new Navigation
{
Type = arguments[0]
});
}
}
services.AddHandler<THandler>();
return services;
}
public static IServiceCollection AddRange(this IServiceCollection services,
IServiceCollection fromServices)
{
foreach (ServiceDescriptor service in fromServices)
{
services.Add(service);
}
return services;
}
}
+8
View File
@@ -0,0 +1,8 @@
namespace Toolkit.Foundation;
public interface IServiceFactory
{
object Create(Type type, params object?[]? parameters);
TService Create<TService>(params object?[]? parameters);
}
@@ -0,0 +1,6 @@
namespace Toolkit.Foundation;
public interface IServiceScopeFactory<TService>
{
TService? Create(params object?[] parameters);
}
@@ -0,0 +1,8 @@
using Microsoft.Extensions.DependencyInjection;
namespace Toolkit.Foundation;
public interface IServiceScopeProvider<TService>
{
bool TryGet(TService service, out IServiceScope? serviceScope);
}
+8
View File
@@ -0,0 +1,8 @@
namespace Toolkit.Foundation;
public interface ISubscriber
{
void Remove(object subscriber);
void Add(object subscriber);
}
@@ -0,0 +1,10 @@
namespace Toolkit.Foundation;
public interface ISubscriptionManager
{
IEnumerable<object?> GetHandlers(Type notificationType, object key);
void Remove(object subscriber);
void Add(object subscriber);
}
@@ -0,0 +1,8 @@
namespace Toolkit.Foundation;
public interface IWritableConfiguration<out TConfiguration>
where TConfiguration :
class
{
void Write(Action<TConfiguration> updateDelegate);
}
+3
View File
@@ -0,0 +1,3 @@
namespace Toolkit.Foundation;
public record Insert<TValue>(int Index, TValue Value) : INotification;
+5
View File
@@ -0,0 +1,5 @@
namespace Toolkit.Foundation;
public record KeyAccelerator(VirtualKey Key,
VirtualKey[]? Modifiers = null) :
IRequest;
+48
View File
@@ -0,0 +1,48 @@
using System.Reflection;
namespace Toolkit.Foundation;
public class Mediator(IServiceProvider provider) :
IMediator
{
public Task<TResponse?> SendAsync<TResponse>(IRequest<TResponse> request,
CancellationToken cancellationToken = default)
{
Type handlerType = typeof(HandlerWrapper<,>).MakeGenericType(request.GetType(),
typeof(TResponse));
if (provider.GetService(handlerType)
is object handler)
{
if (handlerType.GetMethod("Handle") is MethodInfo handleMethod)
{
return (Task<TResponse?>)handleMethod.Invoke(handler, new object[] { request, cancellationToken })!;
}
}
return Task.FromResult<TResponse?>(default);
}
public Task<object?> SendAsync(object message,
CancellationToken cancellationToken = default)
{
if (message.GetType().GetInterface(typeof(IRequest<>).Name) is Type requestType &&
requestType.GetGenericArguments().Length == 1)
{
Type responseType = requestType.GetGenericArguments()[0];
Type handlerType = typeof(HandlerWrapper<,>).MakeGenericType(message.GetType(),
responseType);
if (provider.GetService(handlerType)
is object handler)
{
if (handlerType.GetMethod("Handle") is MethodInfo handleMethod)
{
return (Task<object?>)handleMethod.Invoke(handler, new object[] { message, cancellationToken })!;
}
}
}
return Task.FromResult<object?>(default);
}
}
@@ -0,0 +1,20 @@
using System.Reflection;
namespace Toolkit.Foundation;
public static class MethodInfoExtensions
{
public static async Task<TResult> InvokeAsync<TResult>(this MethodInfo methodInfo,
object? obj)
{
dynamic result = await (dynamic?)methodInfo.Invoke(obj, null);
return (TResult)result;
}
public static async Task InvokeAsync(this MethodInfo methodInfo,
object? obj,
params object[] parameters)
{
await (dynamic?)methodInfo.Invoke(obj, parameters);
}
}
+3
View File
@@ -0,0 +1,3 @@
namespace Toolkit.Foundation;
public record Move<TValue>(int Index, TValue Value) : INotification;
+16
View File
@@ -0,0 +1,16 @@
namespace Toolkit.Foundation;
public record Navigate(string Route,
object? Context = null,
string? Scope = null,
object? Sender = null,
EventHandler? Navigated = null,
object[]? Parameters = null) :
INotification;
public record Navigate<TNavigation>(object Context,
object Template,
object Content,
object? Sender = null,
object[]? Parameters = null) :
INotification;
+7
View File
@@ -0,0 +1,7 @@
namespace Toolkit.Foundation;
public record NavigateBack(object? Context = null, string? Scope = null) :
INotification;
public record NavigateBack<TNavigation>(object? Context) :
INotification;
+22
View File
@@ -0,0 +1,22 @@
using Microsoft.Extensions.DependencyInjection;
namespace Toolkit.Foundation;
public class NavigateBackHandler(IComponentScopeProvider provider) :
INotificationHandler<NavigateBack>
{
public async Task Handle(NavigateBack args,
CancellationToken cancellationToken)
{
if (provider.Get(args.Scope ?? "Default")
is IServiceProvider scope)
{
if (scope.GetService<INavigationScope>() is INavigationScope navigationScope)
{
await navigationScope.NavigateBackAsync(args.Context, cancellationToken);
}
}
}
}

Some files were not shown because too many files have changed in this diff Show More