Skip to content

Middleware pipeline

Adam Bajguz edited this page Aug 23, 2020 · 25 revisions

Typin uses middleware pipeline to execute commands. By default it consists only of Typin middlewares that are always executed at the end of the pipeline, just before IMiddlewareExtensions.PipelineTermination action.

typin-middleware-pipeline

Writing custom middlewares

To define a middleware, just create a new class that implements the IMiddleware interface:

public sealed class CommandExecution : IMiddleware
{
    public CommandExecution()
    {

    }

    public async Task HandleAsync(ICliContext context, CommandPipelineHandlerDelegate next, CancellationToken cancellationToken)
    {
        context.Console.Output.WriteLine("-- Before command execution --");

        await next();

        context.Console.Output.WriteLine("-- After command execution-- ");
    }
}

To implement IMiddleware, the class needs to define an HandleAsync method. This is the method that gets by called Typin when the pipeline is executed.

Clone this wiki locally