Class BaseCommandHandler<TCommand, TResult>Abstract

Base class for command handlers with built-in validation and error handling.

Provides a template method pattern with hooks for validation and execution. Automatically handles Result types and throws on failure.

class CreateProductHandler extends BaseCommandHandler<CreateProductCommand, Product> {
constructor(private repository: IProductRepository) {
super();
}

protected validate(command: CreateProductCommand): Result<void, DomainError> {
if (!command.name) {
return Failure.create(new DomainError('INVALID_NAME', 'Name is required'));
}
return Success.create(undefined);
}

protected async execute(command: CreateProductCommand): Promise<Result<Product, DomainError>> {
const product = Product.create(command.name, command.price);
await this.repository.save(product);
return Success.create(product);
}
}

Type Parameters

  • TCommand extends Command

    The command type

  • TResult

    The result type

Implements

Constructors

Methods

Constructors

Methods