Gary F
Gary F

Reputation: 510

Event Sourcing - Data supplied to Command and data saved in Events

I am looking into event sourcing and I have a few thoughts that I need to get my head round.

Take for example an online shop -

The customer adds an item to the basket and saves their order.

The command handler could create an order on the customer aggregate root and add an OrderCreated event which contained the customer id, order id, item id, quantity and unit price. All pretty straight forward but what if the aggregate needed to check to see if that item was on special offer?

If this was for example a basket service would it subscribe to events from the catalog service and store it's own projections of the catalog service which it could then use, so then the basket service would comprise an event store and also some form of projection of the catalog service?

Or if in the example I've just described, if the basket and catalog functionality were part of the same application and they only held event data, then when a customer creates an order the handler would pull all ordered items from the event store via a repository, apply all events to them and then return them to the handler in order to check if the item was on special offer.

Upvotes: 2

Views: 240

Answers (1)

rascio
rascio

Reputation: 9279

what if the aggregate needed to check to see if that item was on special offer?

It needs to execute a query to get the information it needs.

From the aggregate point of view those data are external, so it (or the handler sending the command to it) needs a query to access that information.

How the query work is up to you (there are pros and cons for each way), the query handler can:

  • Call a repository that loads an aggregate and check for the special offer (you can also think to have event sourcing in just one part of your system and having this part using another way to store dara, or not)
  • Make a remote call to the Catalog Service API to check for the special offer
  • Do a query to a local db, that is populated reading events emitted by the catalog service and stored "locally" to the basket service

Upvotes: 3

Related Questions