This commit is contained in:
TheXamlGuy
2024-01-06 14:34:25 +00:00
parent 53537aa4c7
commit e1c7846e45
73 changed files with 251 additions and 198 deletions
@@ -6,8 +6,8 @@ namespace Hyperbar;
public class ConfigurationWriter<TConfiguration>(string path,
string section,
JsonSerializerOptions? serializerOptions = null) :
IConfigurationWriter<TConfiguration>
JsonSerializerOptions? serializerOptions = null) :
IConfigurationWriter<TConfiguration>
where TConfiguration :
class, new()
{
@@ -1,7 +1,7 @@
namespace Hyperbar;
public interface IConfigurationWriter<TConfiguration>
where TConfiguration :
where TConfiguration :
class, new()
{
void Write(Action<TConfiguration> updateDelegate);
@@ -2,11 +2,11 @@
namespace Hyperbar;
public interface IWritableConfiguration<out TConfiguration> :
IOptionsSnapshot<TConfiguration>
public interface IWritableConfiguration<out TConfiguration> :
IOptionsSnapshot<TConfiguration>
where TConfiguration :
class, new()
{
void Write(Action<TConfiguration> updateAction,
void Write(Action<TConfiguration> updateAction,
bool reload = true);
}
}
@@ -5,16 +5,16 @@ namespace Hyperbar;
public class WritableConfiguration<TConfiguration>(IConfigurationWriter<TConfiguration> writer,
IOptionsMonitor<TConfiguration> options,
IConfiguration configuration) :
IWritableConfiguration<TConfiguration>
where TConfiguration :
IConfiguration configuration) :
IWritableConfiguration<TConfiguration>
where TConfiguration :
class, new()
{
public TConfiguration Value => options.CurrentValue;
public TConfiguration Get(string? name) => options.Get(name);
public void Write(Action<TConfiguration> updateDelegate,
public void Write(Action<TConfiguration> updateDelegate,
bool reload = true)
{
writer.Write(updateDelegate);
@@ -23,4 +23,4 @@ public class WritableConfiguration<TConfiguration>(IConfigurationWriter<TConfigu
configurationRoot.Reload();
}
}
}
}
@@ -5,7 +5,7 @@ using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Options;
using System.Text.Json;
namespace Hyperbar;
namespace Hyperbar.Extensions;
public static class IServiceCollectionExtensions
{
@@ -20,14 +20,14 @@ public static class IServiceCollectionExtensions
public static IServiceCollection AddConfiguration<TConfiguration>(this IServiceCollection services,
string section,
string path = "Settings.json",
Action<JsonSerializerOptions>? serializerDelegate = null)
where TConfiguration :
Action<JsonSerializerOptions>? serializerDelegate = null)
where TConfiguration :
class, new()
{
services.AddOptions();
services.AddSingleton<IConfigureOptions<TConfiguration>>(new ConfigureNamedOptions<TConfiguration>("", args => { }));
_ = services.AddOptions();
_ = services.AddSingleton<IConfigureOptions<TConfiguration>>(new ConfigureNamedOptions<TConfiguration>("", args => { }));
services.AddTransient<IConfigurationWriter<TConfiguration>>(provider =>
_ = services.AddTransient<IConfigurationWriter<TConfiguration>>(provider =>
{
string? jsonFilePath = null;
if (provider.GetService<IHostEnvironment>() is IHostEnvironment hostEnvironment)
@@ -50,7 +50,7 @@ public static class IServiceCollectionExtensions
return new ConfigurationWriter<TConfiguration>(jsonFilePath, section, defaultSerializerOptions);
});
services.AddTransient<IWritableConfiguration<TConfiguration>, WritableConfiguration<TConfiguration>>();
_ = services.AddTransient<IWritableConfiguration<TConfiguration>, WritableConfiguration<TConfiguration>>();
return services;
}
@@ -62,14 +62,14 @@ public static class IServiceCollectionExtensions
Type templateType = typeof(IWidgetView);
string key = contentType.Name;
services.AddTransient(typeof(IWidgetViewModel), contentType);
_ = services.AddTransient(typeof(IWidgetViewModel), contentType);
services.TryAddTransient(templateType, provider => provider.GetService<IWidgetView>()!);
services.AddKeyedTransient(typeof(IWidgetViewModel), key, contentType);
services.TryAddKeyedTransient<IWidgetView>(key, (provider, key) => provider.GetService<IWidgetView>()!);
_ = services.AddKeyedTransient(typeof(IWidgetViewModel), key, contentType);
services.TryAddKeyedTransient(key, (provider, key) => provider.GetService<IWidgetView>()!);
services.AddTransient<IContentTemplateDescriptor>(provider => new ContentTemplateDescriptor
_ = services.AddTransient<IContentTemplateDescriptor>(provider => new ContentTemplateDescriptor
{
ContentType = contentType,
TemplateType = templateType,
@@ -88,13 +88,13 @@ public static class IServiceCollectionExtensions
string key = contentType.Name;
services.AddTransient(typeof(IWidgetViewModel), contentType);
_ = services.AddTransient(typeof(IWidgetViewModel), contentType);
services.TryAddTransient(templateType);
services.AddKeyedTransient(typeof(IWidgetViewModel), key, contentType);
_ = services.AddKeyedTransient(typeof(IWidgetViewModel), key, contentType);
services.TryAddKeyedTransient(templateType, key);
services.AddTransient<IContentTemplateDescriptor>(provider => new ContentTemplateDescriptor
_ = services.AddTransient<IContentTemplateDescriptor>(provider => new ContentTemplateDescriptor
{
ContentType = contentType,
TemplateType = templateType,
@@ -112,13 +112,13 @@ public static class IServiceCollectionExtensions
key ??= contentType.Name;
services.AddTransient(contentType);
_ = services.AddTransient(contentType);
services.TryAddTransient(templateType);
services.AddKeyedTransient(contentType, key);
services.AddKeyedTransient(templateType, key);
services.AddTransient<IContentTemplateDescriptor>(provider => new ContentTemplateDescriptor
_ = services.AddKeyedTransient(contentType, key);
_ = services.AddKeyedTransient(templateType, key);
_ = services.AddTransient<IContentTemplateDescriptor>(provider => new ContentTemplateDescriptor
{
ContentType = contentType,
TemplateType = templateType,
@@ -127,4 +127,4 @@ public static class IServiceCollectionExtensions
return services;
}
}
}
+1 -1
View File
@@ -3,4 +3,4 @@
public interface IServiceFactory
{
TService Create<TService>(params object?[] parameters);
}
}
+3 -3
View File
@@ -1,8 +1,8 @@
namespace Hyperbar;
public class ServiceFactory(Func<Type, object?[], object> factory) :
public class ServiceFactory(Func<Type, object?[], object> factory) :
IServiceFactory
{
public TService Create<TService>(params object?[] parameters) =>
public TService Create<TService>(params object?[] parameters) =>
(TService)factory(typeof(TService), parameters);
}
}
+1 -1
View File
@@ -17,4 +17,4 @@ public class AppService(IEnumerable<IInitializer> initializers) :
{
throw new NotImplementedException();
}
}
}
-1
View File
@@ -7,5 +7,4 @@ public interface IInitializer
public interface IDataTemplateSelector
{
}
+1 -1
View File
@@ -5,4 +5,4 @@ namespace Hyperbar;
public interface IWidgetBuilder
{
void Create(IServiceCollection services);
}
}
+1 -1
View File
@@ -3,4 +3,4 @@
public interface IWidgetContext
{
IServiceProvider ServiceProvider { get; }
}
}
@@ -1,7 +1,7 @@
namespace Hyperbar;
public class CommandClassHandlerWrapper<TRequest, TResponse>
where TRequest :
public class CommandClassHandlerWrapper<TRequest, TResponse>
where TRequest :
class,
ICommand<TResponse>
{
@@ -16,7 +16,7 @@ public class CommandClassHandlerWrapper<TRequest, TResponse>
{
MessageHandlerDelegate<TRequest, TResponse> handlerCopy = handler;
IPipelineBehavior<TRequest, TResponse> pipelineCopy = pipeline;
handler = (TRequest message, CancellationToken cancellationToken) =>
handler = (TRequest message, CancellationToken cancellationToken) =>
pipelineCopy.Handle(message, handlerCopy, cancellationToken);
}
@@ -24,4 +24,4 @@ public class CommandClassHandlerWrapper<TRequest, TResponse>
}
public ValueTask<TResponse> Handle(TRequest request, CancellationToken cancellationToken) => handler(request, cancellationToken);
}
}
+1 -2
View File
@@ -2,5 +2,4 @@
public interface ICommand : ICommand<Unit>;
public interface ICommand<out TResponse> : IMessage;
public interface ICommand<out TResponse> : IMessage;
+18 -5
View File
@@ -1,13 +1,26 @@
namespace Hyperbar;
public record KeyAcceleratorCommand(string Key, string[]? Modifiers = null) :
ICommand;
public class KeyAcceleratorCommandHanler :
ICommandHandler<KeyAcceleratorCommand>
{
public ValueTask<Unit> Handle(KeyAcceleratorCommand command,
CancellationToken cancellationToken)
{
throw new NotImplementedException();
}
}
public interface ICommandHandler<in TCommand> : ICommandHandler<TCommand, Unit>
where TCommand :
where TCommand :
ICommand<Unit>;
public interface ICommandHandler<in TCommand, TResponse>
where TCommand :
public interface ICommandHandler<in TCommand, TResponse>
where TCommand :
ICommand<TResponse>
{
ValueTask<TResponse> Handle(TCommand command,
ValueTask<TResponse> Handle(TCommand command,
CancellationToken cancellationToken);
}
}
+6 -7
View File
@@ -2,9 +2,9 @@
public interface IMediator
{
ValueTask Publish<TNotification>(TNotification notification,
CancellationToken cancellationToken = default)
where TNotification :
ValueTask Publish<TNotification>(TNotification notification,
CancellationToken cancellationToken = default)
where TNotification :
INotification;
ValueTask<TResponse> Send<TResponse>(IRequest<TResponse> request,
@@ -13,12 +13,11 @@ public interface IMediator
ValueTask<TResponse> Send<TResponse>(ICommand<TResponse> command,
CancellationToken cancellationToken = default);
ValueTask<TResponse> Send<TResponse>(IQuery<TResponse> query,
ValueTask<TResponse> Send<TResponse>(IQuery<TResponse> query,
CancellationToken cancellationToken = default);
ValueTask<object?> Send(object message, CancellationToken
ValueTask<object?> Send(object message, CancellationToken
cancellationToken = default);
void Subscribe(object subscriber);
}
}
+1 -2
View File
@@ -1,4 +1,3 @@
namespace Hyperbar;
public interface IMessage;
public interface IMessage;
+1 -2
View File
@@ -1,4 +1,3 @@
namespace Hyperbar;
public interface INotification : IMessage;
public interface INotification : IMessage;
+4 -5
View File
@@ -1,10 +1,9 @@
namespace Hyperbar;
public interface INotificationHandler<in TNotification>
where TNotification :
public interface INotificationHandler<in TNotification>
where TNotification :
INotification
{
ValueTask Handle(TNotification notification,
ValueTask Handle(TNotification notification,
CancellationToken cancellationToken);
}
}
+4 -5
View File
@@ -1,12 +1,11 @@
namespace Hyperbar;
public interface IPipelineBehavior<TMessage, TResponse>
where TMessage :
notnull,
public interface IPipelineBehavior<TMessage, TResponse>
where TMessage :
notnull,
IMessage
{
ValueTask<TResponse> Handle(TMessage message,
MessageHandlerDelegate<TMessage, TResponse> next,
CancellationToken cancellationToken = default);
}
}
+1 -1
View File
@@ -1,3 +1,3 @@
namespace Hyperbar;
public interface IQuery<out TResponse> : IMessage;
public interface IQuery<out TResponse> : IMessage;
+3 -3
View File
@@ -1,9 +1,9 @@
namespace Hyperbar;
public interface IQueryHandler<in TQuery, TResponse>
public interface IQueryHandler<in TQuery, TResponse>
where TQuery :
IQuery<TResponse>
{
ValueTask<TResponse> Handle(TQuery query,
ValueTask<TResponse> Handle(TQuery query,
CancellationToken cancellationToken);
}
}
+1 -1
View File
@@ -2,4 +2,4 @@
public interface IRequest<out TResponse> : IMessage;
public interface IRequest : IRequest<Unit>;
public interface IRequest : IRequest<Unit>;
+5 -6
View File
@@ -4,14 +4,13 @@ public interface IRequestHandler<in TRequest, TResponse>
where TRequest :
IRequest<TResponse>
{
ValueTask<TResponse> Handle(TRequest request,
ValueTask<TResponse> Handle(TRequest request,
CancellationToken cancellationToken);
}
public interface IRequestHandler<in TRequest> :
IRequestHandler<TRequest, Unit>
where TRequest :
public interface IRequestHandler<in TRequest> :
IRequestHandler<TRequest, Unit>
where TRequest :
IRequest<Unit>
{
}
}
+9 -9
View File
@@ -3,17 +3,17 @@ using System.Runtime.CompilerServices;
namespace Hyperbar;
public class Mediator(IServiceProvider provider) :
public class Mediator(IServiceProvider provider) :
IMediator
{
private readonly ConditionalWeakTable<Type, dynamic> handlers = [];
public ValueTask Publish<TNotification>(TNotification notification,
CancellationToken cancellationToken = default)
where TNotification :
public ValueTask Publish<TNotification>(TNotification notification,
CancellationToken cancellationToken = default)
where TNotification :
INotification
{
List<INotificationHandler<TNotification>> handlers =
List<INotificationHandler<TNotification>> handlers =
provider.GetServices<INotificationHandler<TNotification>>().ToList();
foreach (KeyValuePair<Type, dynamic> handler in this.handlers)
@@ -36,7 +36,7 @@ public class Mediator(IServiceProvider provider) :
return default;
}
public ValueTask<TResponse> Send<TResponse>(IRequest<TResponse> request,
public ValueTask<TResponse> Send<TResponse>(IRequest<TResponse> request,
CancellationToken cancellationToken = default)
{
dynamic? handler = provider.GetService(typeof(RequestClassHandlerWrapper<,>)
@@ -50,7 +50,7 @@ public class Mediator(IServiceProvider provider) :
return default;
}
public ValueTask<TResponse> Send<TResponse>(ICommand<TResponse> command,
public ValueTask<TResponse> Send<TResponse>(ICommand<TResponse> command,
CancellationToken cancellationToken = default)
{
dynamic? handler = provider.GetService(typeof(CommandClassHandlerWrapper<,>)
@@ -64,7 +64,7 @@ public class Mediator(IServiceProvider provider) :
return default;
}
public ValueTask<TResponse> Send<TResponse>(IQuery<TResponse> query,
public ValueTask<TResponse> Send<TResponse>(IQuery<TResponse> query,
CancellationToken cancellationToken = default)
{
dynamic? handler = provider.GetService(typeof(QueryClassHandlerWrapper<,>)
@@ -146,4 +146,4 @@ public class Mediator(IServiceProvider provider) :
}
}
}
}
}
+1 -1
View File
@@ -4,4 +4,4 @@ public delegate ValueTask<TResponse> MessageHandlerDelegate<TMessage, TResponse>
CancellationToken cancellationToken)
where TMessage :
notnull,
IMessage;
IMessage;
@@ -1,7 +1,7 @@
namespace Hyperbar;
public class QueryClassHandlerWrapper<TRequest, TResponse>
where TRequest :
public class QueryClassHandlerWrapper<TRequest, TResponse>
where TRequest :
class,
IQuery<TResponse>
{
@@ -25,4 +25,4 @@ public class QueryClassHandlerWrapper<TRequest, TResponse>
public ValueTask<TResponse> Handle(TRequest request, CancellationToken cancellationToken) =>
handler(request, cancellationToken);
}
}
@@ -1,8 +1,8 @@
namespace Hyperbar;
public class RequestClassHandlerWrapper<TRequest, TResponse>
where TRequest :
class,
where TRequest :
class,
IRequest<TResponse>
{
private readonly MessageHandlerDelegate<TRequest, TResponse> handler;
@@ -16,7 +16,7 @@ public class RequestClassHandlerWrapper<TRequest, TResponse>
MessageHandlerDelegate<TRequest, TResponse> handlerCopy = handler;
IPipelineBehavior<TRequest, TResponse> pipelineCopy = pipeline;
handler = (TRequest message, CancellationToken cancellationToken) =>
handler = (TRequest message, CancellationToken cancellationToken) =>
pipelineCopy.Handle(message, handlerCopy, cancellationToken);
}
@@ -24,4 +24,4 @@ public class RequestClassHandlerWrapper<TRequest, TResponse>
}
public ValueTask<TResponse> Handle(TRequest request, CancellationToken cancellationToken) => handler(request, cancellationToken);
}
}
+4 -5
View File
@@ -1,8 +1,8 @@
namespace Hyperbar;
public readonly struct Unit :
IEquatable<Unit>,
IComparable<Unit>,
public readonly struct Unit :
IEquatable<Unit>,
IComparable<Unit>,
IComparable
{
private static readonly Unit value = new();
@@ -26,5 +26,4 @@ public readonly struct Unit :
public static bool operator !=(Unit _, Unit __) => false;
public override string ToString() => "()";
}
}
@@ -8,5 +8,4 @@ public record ContentTemplateDescriptor :
public required Type TemplateType { get; set; }
public required object Key { get; set; }
}
}
@@ -7,5 +7,4 @@ public interface IContentTemplateDescriptor
object Key { get; set; }
Type TemplateType { get; set; }
}
}
+1 -1
View File
@@ -3,4 +3,4 @@
public interface ITemplateFactory
{
object? Create(object key);
}
}
+1 -1
View File
@@ -3,4 +3,4 @@
public interface ITemplatedViewModel
{
ITemplateFactory TemplateFactory { get; }
}
}
@@ -2,5 +2,4 @@
public interface IWidgetComponentViewModel
{
}
+1 -2
View File
@@ -2,5 +2,4 @@
public interface IWidgetView
{
}
}
+1 -2
View File
@@ -2,5 +2,4 @@
public interface IWidgetViewModel
{
}
}
@@ -13,7 +13,7 @@ public class ObservableCollectionViewModel<TItem>(IServiceFactory serviceFactory
return item;
}
public TItem Add<T>(params object?[] parameters)
public TItem Add<T>(params object?[] parameters)
where T : TItem
{
T? item = serviceFactory.Create<T>(parameters);
@@ -44,5 +44,4 @@ public class ObservableCollectionViewModel<TItem>(IServiceFactory serviceFactory
public class ObservableCollectionViewModel(IServiceFactory serviceFactory) :
ObservableCollectionViewModel<object>(serviceFactory)
{
}
}
+1 -1
View File
@@ -22,4 +22,4 @@ public partial class WidgetButtonViewModel :
click = new RelayCommand(action);
}
}
}
}
@@ -8,4 +8,4 @@ public partial class WidgetComponentViewModelBase(ITemplateFactory templateFacto
ITemplatedViewModel
{
public ITemplateFactory TemplateFactory => templateFactory;
}
}
+1 -1
View File
@@ -7,4 +7,4 @@ public class WidgetViewModelBase(ITemplateFactory templateFactory,
ITemplatedViewModel
{
public ITemplateFactory TemplateFactory => templateFactory;
}
}