Event Sourcing Repository
Last updated
Was this helpful?
Last updated
Was this helpful?
Ecotone comes with inbuilt Event Sourcing repository after . However you want to roll out your own storage for Events, or maybe you already use some event-sourcing framework and would like to integrate with it. For this you can take over the control by introducing your own Event Sourcing Repository.
Using Custom Event Sourcing Repository will not allow you to make use of . Therefore consider configuring your own Event Sourcing Repository only if you want to build your own projecting system.
We do start by implementing EventSourcingRepository interface:
canHandle - Tells whatever given Aggregate is handled by this Repository
findBy - Method returns previously created events for given aggregate. Which Ecotone will use to reconstruct the Aggregate.
save - Stores events recorded by Event Sourced Aggregate
and then we need to mark class which implements this interface as Repository
Ecotone provides enough information to decide how to store provided events.
Identifiers will hold array of identifiers related to given aggregate (e.g. ["orderId" ⇒ 123]). Events will be list of Ecotone's Event classes, which contains of payload and metadata, where payload is your Event class instance and metadata is specific to this event. Metadata as parameter is generic metadata available at the moment of Aggregate execution. Version before handling on other hand is the version of the Aggregate before any action was triggered on it. This can be used to protect from concurrency issues.
The structure of Events is as follows:
It's worth to mention about Ecotone's Events and especially about metadata part of the Event. Each metadata for given Event contains of three core Event attributes:
"_aggregate_id" - This provides aggregate identifier of related Aggregate
"_aggregate_version" - This provides version of the related Event (e.g. 1/2/3/4)
"_aggregate_type" - This provides type of the Aggregate being stored, which can be customized
If our repository stores multiple Aggregates is useful to have the information about the type of Aggregate we are storing. However keeping the class name is not best idea, as simply refactor would break our Event Stream. Therefore Ecotone provides a way to mark our Aggregate type using Attribute
This now will be passed together with Events under _aggregate_type metadata.
In Ecotone we can name the events to avoid storing class names in the Event Stream, to do so we use NamedEvent.
then when events will be passed to save method, they will automatically provide this name under eventName property.
Ecotone then after fetching snapshot, will load events only from this given moment using `fromAggregateVersion`.
If you want to test out your flow and storing with your custom Event Sourced Repository, you should disable default in memory repository
With custom repository we still can use inbuilt . To use it for customized repository we will use BaseEventSourcingConfiguration.