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
@@ -9,6 +9,83 @@ namespace Hyperbar.Extensions;
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,
string path = "Settings.json")
where TConfiguration :
+3
View File
@@ -2,5 +2,8 @@
public interface IServiceFactory
{
object Create(Type type,
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) =>
(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;
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 :
ICommand<Unit>;
@@ -0,0 +1,5 @@
namespace Hyperbar;
public record KeyAcceleratorCommand(string Key,
string[]? Modifiers = null) :
ICommand;