From a77c35638978dda7ff59b33eccfcc549dc0a28fc Mon Sep 17 00:00:00 2001 From: TheXamlGuy Date: Sat, 6 Jan 2024 15:09:11 +0000 Subject: [PATCH] Added Handler registrations in IServiceCollectionExtensions --- Hyperbar.Windows.Win32/KeyIntrop.cs | 12 +-- .../IServiceCollectionExtensions.cs | 1 + .../Mediators/KeyAcceleratorCommandHandler.cs | 11 +++ .../IServiceCollectionExtensions.cs | 77 +++++++++++++++++++ Hyperbar/Factories/IServiceFactory.cs | 3 + Hyperbar/Factories/ServiceFactory.cs | 3 + Hyperbar/Mediators/ICommandHandler.cs | 13 ---- Hyperbar/Mediators/KeyAcceleratorCommand.cs | 5 ++ 8 files changed, 106 insertions(+), 19 deletions(-) create mode 100644 Hyperbar.Windows/Mediators/KeyAcceleratorCommandHandler.cs create mode 100644 Hyperbar/Mediators/KeyAcceleratorCommand.cs diff --git a/Hyperbar.Windows.Win32/KeyIntrop.cs b/Hyperbar.Windows.Win32/KeyIntrop.cs index 4d3f41f..eaa6164 100644 --- a/Hyperbar.Windows.Win32/KeyIntrop.cs +++ b/Hyperbar.Windows.Win32/KeyIntrop.cs @@ -25,11 +25,7 @@ public class KeyInterop VirtualKey.RightWindows, VirtualKey.LeftWindows]; - public static void Press(VirtualKey key) => SendKey(key, true); - - public static void Release(VirtualKey key) => SendKey(key, false); - - public static unsafe void Type(VirtualKey key, + public static void Send(VirtualKey key, params VirtualKey[] modifierKeys) { foreach (VirtualKey modiferKey in modifierKeys) @@ -46,7 +42,11 @@ public class KeyInterop } } - private static unsafe void SendKey(VirtualKey key, + private static void Press(VirtualKey key) => Send(key, true); + + private static void Release(VirtualKey key) => Send(key, false); + + private static unsafe void Send(VirtualKey key, bool pressed) { INPUT input = new() diff --git a/Hyperbar.Windows/Lifecycles/IServiceCollectionExtensions.cs b/Hyperbar.Windows/Lifecycles/IServiceCollectionExtensions.cs index f2e722f..a5b2c36 100644 --- a/Hyperbar.Windows/Lifecycles/IServiceCollectionExtensions.cs +++ b/Hyperbar.Windows/Lifecycles/IServiceCollectionExtensions.cs @@ -14,6 +14,7 @@ namespace Hyperbar.Windows IHost? host = new HostBuilder() .ConfigureServices(isolatedServices => { + isolatedServices.AddSingleton(provider => new ServiceFactory((type, parameters) => ActivatorUtilities.CreateInstance(provider, type, parameters!))); diff --git a/Hyperbar.Windows/Mediators/KeyAcceleratorCommandHandler.cs b/Hyperbar.Windows/Mediators/KeyAcceleratorCommandHandler.cs new file mode 100644 index 0000000..49d51d0 --- /dev/null +++ b/Hyperbar.Windows/Mediators/KeyAcceleratorCommandHandler.cs @@ -0,0 +1,11 @@ +namespace Hyperbar.Windows; + +public class KeyAcceleratorCommandHandler : + ICommandHandler +{ + public ValueTask Handle(KeyAcceleratorCommand command, + CancellationToken cancellationToken) + { + throw new NotImplementedException(); + } +} diff --git a/Hyperbar/Extensions/IServiceCollectionExtensions.cs b/Hyperbar/Extensions/IServiceCollectionExtensions.cs index 9fc0d72..ca4c387 100644 --- a/Hyperbar/Extensions/IServiceCollectionExtensions.cs +++ b/Hyperbar/Extensions/IServiceCollectionExtensions.cs @@ -9,6 +9,83 @@ namespace Hyperbar.Extensions; public static class IServiceCollectionExtensions { + public static IServiceCollection AddHandler(this IServiceCollection serviceCollection, + ServiceLifetime lifetime = ServiceLifetime.Transient) + where THandler : + notnull + { + if (typeof(THandler).GetInterface(typeof(INotificationHandler<>).Name) is { } notificationContract) + { + if (notificationContract.GetGenericArguments() is { Length: 1 } arguments) + { + Type notificationType = arguments[0]; + + serviceCollection.TryAdd(new ServiceDescriptor(typeof(THandler), typeof(THandler), ServiceLifetime.Singleton)); + serviceCollection.Add(new ServiceDescriptor(typeof(INotificationHandler<>).MakeGenericType(notificationType), + provider => provider.GetRequiredService(), lifetime)); + } + } + + if (typeof(THandler).GetInterface(typeof(IRequestHandler<,>).Name) is { } requestContract) + { + if (requestContract.GetGenericArguments() is { Length: 2 } arguments) + { + Type requestType = arguments[0]; + Type responseType = arguments[1]; + + Type wrapperType = typeof(RequestClassHandlerWrapper<,>).MakeGenericType(requestType, responseType); + + serviceCollection.TryAdd(new ServiceDescriptor(typeof(THandler), typeof(THandler), lifetime)); + serviceCollection.Add(new ServiceDescriptor(wrapperType, + provider => provider.GetService()?.Create(wrapperType, + provider.GetRequiredService(), + provider.GetServices(typeof(IPipelineBehavior<,>).MakeGenericType(requestType, responseType)))!, + lifetime + )); + } + } + + if (typeof(THandler).GetInterface(typeof(ICommandHandler<,>).Name) is { } commandContract) + { + if (commandContract.GetGenericArguments() is { Length: 2 } arguments) + { + Type requestType = arguments[0]; + Type responseType = arguments[1]; + + Type wrapperType = typeof(CommandClassHandlerWrapper<,>).MakeGenericType(requestType, responseType); + + serviceCollection.TryAdd(new ServiceDescriptor(typeof(THandler), typeof(THandler), lifetime)); + serviceCollection.Add(new ServiceDescriptor(wrapperType, + provider => provider.GetService()?.Create(wrapperType, + provider.GetRequiredService(), + provider.GetServices(typeof(IPipelineBehavior<,>).MakeGenericType(requestType, responseType)))!, + lifetime + )); + } + } + + if (typeof(THandler).GetInterface(typeof(IQueryHandler<,>).Name) is { } queryContract) + { + if (queryContract.GetGenericArguments() is { Length: 2 } arguments) + { + Type requestType = arguments[0]; + Type responseType = arguments[1]; + + Type wrapperType = typeof(QueryClassHandlerWrapper<,>).MakeGenericType(requestType, responseType); + + serviceCollection.TryAdd(new ServiceDescriptor(typeof(THandler), typeof(THandler), lifetime)); + serviceCollection.Add(new ServiceDescriptor(wrapperType, + provider => provider.GetService()?.Create(wrapperType, + provider.GetRequiredService(), + provider.GetServices(typeof(IPipelineBehavior<,>).MakeGenericType(requestType, responseType)))!, + lifetime + )); + } + } + return serviceCollection; + } + + public static IServiceCollection AddConfiguration(this IServiceCollection services, string path = "Settings.json") where TConfiguration : diff --git a/Hyperbar/Factories/IServiceFactory.cs b/Hyperbar/Factories/IServiceFactory.cs index 1ad38d7..dcf52e1 100644 --- a/Hyperbar/Factories/IServiceFactory.cs +++ b/Hyperbar/Factories/IServiceFactory.cs @@ -2,5 +2,8 @@ public interface IServiceFactory { + object Create(Type type, + params object?[] parameters); + TService Create(params object?[] parameters); } \ No newline at end of file diff --git a/Hyperbar/Factories/ServiceFactory.cs b/Hyperbar/Factories/ServiceFactory.cs index 600ce40..8eef40f 100644 --- a/Hyperbar/Factories/ServiceFactory.cs +++ b/Hyperbar/Factories/ServiceFactory.cs @@ -5,4 +5,7 @@ public class ServiceFactory(Func factory) : { public TService Create(params object?[] parameters) => (TService)factory(typeof(TService), parameters); + + public object Create(Type type, params object?[] parameters) => + factory(type, parameters); } \ No newline at end of file diff --git a/Hyperbar/Mediators/ICommandHandler.cs b/Hyperbar/Mediators/ICommandHandler.cs index e83363a..7ae513a 100644 --- a/Hyperbar/Mediators/ICommandHandler.cs +++ b/Hyperbar/Mediators/ICommandHandler.cs @@ -1,18 +1,5 @@ namespace Hyperbar; -public record KeyAcceleratorCommand(string Key, string[]? Modifiers = null) : - ICommand; - -public class KeyAcceleratorCommandHanler : - ICommandHandler -{ - public ValueTask Handle(KeyAcceleratorCommand command, - CancellationToken cancellationToken) - { - throw new NotImplementedException(); - } -} - public interface ICommandHandler : ICommandHandler where TCommand : ICommand; diff --git a/Hyperbar/Mediators/KeyAcceleratorCommand.cs b/Hyperbar/Mediators/KeyAcceleratorCommand.cs new file mode 100644 index 0000000..58a0f33 --- /dev/null +++ b/Hyperbar/Mediators/KeyAcceleratorCommand.cs @@ -0,0 +1,5 @@ +namespace Hyperbar; + +public record KeyAcceleratorCommand(string Key, + string[]? Modifiers = null) : + ICommand;