Adding the Event Sourcing Package
Explore how to implement event sourcing in Golang by creating event-sourced aggregates with embedded base aggregates, constructors, and versioning controls. Learn to use events as the source of truth, apply domain events properly, and handle event versioning for maintaining aggregate history.
The updates made to enhance the events did not add any new code to support event sourcing. Because we do not want to turn every aggregate in the application into an event-sourced aggregate, the bits necessary to support event sourcing will go into a new internal/es package.
Creating the event-sourced aggregate
To avoid having to build the aggregate from scratch, this new aggregate will contain an embedded ddd.Aggregate and provide a new constructor. Here is what we are starting with, the event-sourced Aggregate definition:
Adding a constructor
This new Aggregate will also need a constructor:
func NewAggregate(id, name string) Aggregate {return Aggregate{Aggregate: ddd.NewAggregate(id, name),version: 0,}}
Adding the es.Aggregate struct
The purpose of es.Aggregate struct is to layer on the versioning controls required to work with the event-sourced aggregates. It accomplishes this by embedding ddd.Aggregate. The AddEvent() ...