< Summary

Information
Class: Elsa.Mediator.Extensions.HandlerExtensions
Assembly: Elsa.Mediator
File(s): /home/runner/work/elsa-core/elsa-core/src/common/Elsa.Mediator/Extensions/HandlerExtensions.cs
Line coverage
100%
Covered lines: 14
Uncovered lines: 0
Coverable lines: 14
Total lines: 66
Line coverage: 100%
Branch coverage
100%
Covered branches: 6
Total branches: 6
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
GetNotificationHandlerMethod(...)100%11100%
GetCommandHandlerMethod(...)100%66100%
InvokeAsync(...)100%11100%
InvokeAsync(...)100%11100%

File(s)

/home/runner/work/elsa-core/elsa-core/src/common/Elsa.Mediator/Extensions/HandlerExtensions.cs

#LineLine coverage
 1using System.Diagnostics.CodeAnalysis;
 2using System.Reflection;
 3using Elsa.Mediator.Contracts;
 4using Elsa.Mediator.Middleware.Command;
 5using Elsa.Mediator.Middleware.Notification;
 6using Elsa.Mediator.Models;
 7
 8namespace Elsa.Mediator.Extensions;
 9
 10/// <summary>
 11/// Contains helper methods for invoking notification handlers.
 12/// </summary>
 13public 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    {
 1131622        var handlerType = typeof(INotificationHandler<>).MakeGenericType(notificationType);
 1131623        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    {
 3333        var commandTypeInterfaces = commandType.GetInterfaces();
 7734        var commandTypeInterface = commandTypeInterfaces.FirstOrDefault(x => x.IsGenericType && x.GetGenericTypeDefiniti
 3335        var resultType = commandTypeInterface?.GetGenericArguments()[0] ?? typeof(Unit);
 3336        var handlerType = typeof(ICommandHandler<,>).MakeGenericType(commandType, resultType);
 3337        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    {
 1273348        var notification = notificationContext.Notification;
 1273349        var cancellationToken = notificationContext.CancellationToken;
 1273350        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    {
 3361        var command = commandContext.Command;
 3362        var cancellationToken = commandContext.CancellationToken;
 3363        var task = (Task<TResult>)handleMethod.Invoke(handler, [command, cancellationToken])!;
 3364        return task;
 65    }
 66}