Add basic notifications handling
This commit is contained in:
@@ -1,4 +1,6 @@
|
||||
namespace Toolkit.Framework.Foundation;
|
||||
using System.Diagnostics;
|
||||
|
||||
namespace Toolkit.Framework.Foundation;
|
||||
|
||||
public class WriteHandler<TConfiguration> : IRequestHandler<Write<TConfiguration>> where TConfiguration : class
|
||||
{
|
||||
@@ -20,7 +22,7 @@ public class WriteHandler<TConfiguration> : IRequestHandler<Write<TConfiguration
|
||||
request.UpdateDelegate.Invoke(configuration);
|
||||
writer.Write(configuration);
|
||||
|
||||
await mediator.Send(new ConfigurationChanged<TConfiguration>(configuration), cancellationToken);
|
||||
await mediator.Publish(new ConfigurationChanged<TConfiguration>(configuration), cancellationToken);
|
||||
|
||||
return default;
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.DependencyInjection.Extensions;
|
||||
using Microsoft.Extensions.Options;
|
||||
using System.Diagnostics;
|
||||
|
||||
namespace Toolkit.Framework.Foundation;
|
||||
|
||||
@@ -38,6 +39,17 @@ public static class IServiceCollectionExtensions
|
||||
|
||||
public static IServiceCollection AddHandler<THandler>(this IServiceCollection services, 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];
|
||||
|
||||
services.TryAdd(new ServiceDescriptor(typeof(THandler), typeof(THandler), ServiceLifetime.Singleton));
|
||||
services.Add(new ServiceDescriptor(typeof(INotificationHandler<>).MakeGenericType(notificationType), sp => sp.GetRequiredService<THandler>(), ServiceLifetime.Singleton));
|
||||
}
|
||||
}
|
||||
|
||||
if (typeof(THandler).GetInterface(typeof(IRequestHandler<,>).Name) is { } requestContract)
|
||||
{
|
||||
if (requestContract.GetGenericArguments() is { Length: 2 } arguments)
|
||||
|
||||
@@ -2,6 +2,8 @@
|
||||
|
||||
public interface IMediator
|
||||
{
|
||||
ValueTask Publish<TNotification>(TNotification notification, CancellationToken cancellationToken = default) where TNotification : INotification;
|
||||
|
||||
ValueTask<TResponse> Send<TResponse>(IRequest<TResponse> request, CancellationToken cancellationToken = default);
|
||||
|
||||
ValueTask<TResponse> Send<TResponse>(ICommand<TResponse> command, CancellationToken cancellationToken = default);
|
||||
|
||||
@@ -1,17 +1,35 @@
|
||||
namespace Toolkit.Framework.Foundation;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace Toolkit.Framework.Foundation;
|
||||
|
||||
public class Mediator : IMediator
|
||||
{
|
||||
private readonly IServiceFactory factory;
|
||||
private readonly IServiceProvider factory;
|
||||
|
||||
public Mediator(IServiceFactory factory)
|
||||
public Mediator(IServiceProvider factory)
|
||||
{
|
||||
this.factory = factory;
|
||||
}
|
||||
|
||||
public ValueTask Publish<TNotification>(TNotification notification, CancellationToken cancellationToken = default) where TNotification : INotification
|
||||
{
|
||||
List<INotificationHandler<TNotification>> handlers = factory.GetServices<INotificationHandler<TNotification>>().ToList();
|
||||
|
||||
if (handlers.Count == 0)
|
||||
{
|
||||
return default;
|
||||
}
|
||||
else if (handlers.Count == 1)
|
||||
{
|
||||
return handlers[0].Handle(notification, cancellationToken);
|
||||
}
|
||||
|
||||
return default;
|
||||
}
|
||||
|
||||
public ValueTask<TResponse> Send<TResponse>(IRequest<TResponse> request, CancellationToken cancellationToken = default)
|
||||
{
|
||||
dynamic? handler = factory.Create(typeof(RequestClassHandlerWrapper<,>).MakeGenericType(request.GetType(), typeof(TResponse)));
|
||||
dynamic? handler = factory.GetService(typeof(RequestClassHandlerWrapper<,>).MakeGenericType(request.GetType(), typeof(TResponse)));
|
||||
if (handler is not null)
|
||||
{
|
||||
return handler.Handle((dynamic)request, cancellationToken);
|
||||
@@ -22,7 +40,7 @@ public class Mediator : IMediator
|
||||
|
||||
public ValueTask<TResponse> Send<TResponse>(ICommand<TResponse> command, CancellationToken cancellationToken = default)
|
||||
{
|
||||
dynamic? handler = factory.Create(typeof(CommandClassHandlerWrapper<,>).MakeGenericType(command.GetType(), typeof(TResponse)));
|
||||
dynamic? handler = factory.GetService(typeof(CommandClassHandlerWrapper<,>).MakeGenericType(command.GetType(), typeof(TResponse)));
|
||||
if (handler is not null)
|
||||
{
|
||||
return handler.Handle((dynamic)command, cancellationToken);
|
||||
@@ -33,7 +51,7 @@ public class Mediator : IMediator
|
||||
|
||||
public ValueTask<TResponse> Send<TResponse>(IQuery<TResponse> query, CancellationToken cancellationToken = default)
|
||||
{
|
||||
dynamic? handler = factory.Create(typeof(QueryClassHandlerWrapper<,>).MakeGenericType(query.GetType(), typeof(TResponse)));
|
||||
dynamic? handler = factory.GetService(typeof(QueryClassHandlerWrapper<,>).MakeGenericType(query.GetType(), typeof(TResponse)));
|
||||
if (handler is not null)
|
||||
{
|
||||
return handler.Handle((dynamic)query, cancellationToken);
|
||||
@@ -50,7 +68,7 @@ public class Mediator : IMediator
|
||||
{
|
||||
Type responseType = arguments[0];
|
||||
|
||||
dynamic? handler = factory.Create(typeof(RequestClassHandlerWrapper<,>).MakeGenericType(message.GetType(), responseType));
|
||||
dynamic? handler = factory.GetService(typeof(RequestClassHandlerWrapper<,>).MakeGenericType(message.GetType(), responseType));
|
||||
if (handler is not null)
|
||||
{
|
||||
return handler.Handle((dynamic)message, cancellationToken);
|
||||
@@ -64,7 +82,7 @@ public class Mediator : IMediator
|
||||
{
|
||||
Type responseType = arguments[0];
|
||||
|
||||
dynamic? handler = factory.Create(typeof(CommandClassHandlerWrapper<,>).MakeGenericType(message.GetType(), responseType));
|
||||
dynamic? handler = factory.GetService(typeof(CommandClassHandlerWrapper<,>).MakeGenericType(message.GetType(), responseType));
|
||||
if (handler is not null)
|
||||
{
|
||||
return handler.Handle((dynamic)message, cancellationToken);
|
||||
@@ -78,7 +96,7 @@ public class Mediator : IMediator
|
||||
{
|
||||
Type responseType = arguments[0];
|
||||
|
||||
dynamic? handler = factory.Create(typeof(QueryClassHandlerWrapper<,>).MakeGenericType(message.GetType(), responseType));
|
||||
dynamic? handler = factory.GetService(typeof(QueryClassHandlerWrapper<,>).MakeGenericType(message.GetType(), responseType));
|
||||
if (handler is not null)
|
||||
{
|
||||
return handler.Handle((dynamic)message, cancellationToken);
|
||||
|
||||
Reference in New Issue
Block a user