Data Retrieval and Caching for a Service's Entities

Entities are retrieved from stores.

In the example handler above, the account entity is retrieved along with the version:

account, current_version = store.fetch(account_id, include: :version)

But where is the entity? Where is it saved?

It's not a row in a table.

An entity (or model if you prefer) is just stored as a series of things that has happened to it. It doesn't need to be a database record that's constantly worked over, saving and retrieving, continually working it over with updated, and maybe even making it inconsistent with reality.

A ORM model is also the sum of its events. But with ORM we discard the reasons for the data being the way it is and keep the well-worn model with no real way to understand what series of events led to its current state (or it's state at any point in time, really).

When a store retrieves an entity, it's actually retrieving new events, and then applying them to the entity.

When a deposited event is applied to the account entity, the account's balance increases by the amount of the deposit. This would be done by invoking the entity's `deposit` method with the amount conveyed by the deposited event. A withdrawn event would decrease the account's balance, which is done by invoking the entity's `withdraw` method with the amount conveyed by the withdrawn event.

Only events that have been published since the last time that an entity was retrieved are read from storage. In practice, only a few or a handful of events are ever read in a single retrieval of an entity.