Bus for dispatching commands to their handlers.
Ensures that each command is handled by exactly one handler.
// Register handlercommandBus.register(CreateUserCommand, new CreateUserCommandHandler());// Dispatch commandconst user = await commandBus.dispatch<User>( new CreateUserCommand('user@example.com', 'John Doe')); Copy
// Register handlercommandBus.register(CreateUserCommand, new CreateUserCommandHandler());// Dispatch commandconst user = await commandBus.dispatch<User>( new CreateUserCommand('user@example.com', 'John Doe'));
Registers a handler for a specific command type.
The type of command
The result type
The command class or identifier
The handler for this command type
commandBus.register( CreateUserCommand, new CreateUserCommandHandler(repository)); Copy
commandBus.register( CreateUserCommand, new CreateUserCommandHandler(repository));
Dispatches a command to its registered handler.
The expected result type
The command to dispatch
The result from the command handler
Error if no handler is registered for the command
const command = new CreateUserCommand('user@example.com', 'John');const user = await commandBus.dispatch<User>(command); Copy
const command = new CreateUserCommand('user@example.com', 'John');const user = await commandBus.dispatch<User>(command);
Bus for dispatching commands to their handlers.
Ensures that each command is handled by exactly one handler.
Example