diff --git a/Framework/Foundation/Configurations/WriteHandler.cs b/Framework/Foundation/Configurations/WriteHandler.cs index d5a96a7..7c2bad9 100644 --- a/Framework/Foundation/Configurations/WriteHandler.cs +++ b/Framework/Foundation/Configurations/WriteHandler.cs @@ -1,4 +1,6 @@ -namespace Toolkit.Framework.Foundation; +using System.Diagnostics; + +namespace Toolkit.Framework.Foundation; public class WriteHandler : IRequestHandler> where TConfiguration : class { @@ -20,7 +22,7 @@ public class WriteHandler : IRequestHandler(configuration), cancellationToken); + await mediator.Publish(new ConfigurationChanged(configuration), cancellationToken); return default; } diff --git a/Framework/Foundation/Extensions/IServiceCollectionExtensions.cs b/Framework/Foundation/Extensions/IServiceCollectionExtensions.cs index 16d6fae..4be88f2 100644 --- a/Framework/Foundation/Extensions/IServiceCollectionExtensions.cs +++ b/Framework/Foundation/Extensions/IServiceCollectionExtensions.cs @@ -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(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(), ServiceLifetime.Singleton)); + } + } + if (typeof(THandler).GetInterface(typeof(IRequestHandler<,>).Name) is { } requestContract) { if (requestContract.GetGenericArguments() is { Length: 2 } arguments) diff --git a/Framework/Foundation/Lifecycles/IMediator.cs b/Framework/Foundation/Lifecycles/IMediator.cs index 2d83080..41947bf 100644 --- a/Framework/Foundation/Lifecycles/IMediator.cs +++ b/Framework/Foundation/Lifecycles/IMediator.cs @@ -2,6 +2,8 @@ public interface IMediator { + ValueTask Publish(TNotification notification, CancellationToken cancellationToken = default) where TNotification : INotification; + ValueTask Send(IRequest request, CancellationToken cancellationToken = default); ValueTask Send(ICommand command, CancellationToken cancellationToken = default); diff --git a/Framework/Foundation/Lifecycles/Mediator.cs b/Framework/Foundation/Lifecycles/Mediator.cs index b3989e3..3fe0ebf 100644 --- a/Framework/Foundation/Lifecycles/Mediator.cs +++ b/Framework/Foundation/Lifecycles/Mediator.cs @@ -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 notification, CancellationToken cancellationToken = default) where TNotification : INotification + { + List> handlers = factory.GetServices>().ToList(); + + if (handlers.Count == 0) + { + return default; + } + else if (handlers.Count == 1) + { + return handlers[0].Handle(notification, cancellationToken); + } + + return default; + } + public ValueTask Send(IRequest 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 Send(ICommand 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 Send(IQuery 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);