TaskHub.Shared

Overview

IPostBehavior defines a post-processing step in the command execution pipeline. It is invoked after the main command handler. It’s best place to perform actions such as logging, auditing, notifications and tasks that should occur after the command has been processed.

Package

TaskHub.Shared.Commands.Abstractions

Namespace

TaskHub.Shared.Commands.Abstractions.Behavior

Definition

public interface IPostBehavior<in TCommand, in TResult>
    where TCommand : ICommand
    where TResult : Result
{
    Task HandleAsync(TCommand command, TResult result, CancellationToken ct);
}

Usage Example

public class SaveBehavior<TCommand, TResult>(IJobUnitOfWork unit) : IPostBehavior<TCommand, TResult>
    where TCommand : IJobCommand
    where TResult : Result
{
    public async Task HandleAsync(TCommand cmd, TResult result, CancellationToken ct)
    {
        if (result.IsSuccess)
        {
            await unit.SaveAsync(ct);
        }
    }
}

Registration

Post-behaviors can be registered in the DI container.

builder.Services.AddScoped(typeof(IPostBehavior<,>), typeof(SaveBehavior<,>));