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.
TaskHub.Shared.Commands.Abstractions
TaskHub.Shared.Commands.Abstractions.Handler
public interface ICommandHandler<in TRequest, TResponse>
where TRequest : ICommand
where TResponse : Result
{
Task<TResponse> HandleAsync(TRequest request, CancellationToken ct);
}
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;
}
}
Command handlers can be registered in the DI container.
services.AddScoped<ICommandHandler<TCommand, Result>, THandler>();