Working with Aggregates
Last updated
Last updated
Just as with Standard Aggregate, ES Aggregates are called by Command Handlers, however what they return are Events and they do not change their internal state.
When this Aggregate will be called via Command Bus with CreateProduct Command, it will then return new ProductWasCreated Event.
Command Handlers may return single events, multiple events or no events at all, if nothing is meant to be changed.
Aggregates under the hood make use of Partition persistence strategy (Refer to Working with Event Streams). This means that we need to know:
Aggregate Version
Aggregate Id
Aggregate Type
To find out about current version of Aggregate Ecotone will look for property marked with Version Attribute.
We don't to add this property directly, we can use trait instead:
Anyways, this is all we need to do, as Ecotone will take care of reading and writing to this property. This way we can focus on the business logic of the Aggregate, and Framework will take care of tracking the version.
We need to tell to Ecotone what is the Identifier of our Event Sourcing Aggregate. This is done by having property marked with Identifier in the Aggregate:
As Command Handlers are pure and do not change the state of our Event Sourcing Aggregate, this means we need a different way to mutate the state in order to assign the identifier. For changing the state we use EventSourcingHandler attribute, which tell Ecotone that if given Event happens, then trigger this method afterwards:
We will explore how applying Events works more in next section.
Aggregate Type will be the same as Aggregate Class. We can decouple the class from the Aggregate Type, more about this can be found in "Making Stream immune to changes" section.
So when this Command Handler happens:
What actually will happen under the hood is that this Event will be applied to the Event Stream:
As storing in Event Store is abstracted away, the code stays clean and contains only of the business part. We can customize the Stream Name, Aggregate Type and even Event Names when needed.