The Command encapsulates the intent to perform an action and carry the necessary data for that action.
A command is implemented a data transfer object with the name of an operation and the data required to perform that operation.
Name
The Name is used to name the Command and CommandHandler. It also sets the folder and infrastructure name for the Usecase.
For more details, see Naming.
Schema
The properties of the Command Payload are planned with the Codebricks Planner.
These properties of the Payload are Value Objects.
For more details, see Schema.
Value Sources
The Command Schema Property Value Sources define how each property is initialized.
For more details, see Data Flow.
Addtionally Command can use the following Metadata Value Sources:
Commands are implemented in the application layer to validate information and transport them to the domain layer. They contain ValueObjects to ensure that information is validated by business rules.
The command file is located at: Task/src/useCases/write/AddTask/application/AddTaskCommand.ts
1import { TaskTitleValueObject } from "shared/domain/valueObjects/TaskTitleValueObject";2import { TaskDescriptionValueObject } from "shared/domain/valueObjects/TaskDescriptionValueObject";3import { AssigneeIdValueObject } from "shared/domain/valueObjects/AssigneeIdValueObject";45export class AddTaskCommand {6 constructor(readonly payload: AddTaskCommandPayload) {7 }8}910export interface AddTaskCommandPayload {11 title: TaskTitleValueObject;12 description: TaskDescriptionValueObject;13 assigneeId: AssigneeIdValueObject;14}
Parameters and Properties
Commands are processed by their respective command handlers. The command handler is responsible for executing all application logic.
The command handler file is located at: Task/src/useCases/write/AddTask/application/AddTaskCommandHandler.ts
1import { OverwriteProtectionBody, NotFoundError, PreconditionFailedError, UuidGenerator } from "@codebricks/typebricks";2import { TaskAggregate } from "shared/domain/aggregate/TaskAggregate";3import { AddTaskCommand } from "./AddTaskCommand";4import { TaskRepository } from "shared/infrastructure/persistence/aggregate/TaskRepository";5import { TaskPublisherTrigger } from "shared/infrastructure/publishing/TaskPublisherTrigger";67export class AddTaskCommandHandler {8 constructor(readonly repository: TaskRepository = new TaskRepository(), readonly publisher: TaskPublisherTrigger = new TaskPublisherTrigger()) {9 }1011 @OverwriteProtectionBody(false)12 async handleAddTask(command: AddTaskCommand): Promise<TaskAggregate> {13 const aggregate: TaskAggregate = new TaskAggregate(UuidGenerator.uuid());14 await aggregate.addTask({15 title: command.payload.title,16 description: command.payload.description,17 assigneeId: command.payload.assigneeId,18 });19 await this.repository.save(aggregate);20 await this.publisher.trigger();2122 return aggregate;23 }24}
Key Concepts of Command Handler