The chapter frames persistence through a functional lens: instead of treating a database as mutable global state, it argues for immutable, append-only storage. It contrasts two styles of immutable persistence—assertion-based and event-based—but emphasizes event sourcing as the more accessible and widely adopted approach in .NET. The core idea is that systems should record what happened over time rather than repeatedly overwriting the latest state, preserving history, reducing contention, and making past behavior available for analysis.
Event sourcing is presented as a shift in focus from states to state transitions. Events are simple, immutable, serializable data objects that describe past occurrences, while states are lightweight snapshots used to make decisions and to support queries. State transitions are modeled as pure functions that take a current state and an event to produce a new state, and the current entity state can be reconstructed by replaying its event history. The chapter shows this with a bank-account example, including how to store events, derive state from them, and handle creation as a special case.
At the architectural level, an event-sourced system is split into a command side and a query side. The command side validates incoming commands, converts them into events, persists and publishes those events, and computes the resulting state using the same transition logic used during replay. The query side builds user-facing view models from event histories, often using aggregation and filtering to create structures like account statements, and may cache these projections for performance. The chapter closes by comparing event sourcing with assertion-based storage and noting that the best choice depends on the domain: event sourcing fits naturally when events drive meaningful change and command/query needs differ sharply, while more CRUD-like domains may be better served by assertion-based storage.
So-called stateless servers usually rely on a big blob of mutable data called the database.
An entity can be viewed as a sequence of logically related states. The identity of the entity remains the same, but the state changes as the result of events that affect the entity’s state.
Event sourcing implies a shift of focus in thinking about entities. Instead of concentrating on an entity’s state, we concentrate on the transitions that bring about the new state.
Recovering the present state of an entity from its event history
High-level comparison of data flows in a traditional vs. event-sourced system
The command side of an event-sourced system
Event handlers can subscribe to event streams published by Event Store.
Example structure of a bank statement
Summary
- Thinking functionally about data also encompasses storage. Instead of mutating stored data, consider the database as a big immutable collection: you can append new data but never overwrite existing data.
- There are two main approaches to immutable storage:
- Event sourcing means persisting event data as events occur. The state of an entity need not be stored because it can always be computed as the “sum” of all events that affected the entity.
- An event-sourced system naturally separates the concerns of reading and writing data, enabling a CQRS architecture that separates between
- Event-sourced systems include the following components:
- Commands—Simple, immutable data objects encapsulating a request from a client.
- Events—Simple, immutable data objects capturing what happened.
- States—Data objects representing the state of an entity at a certain point in time.
- State transitions—Functions that take a state and an event and produce a new state.
- View models—Data objects for populating views. They’re computed from events.
- Event handlers—These subscribe to events to perform business logic (on the command side) or to update view models (on the query side).
FAQ
What is the main functional-programming idea behind event sourcing?
Event sourcing treats persisted data as immutable. Instead of updating or deleting state in place, the system appends new events, and the current state is derived by replaying those events.Why does the chapter recommend append-only storage?
Append-only storage preserves history, supports auditability, avoids overwriting valuable information, and reduces database contention because existing records are never modified in place.What is the difference between an event and a state snapshot?
An event records something that happened at a point in time, while a state snapshot represents the entity at a given moment. In event sourcing, state is secondary and can be reconstructed from the event history.How are events modeled in the chapter?
Events are represented as simple, immutable data objects that capture the minimum information needed to describe what happened. They must also be serializable because they are persisted to storage.How does the chapter represent state transitions?
State transitions are modeled as functions with the shapestate → event → state. In the example, an AccountState and an Event are combined to produce a new AccountState.Why is the first event in an event history treated specially?
The first event usually creates the entity, so there is no prior state to apply it to. That is why creation is handled with a special function such asCreate(CreatedAccount) before applying later events.How is an entity’s current state reconstructed from its event history?
The chapter usesAggregate to replay the event stream. The first event creates the initial state, and each subsequent event is applied in order to compute the current state.Why are command handling and query handling separated in an event-sourced system?
The command side validates input, turns commands into events, and persists them. The query side builds view models from events for display to users. This separation is commonly described as CQRS.What is the purpose of the Do function in the command workflow?
Do performs a side effect in the middle of a functional pipeline without losing the value flowing through it. In the chapter, it is used to call saveAndPublish while still passing the result onward.
Functional Programming in C#, Second Edition ebook for free