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.
TaskHub.Shared.Commands.Abstractions
TaskHub.Shared.Commands.Abstractions.Behavior
public interface IPostBehavior<in TCommand, in TResult>
where TCommand : ICommand
where TResult : Result
{
Task HandleAsync(TCommand command, TResult result, CancellationToken ct);
}
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);
}
}
}
Post-behaviors can be registered in the DI container.
builder.Services.AddScoped(typeof(IPostBehavior<,>), typeof(SaveBehavior<,>));