O'Reilly logo

Domain Modeling Made Functional by Scott Wlaschin

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required