Segregate operations that read data from operations that update data by using separate interfaces. This pattern can maximize performance, scalability, and security; support evolution of the system over time through higher flexibility; and prevent update commands from causing merge conflicts at the domain level. The term sounds heavy, but the concept is not. At least, this is what we discovered at the end of our regular KnolX session that we have at the Knoldus office every Wednesday.
The main ideas are summarized as
- A method should either change state or return a result, not both
- Asking a question should not change the answer (SRP + Immutability) – No side effects
- Commands – change the state of an object or entire system (sometimes called as modifiers or mutators).
- Queries – return results and do not change the state of an object.
In the real world,
- Methods which return something are mostly queries – specialized reporting needs
- Methods which return unit are commands – domain with all the behavior
Looks interesting, go through the slide deck and video below