Aggregate Invariants relying on State

I have an aggregate that has a time scope ensured by aggregate identifier like “customer-1234-2022W02” for all products sold in a week

For some invariants I have too keep a list of price-request that maps as N-1 with an order

How can I hydrate those prices into the aggregate state without add them in the events as the price request has no relation with any event?

  1. Create temporary Order containing a list of prices request
  2. Order item added ( here the price request takes precedence over the item just added)
  3. Order Confirmed

Aggregate: Order(identifier, orderId, date, customerId, listOf(price), listOf(items))

Extra question: There is any other strategy to create a time-scoped-aggregate?

Would the event source handler’s metadata a way to communicate the re-hydrated request-price State field to not pollute events’ payload and deturpe its intention?

Has the metadata size limits?

Hi @habutre,

If I understand correctly you have multiple aggregates that are invariants of each other. I sense that besides the ProductsSoldPerWeek aggregate there are aggregates as Products (with prices) and Orders (referencing products).

How can I hydrate those prices into the aggregate state without add them in the events as the price request has no relation with any event?’

I would model the price into the Order, so you have the price at that exact moment (a fact) for that product in your order. This way that fact does not need to be looked up. And Order is a fact, and product pricing changes afterwards probably don’t affect it.

As for the invariant aggregate for the time-scope, to me it feels like you can solve it in three ways:

  • Create a query model, with customer , weeks and product as keys, so it can be queried by them
  • Create a Saga that feeds the ProductsSoldPerWeek based on Order events. This can saga can either look up the prices and send them in the command, or the prices in the events can be sent.
  • Create a query model of prices, and inject the repository that can be used to query that in your command handler. This will prolong command handler execution and slow calls are not recommended.

Has the metadata size limits?

As this is domain data for you, I would not store pricing information in MetaData. If you are using a SQL store, the metadata size is limited by your database.

I hope this answers most of your questions, I’m available if there are any more.

Thank you @Morlack your proposal with aggregates fits what I need. My mistake was not think Product as an Aggregate collaborating on my Root Aggregate

1 Like