Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CQRS aggregates

I'm new to the CQRS/ES world and I have a question. I'm working on an invoicing web application which uses event sourcing and CQRS.

My question is this - to my understanding, a new command coming into the system (let's say ChangeLineItemPrice) should pass through the domain model so it can be validated as a legal command (for example, to check if this line item actually exists, the price doesn't violate any business rules, etc). If all goes well (the command is not rejected) - then the appropriate event is created and stored (for example LineItemPriceChanged)

The thing I didn't quite get is how do I keep this aggregate in memory to begin with, before trying to apply the command. If I have a million invoices in the system, should I playback the whole history every time I want to apply a command? Do I always save the event without any validations and do the validations when constructing the view models / projections?

If I misunderstood any part of the process I would appreciate your feedback.

Thanks for your help!

like image 712
amitayh Avatar asked Jul 16 '15 18:07

amitayh


People also ask

What are aggregates in CQRS?

Concretely, an aggregate will handle commands, apply events, and have a state model encapsulated within it that allows it to implement the required command validation, thus upholding the invariants (business rules) of the aggregate.

What are aggregates in Event Sourcing?

An aggregate is a cluster of associated objects that we treat as a unit for the purpose of data changes. Each aggregate has a root and a boundary. The boundary defines what is inside the aggregate. The root is a single, specific entity contained in the aggregate.

What are aggregates in Microservices?

Aggregator Microservice collects pieces of data from various microservices and returns an aggregate for processing. Aggregator Microservice invokes multiple services to achieve the functionality required by the application.

Can a Microservice have multiple aggregates?

Each Microservice uses its own database to persist data of domain model of a Bounded Context. A single Bounded Context can include many aggregate roots, or we can organise a single aggregate root into a single Bounded Context.


1 Answers

It is right that you have to replay the event to 'rehydrate' the domain aggregate. But you don't have to replay all events for all invoices. If you store the entity id of the root aggregate in the events, you can just select and replay the events that with the relevant id.

Then, how do you find the relevant aggregate root id? One of the read repositories should contain the relevant information to get the id, based on a set of search criteria.

like image 105
Marc Enschede Avatar answered Sep 25 '22 20:09

Marc Enschede