Last updated on June 28, 2020

Command Design Pattern

👋 Hello! Most of these notes are "knowledge in the making", so parts of them could be incomplete 🙈, inaccurate 🙉 or even potentially wrong 🙊.

Encapsulates a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations1. It also serves to decouple the implementation of the request from the requestor2.


[2020-05-10 Sun 23:20]

On previous projects I've worked on, this approach created some problems because of the way it was implemented and used. Some problems I remember are:

  • Most (if not all) commands were expected to return something.
  • Controllers were calling services which, inside, were dispatching commands which were calling other services, and so on. The whole flow was hard to understand and to follow.
  • Command handlers did not support throwing events when the operation was done, thus adding behavior on command completion was not supported, and required coupling the actual command to all these other behaviors.

Open Questions

  • Under which circumstances is it acceptable to expect a return value from commands, and under which circumstances is it not? I know, for example, that Brighter supports certain uses cases where you can mutate the command itself to communicate something back to the caller (e.g. a creation API which needs to return the newly-created resource).