Skip to content

Latest commit

 

History

History
50 lines (29 loc) · 2.92 KB

File metadata and controls

50 lines (29 loc) · 2.92 KB
description
Using Event Sourcing in PHP

Event Sourcing Introduction

Before diving into this section be sure to understand how Aggregates works in Ecotone based on previous sections.

Difference between Aggregate Types

Ecotone provides higher level abstraction to work with Event Sourcing, which is based on Event Sourced Aggregates.
Event Sourced Aggregate just like normal Aggregates protect our business rules, the difference is in how they are stored.

State-Stored Aggregates

Normal Aggregates are stored based on their current state:

State-Stored Aggregate State

Yet if we change the state, then our previous history is lost:

Price was changed, we don't know what was the previous price anymore

Having only the current state may be fine in a lot of cases and in those situation it's perfectly fine to make use of State-Stored Aggregates. This is most easy way of dealing with changes, we change and we forget the history, as we are interested only in current state.

{% hint style="success" %} When we actually need to know what was the history of changes, then State-Stored Aggregates are not right path for this. If we will try to adjust them so they are aware of history we will most likely complicate our business code. This is not necessary as there is better solution - Event Sourced Aggregates. {% endhint %}

Event Sourcing Aggregate

When we are interested in history of changes, then Event Sourced Aggregate will help us.
Event Sourced Aggregates are stored in forms of Events. This way we preserve all the history of given Aggregate:

Event-Sourced Aggregate

When we change the state the previous Event is preserved, yet we add another one to the audit trail (Event Stream).

Price was changed, yet we still have the previous Event in audit trail (Event Stream)

This way all changes are preserved and we are able to know what was the historic changes of the Product.

Event Stream

The audit trail of all the Events that happened for given Aggregate is called Event Stream.
Event Stream contains of all historic Events for all instance of specific Aggregate type, for example all Events for Product Aggregate

Product Event Stream

Let's now dive a bit more into Event Streams, and what they actually are.