ADR: Commands & Handlers
Status
In Progress
Context
We currently have no commands nor handlers. Domain changes occur in controllers and services. There is no standardisation on these processes, no tests and no logging. This is difficult to maintain as we don't know what is going to break, no data on performance and poor re-usability. This architecture intends to improve in this regard as well as providing a domain interface to ease the development of new functionalities and AI control of the domain.
What is a Command?
A Command is something that someone wants to happen. Commands are dispatched before the thing has happened (indeed, they are what triggers the thing to happen).
Example: GridCloneCommand is dispatched → GridCloningHandler is provided that command and clones the grid → GridCloningHandler dispatches GridClonedEvent.
A Command represents a user intent. It's a domain change, it does not handle model integrity (should be handled in model). It may handle integrity between multiple models when it's not convenient to do so in a specific model, although it should be avoided to prevent shared responsibility leading to broken states.
A Command must encapsulate all the information needed to perform the change without including full models. A command is a Data Transfer Object (DTO), an object without any behaviour (a data structure). A command must be easily serialisable to ease transport.
Decision
Commands
Use Domain objects for static validation: - Validation on construct - Cannot exist in invalid state - Use GlobalIdentifiers, never ids if it can be avoided
Commands:
- Extend AppCommand
- Are final
- Return AppCommand::TYPE_COMMAND on getType
Operations:
- Extend AppCommand
- Are final
- Return AppCommand::TYPE_OPERATION on getType
Handlers
Open Question: Where should we perform permission checks for each type? - Outside (routes, controller) - Middleware - Command Handler
The closer to handler the more robust but the more checks we'll have.
Consequences
What becomes easier or more difficult to do because of this change?
[To be defined]
Impact
High
Driver
@Eudald Rossell Vivo
Contributors
[Team]
Accepted Date
[In Progress]