forked from Sairyss/domain-driven-hexagon
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand-handler.base.ts
More file actions
27 lines (24 loc) · 849 Bytes
/
command-handler.base.ts
File metadata and controls
27 lines (24 loc) · 849 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import { UnitOfWorkPort } from '../ports/unit-of-work.port';
import { Result } from '../utils/result.util';
import { Command } from './command.base';
export abstract class CommandHandlerBase<
CommandHandlerReturnType = unknown,
CommandHandlerError extends Error = Error
> {
constructor(protected readonly unitOfWork: UnitOfWorkPort) {}
// Forces all command handlers to implement a handle method
abstract handle(
command: Command,
): Promise<Result<CommandHandlerReturnType, CommandHandlerError>>;
/**
* Execute a command as a UnitOfWork to include
* everything in a single atomic database transaction
*/
execute(
command: Command,
): Promise<Result<CommandHandlerReturnType, CommandHandlerError>> {
return this.unitOfWork.execute(command.correlationId, async () =>
this.handle(command),
);
}
}