| | | 1 | | using System.Diagnostics.CodeAnalysis; |
| | | 2 | | using System.Reflection; |
| | | 3 | | using Elsa.Mediator.Contracts; |
| | | 4 | | using Elsa.Mediator.Middleware.Command; |
| | | 5 | | using Elsa.Mediator.Middleware.Notification; |
| | | 6 | | using Elsa.Mediator.Models; |
| | | 7 | | |
| | | 8 | | namespace Elsa.Mediator.Extensions; |
| | | 9 | | |
| | | 10 | | /// <summary> |
| | | 11 | | /// Contains helper methods for invoking notification handlers. |
| | | 12 | | /// </summary> |
| | | 13 | | public static class HandlerExtensions |
| | | 14 | | { |
| | | 15 | | /// <summary> |
| | | 16 | | /// Gets the handle method for a notification handler of the given notification type. |
| | | 17 | | /// </summary> |
| | | 18 | | /// <param name="notificationType">The notification type.</param> |
| | | 19 | | /// <returns>The handle method.</returns> |
| | | 20 | | public static MethodInfo GetNotificationHandlerMethod(this Type notificationType) |
| | | 21 | | { |
| | 11316 | 22 | | var handlerType = typeof(INotificationHandler<>).MakeGenericType(notificationType); |
| | 11316 | 23 | | return handlerType.GetMethod("HandleAsync")!; |
| | | 24 | | } |
| | | 25 | | |
| | | 26 | | /// <summary> |
| | | 27 | | /// Gets the handle method for a command handler of the given command type. |
| | | 28 | | /// </summary> |
| | | 29 | | /// <param name="commandType">The command type.</param> |
| | | 30 | | /// <returns>The handle method.</returns> |
| | | 31 | | public static MethodInfo GetCommandHandlerMethod([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.Interfac |
| | | 32 | | { |
| | 33 | 33 | | var commandTypeInterfaces = commandType.GetInterfaces(); |
| | 77 | 34 | | var commandTypeInterface = commandTypeInterfaces.FirstOrDefault(x => x.IsGenericType && x.GetGenericTypeDefiniti |
| | 33 | 35 | | var resultType = commandTypeInterface?.GetGenericArguments()[0] ?? typeof(Unit); |
| | 33 | 36 | | var handlerType = typeof(ICommandHandler<,>).MakeGenericType(commandType, resultType); |
| | 33 | 37 | | return handlerType.GetMethod("HandleAsync")!; |
| | | 38 | | } |
| | | 39 | | |
| | | 40 | | /// <summary> |
| | | 41 | | /// Invokes the given handler for the given notification. |
| | | 42 | | /// </summary> |
| | | 43 | | /// <param name="handler">The handler to invoke.</param> |
| | | 44 | | /// <param name="handleMethod">The handle method.</param> |
| | | 45 | | /// <param name="notificationContext">The notification context containing the notification and cancellation token.</ |
| | | 46 | | public static Task InvokeAsync(this INotificationHandler handler, MethodBase handleMethod, NotificationContext notif |
| | | 47 | | { |
| | 12733 | 48 | | var notification = notificationContext.Notification; |
| | 12733 | 49 | | var cancellationToken = notificationContext.CancellationToken; |
| | 12733 | 50 | | return (Task)handleMethod.Invoke(handler, [notification, cancellationToken])!; |
| | | 51 | | } |
| | | 52 | | |
| | | 53 | | /// <summary> |
| | | 54 | | /// Invokes the given handler for the given command. |
| | | 55 | | /// </summary> |
| | | 56 | | /// <param name="handler">The handler to invoke.</param> |
| | | 57 | | /// <param name="handleMethod">The handle method.</param> |
| | | 58 | | /// <param name="commandContext">The command to handle.</param> |
| | | 59 | | public static Task<TResult> InvokeAsync<TResult>(this ICommandHandler handler, MethodBase handleMethod, CommandConte |
| | | 60 | | { |
| | 33 | 61 | | var command = commandContext.Command; |
| | 33 | 62 | | var cancellationToken = commandContext.CancellationToken; |
| | 33 | 63 | | var task = (Task<TResult>)handleMethod.Invoke(handler, [command, cancellationToken])!; |
| | 33 | 64 | | return task; |
| | | 65 | | } |
| | | 66 | | } |