Command-Query Separation

The next principle we’ll look at is command-query separation, or CQS.

In the functional approach to domain modeling, all our objects are designed to be immutable. Let’s think of the storage system as some kind of immutable object too. That is, every time we change the data in the storage system, it transforms into a new version of itself.

For example, if we want to model inserting a record in a functional way, we can think of the insert function as having two parameters: the data to insert and the original state of the data store. The output of the function, after the insert is completed, is a new version of the data store with the data added to it.

In code, we could model it using this type signature:

 type​ InsertData ...

Get Domain Modeling Made Functional now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.