TaskHub.Shared

Overview

ICommandHandler defines the main handler for a command in the command execution pipeline. It is responsible for executing the core business logic associated with the command.

Package

TaskHub.Shared.Commands.Abstractions

Namespace

TaskHub.Shared.Commands.Abstractions.Handler

Definition

public interface ICommandHandler<in TRequest, TResponse>
    where TRequest : ICommand
    where TResponse : Result
{
    Task<TResponse> HandleAsync(TRequest request, CancellationToken ct);
}

Usage Example

public class SomeHandler(IWriteRepository rep) : ICommandHandler<SomeCommand, Result>
{
    public async Task<Result> HandleAsync(SomeCommand cmd, CancellationToken ct)
    {
        if (isSuccess)
        {
            return ResultFactory.Success;
        }
     
        return ResultFactory.ItemNotFound;
    }
}

Registration

Command handlers can be registered in the DI container.

services.AddScoped<ICommandHandler<TCommand, Result>, THandler>();