Added Handler registrations in IServiceCollectionExtensions

This commit is contained in:
TheXamlGuy
2024-01-06 15:09:11 +00:00
parent e1c7846e45
commit a77c356389
8 changed files with 106 additions and 19 deletions
+6 -6
View File
@@ -25,11 +25,7 @@ public class KeyInterop
VirtualKey.RightWindows, VirtualKey.RightWindows,
VirtualKey.LeftWindows]; VirtualKey.LeftWindows];
public static void Press(VirtualKey key) => SendKey(key, true); public static void Send(VirtualKey key,
public static void Release(VirtualKey key) => SendKey(key, false);
public static unsafe void Type(VirtualKey key,
params VirtualKey[] modifierKeys) params VirtualKey[] modifierKeys)
{ {
foreach (VirtualKey modiferKey in 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) bool pressed)
{ {
INPUT input = new() INPUT input = new()
@@ -14,6 +14,7 @@ namespace Hyperbar.Windows
IHost? host = new HostBuilder() IHost? host = new HostBuilder()
.ConfigureServices(isolatedServices => .ConfigureServices(isolatedServices =>
{ {
isolatedServices.AddSingleton<IServiceFactory>(provider => isolatedServices.AddSingleton<IServiceFactory>(provider =>
new ServiceFactory((type, parameters) => ActivatorUtilities.CreateInstance(provider, type, parameters!))); new ServiceFactory((type, parameters) => ActivatorUtilities.CreateInstance(provider, type, parameters!)));
@@ -0,0 +1,11 @@
namespace Hyperbar.Windows;
public class KeyAcceleratorCommandHandler :
ICommandHandler<KeyAcceleratorCommand>
{
public ValueTask<Unit> Handle(KeyAcceleratorCommand command,
CancellationToken cancellationToken)
{
throw new NotImplementedException();
}
}
@@ -9,6 +9,83 @@ namespace Hyperbar.Extensions;
public static class IServiceCollectionExtensions public static class IServiceCollectionExtensions
{ {
public static IServiceCollection AddHandler<THandler>(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<THandler>(), 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<IServiceFactory>()?.Create(wrapperType,
provider.GetRequiredService<THandler>(),
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<IServiceFactory>()?.Create(wrapperType,
provider.GetRequiredService<THandler>(),
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<IServiceFactory>()?.Create(wrapperType,
provider.GetRequiredService<THandler>(),
provider.GetServices(typeof(IPipelineBehavior<,>).MakeGenericType(requestType, responseType)))!,
lifetime
));
}
}
return serviceCollection;
}
public static IServiceCollection AddConfiguration<TConfiguration>(this IServiceCollection services, public static IServiceCollection AddConfiguration<TConfiguration>(this IServiceCollection services,
string path = "Settings.json") string path = "Settings.json")
where TConfiguration : where TConfiguration :
+3
View File
@@ -2,5 +2,8 @@
public interface IServiceFactory public interface IServiceFactory
{ {
object Create(Type type,
params object?[] parameters);
TService Create<TService>(params object?[] parameters); TService Create<TService>(params object?[] parameters);
} }
+3
View File
@@ -5,4 +5,7 @@ public class ServiceFactory(Func<Type, object?[], object> factory) :
{ {
public TService Create<TService>(params object?[] parameters) => public TService Create<TService>(params object?[] parameters) =>
(TService)factory(typeof(TService), parameters); (TService)factory(typeof(TService), parameters);
public object Create(Type type, params object?[] parameters) =>
factory(type, parameters);
} }
-13
View File
@@ -1,18 +1,5 @@
namespace Hyperbar; 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> public interface ICommandHandler<in TCommand> : ICommandHandler<TCommand, Unit>
where TCommand : where TCommand :
ICommand<Unit>; ICommand<Unit>;
@@ -0,0 +1,5 @@
namespace Hyperbar;
public record KeyAcceleratorCommand(string Key,
string[]? Modifiers = null) :
ICommand;