diff --git a/Toolkit.Foundation/IMediator.cs b/Toolkit.Foundation/IMediator.cs index 4af4be9..de03ff5 100644 --- a/Toolkit.Foundation/IMediator.cs +++ b/Toolkit.Foundation/IMediator.cs @@ -1,14 +1,22 @@ -namespace Toolkit.Foundation -{ - public interface IMediator - { - Task Handle(object message, - object? key = null, - CancellationToken cancellationToken = default); +namespace Toolkit.Foundation; - Task Handle(TMessage message, - object? key = null, - CancellationToken cancellationToken = default) - where TMessage : notnull; - } +public interface IMediator +{ + Task Handle(object message, + object? key = null, + CancellationToken cancellationToken = default); + + Task Handle(TMessage message, + object? key = null, + CancellationToken cancellationToken = default) + where TMessage : notnull; + + IAsyncEnumerable HandleMany(object message, + object? key = null, + CancellationToken cancellationToken = default); + + IAsyncEnumerable HandleMany(TMessage message, + object? key = null, + CancellationToken cancellationToken = default) + where TMessage : notnull; } \ No newline at end of file diff --git a/Toolkit.Foundation/Mediator.cs b/Toolkit.Foundation/Mediator.cs index cde5808..f1d64a5 100644 --- a/Toolkit.Foundation/Mediator.cs +++ b/Toolkit.Foundation/Mediator.cs @@ -27,29 +27,11 @@ public class Mediator(IHandlerProvider handlerProvider, return default; } - public async IAsyncEnumerable HandleMany(TMessage message, - object? key = null, - [EnumeratorCancellation] CancellationToken cancellationToken = default) - where TMessage : notnull - { - List handlers = GetHandlers(message, key); - - foreach (object? handler in handlers) - { - MethodInfo? handleMethod = handler?.GetType().GetMethod("Handle", [message.GetType(), typeof(CancellationToken)]); - if (handleMethod != null) - { - yield return await (Task)handleMethod.Invoke(handler, new object[] { message, cancellationToken })!; - } - } - } - public async Task Handle(object message, object? key = null, CancellationToken cancellationToken = default) { Type messageType = message.GetType(); - if (messageType.GetInterface(message.GetType().Name) is Type requestType && requestType.GetGenericArguments().Length == 1) { @@ -71,6 +53,46 @@ public class Mediator(IHandlerProvider handlerProvider, return default; } + public async IAsyncEnumerable HandleMany(object message, + object? key = null, + [EnumeratorCancellation] CancellationToken cancellationToken = default) + { + Type messageType = message.GetType(); + + if (messageType.GetInterface(message.GetType().Name) is Type requestType && + requestType.GetGenericArguments().Length == 1) + { + Type responseType = requestType.GetGenericArguments()[0]; + Type handlerWrapperType = typeof(HandlerWrapper<,>).MakeGenericType(message.GetType(), responseType); + + List handlers = GetHandlers(message, handlerWrapperType, key); + foreach (object? handler in handlers) + { + MethodInfo? handleMethod = handler?.GetType().GetMethod("Handle", [messageType, typeof(CancellationToken)]); + if (handleMethod != null) + { + yield return await (Task)handleMethod.Invoke(handler, new object[] { message, cancellationToken })!; + } + } + } + } + + public async IAsyncEnumerable HandleMany(TMessage message, + object? key = null, + [EnumeratorCancellation] CancellationToken cancellationToken = default) + where TMessage : notnull + { + List handlers = GetHandlers(message, key); + + foreach (object? handler in handlers) + { + MethodInfo? handleMethod = handler?.GetType().GetMethod("Handle", [message.GetType(), typeof(CancellationToken)]); + if (handleMethod != null) + { + yield return await (Task)handleMethod.Invoke(handler, new object[] { message, cancellationToken })!; + } + } + } private List GetHandlers(TMessage message, object? key) where TMessage : notnull {