Reputation: 91
I really like the concept of event sourcing. To replay specific events to calculate a domain model state is just wonderful.
When i read into Event Sourcing, i am still confused by the term "Aggregate". I understood that the Aggregate as a "Collection of Events related to a specific thing".
Different to this, a "Projection" is a kind of a Event Handler which is called when a event (which is supported by the Projector) is fired. When a event stream is replayed, the Projector may handle this events (again).
An event handler (Projector) can react to an event. For example, there is a ERP Application where the employee can ship, reserve or release a product. If a product is shipped, a new Event called ProductShipped is fired (and persisted to the database). The projector, which acts as the event handler, come into place and decrease the available stock quantity for that product.
The aggregate can act as a validator, if i read correctly. So if i look at all past events related to that product i am able to calculate the quantity. This calculation has to be done before the ProductShipped Event is persisted, and this is where is see the role of an Aggregate.
If i am right, when does the Aggregator show up? In a naive solution, i would inject a ProductStockAggregate into the projector and the projector now has to call it to get information about the available quantity. But this seems weird as the projector has to replay all the events (related to the SKU) just to get this information. Additionally, this seems as a violation to the separations of concerns principle.
In some tutorials i found out that the aggregate itself is the "Source of Control": If the employee ships a product, the application will call the method ProductStockAggregate.shipProduct(productId). This method would replay all past events related to that product and decides if the product is shipable (based on quantity availablity). The decision results in a new event (ProductShipped or ProductQuantityTooLow), which has to be handled by the application. But if this is true, there is also a Separation of Concerns violation as the Aggregate has to know about the events which has to be fired, the product model itself and also the logic to enforce business rules. Looks like a god object.
But I tend to think that I didn't really understand something in particular. Can you help me?
Upvotes: 2
Views: 1759
Reputation: 57277
When i read into Event Sourcing, i am still confused by the term "Aggregate". I understood that the Aggregate as a "Collection of Events related to a specific thing".
"Aggregate" in event sourcing means the same thing as "Aggregate" in domain driven design.
It's a pattern that was identified in "the blue book" (Domain Driven Design: Tackling Complexity at the Heart of Software -- Eric Evans, 2003); more specifically, it's a lifecycle management pattern (chapter 6).
An AGGREGATE is a cluster of associated objects that we treat as a unit for the purpose of data changes.
We're really talking about objects in our domain model here; primarily the entities that manage change of information in our domain, but also of course we would include here objects that hold values for those entities.
Reviewing Evans is a Really Good Idea[tm] when learning about event sourcing, because a significant portion of the folks talking about (and doing) event sourcing came to it through a lineage with roots in DDD.
"Collection of events" isn't quite right, but is an understandable confusion, given the history of things and the available literature.
Event sourcing is about using histories of events as our data model (and in particular, using histories of events as the persisted representation of information in the domain).
Where things get tangled is the "treat as a unit" bit; treating an aggregate as a unit adds some constraints to how we load and store the aggregates data - in particular, we need to ensure that changes to that data are atomic (all changes saved or no changes saved). When your storage appliance is an "event store" that only allows you to save one stream of events (history) at a time, it becomes natural to align "aggregate" and "stream".
Upvotes: 4