Added Handler registrations in IServiceCollectionExtensions
This commit is contained in:
@@ -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 :
|
||||
|
||||
@@ -2,5 +2,8 @@
|
||||
|
||||
public interface IServiceFactory
|
||||
{
|
||||
object Create(Type type,
|
||||
params object?[] parameters);
|
||||
|
||||
TService Create<TService>(params object?[] parameters);
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
@@ -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;
|
||||
Reference in New Issue
Block a user