< Summary

Information
Class: Elsa.Mediator.Middleware.Command.MiddlewareExtensions
Assembly: Elsa.Mediator
File(s): /home/runner/work/elsa-core/elsa-core/src/common/Elsa.Mediator/Middleware/Command/MiddlewareExtensions.cs
Line coverage
90%
Covered lines: 9
Uncovered lines: 1
Coverable lines: 10
Total lines: 45
Line coverage: 90%
Branch coverage
100%
Covered branches: 2
Total branches: 2
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
UseMiddleware(...)100%11100%
UseMiddleware(...)100%210%
BuildMiddlewareDelegate(...)100%22100%

File(s)

/home/runner/work/elsa-core/elsa-core/src/common/Elsa.Mediator/Middleware/Command/MiddlewareExtensions.cs

#LineLine coverage
 1using Elsa.Mediator.Middleware.Command.Contracts;
 2using Microsoft.Extensions.DependencyInjection;
 3
 4namespace Elsa.Mediator.Middleware.Command;
 5
 6/// <summary>
 7/// Provides extension methods for <see cref="ICommandPipelineBuilder"/>.
 8/// </summary>
 9public static class MiddlewareExtensions
 10{
 11    /// <summary>
 12    /// Adds middleware to the pipeline.
 13    /// </summary>
 14    public static ICommandPipelineBuilder UseMiddleware<TMiddleware>(this ICommandPipelineBuilder builder, params object
 15    {
 416        return builder.Use(next => BuildMiddlewareDelegate<TMiddleware>(builder, next, args));
 17    }
 18
 19    /// <summary>
 20    /// Inserts middleware at a specific index in the pipeline.
 21    /// </summary>
 22    public static ICommandPipelineBuilder UseMiddleware<TMiddleware>(this ICommandPipelineBuilder builder, int index, pa
 23    {
 024        return builder.Use(index, next => BuildMiddlewareDelegate<TMiddleware>(builder, next, args));
 25    }
 26
 27    /// <summary>
 28    /// Builds a delegate for the middleware type.
 29    /// </summary>
 30    private static CommandMiddlewareDelegate BuildMiddlewareDelegate<TMiddleware>(
 31        ICommandPipelineBuilder builder,
 32        CommandMiddlewareDelegate next,
 33        object[] args
 34    ) where TMiddleware : ICommandMiddleware
 35    {
 236        var middleware = typeof(TMiddleware);
 237        var invokeMethod = MiddlewareHelpers.GetInvokeMethod(middleware);
 238        var ctorParams = new[]
 239        {
 240            next
 441        }.Concat(args).Select(x => x!).ToArray();
 242        var instance = ActivatorUtilities.CreateInstance(builder.ApplicationServices, middleware, ctorParams);
 243        return (CommandMiddlewareDelegate)invokeMethod.CreateDelegate(typeof(CommandMiddlewareDelegate), instance);
 44    }
 45}