A workshop for hands-on introduction to Event Sourcing.
The fundamental idea of Event Sourcing is that changes to state (events) are persisted in sequence. As opposed to storing the current state. A common use case for Event Sourcing is to persist events from an aggregate.
Input/Output | Events | Commands | External |
---|---|---|---|
Events | Translator (ACL) | Process Manager | Projector (Projection) |
Commands | Aggregator (Aggregate) | Controller (Translator/ACL) | Actuator |
External | Sensor | Actor | System |
- .NET
- Visual Studio Code (or anything that can be used for dotnet development)
- .NET Core SDK 3.1
- C# for Visual Studio Code version 1.17.1 or later (If you are using VSCode and prefer C#)
- F#: Ionide-fsharp (If you are using VSCode and prefer F#)
- Node.js
- Visual Studio Code (or anything that can be used for Node development)
- Node.js with npm
In this workshop we will be working with the inventory item example. Assume this example is for a store and a user can be create, stock, sell, activate, and deactivate inventory items.
InventoryItem is a container that holds state.
inventoryItem:
id: e85c1ec3-f077-4625-adf2-5a593a293988
name: "Item 1"
count: 212
active: true
InventoryEvent defines the events that can happen to an InventoryItem state.
- Created:
InventoryId: e85c1ec3-f077-4625-adf2-5a593a293988
Name: "Item 1"
- Stocked:
Count: 123
- Sold:
Count: 43
- Activated
- Deactivated
InventoryCommand defines the commands that a user can ask to execute on an InventoryItem state.
- Create:
InventoryId: e85c1ec3-f077-4625-adf2-5a593a293988
Name: "Item 1"
- Stock:
Count: 123
- Sell:
Count: 43
- Activate
- Deactivate
The basic function to describe event sourcing is: (state, event) -> state'
This function is described as: given some state, when an event is applied, then a new state is given.
This function is often referred to as the Apply
function.
When utilizing commands the execute function is: (state, command) -> event
This function is describe as: given some state, when a command is executed, then an event is given.
This function is where your business logic belongs and is often referred to as the Execute
function.
Another function that is useful for event sourcing is an initialization function. () -> state
This function allows for a known initialization state before applying events.
This workshop is implemented as using Quality Assurance and test writing to verify an Event Sourcing implementation.
- Clone this repo and navigate to the directory of your chosen framework/language.
- Build if using dotnet
dotnet build
; Install packages if using nodenpm i
- Run the tests
dotnet test
ornpm test
- A number of the tests will fail.
- Open the
Domain.EventTests.fs
,EventTests.cs
, orevent.test.js
file under the tests directory. - Fix the broken event tests by following the
Given,When,Then
pattern of the test names. You will need to use the Apply function. - Open the
Domain.CommandTests.fs
,CommandTests.cs
orcommand.test.js
file under the tests directory. - Fix the broken command tests by following the
Given,When,Then
pattern of the test names. You will need to use the Execute function.
- Compensating Events
- Error Events