Command
Escrito por Darjus Kazakevic · revisado el
Make the call itself a value.
A method call happens and is gone. You cannot put it in a queue, write it to an audit log, send it to another worker, or ask it to undo itself. It was never a thing, only a moment on the stack.
Command makes the call data: a small object with a type and arguments, plus a handler that runs it. Now you can queue, log, retry and undo it. In TypeScript the command is usually a plain object type, and the handlers are a map of functions keyed by command type.
En esencia — Turn a request into an object that holds the intent and its arguments, so the call can be queued, logged, retried or undone like any other data.
Cuándo recurrir a él
Recurre a él cuando
- Work must be queued, retried, delayed or run on another worker
- Users need undo, or every change needs an audit entry
- HTTP, CLI and scheduled jobs trigger the same actions
Reconsidéralo cuando
- A call made once, synchronously, and never looked at again
- As undo for effects that leave your system, like a sent email
- A thin CRUD app, where the extra types only add files
Conceptos clave
- Command
- A plain object that names an intent in the imperative, with its arguments. It is data, so it can be stored, sent and replayed.
- Handler
- The function that executes one type of command. It loads what it needs, does the work, and may record what undo would need.
- Dispatcher
- One function that runs any command the same way: log it, find its handler, run it, keep history. Work every command needs is written there once.
- Undo
- To undo, a command must record the previous state when it runs. Undo only works for effects inside your system, not for a sent email or a card charge.
- In modern TypeScript
- A discriminated union of command objects plus a map of handlers replaces the class-per-command form. A mapped type makes the compiler check that every command has a handler.
Reglas de diseño
- Keep queued commands plain data
- A command that crosses a queue or a log must serialise to JSON: a type and its arguments. Carry ids, not loaded objects or connections, and let the handler load what it needs.
- Capture what undo needs when it runs
- A command can only be reversed if it saved the old value when it ran. If undo reads the old value later, it reads the new one, and restores nothing.
- Make retried commands idempotent
- A queued command may run twice after a timeout. Give it an id and let the handler skip ids it has already processed, so a retry does not charge a card twice.
- One handler per command type
- A command asks for one thing, so exactly one handler runs it. If several parts of the system should react, raise an event after the command succeeds.
Errores habituales
- Loaded objects or database connections inside a queued command. It cannot be serialised, or it is out of date when it runs.
- Undo that reads the old state at undo time instead of execute time, so it restores the wrong value.
- Several handlers for one command. That is an event, and the sender no longer gets one clear result.
- Handlers that are not idempotent behind a retrying queue. The symptom: a customer charged twice after a timeout.