Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DDD, CQRS, Event Sourcing: Where do entities come from?

I'm a bit confused about the origin of entities in an environment which takes advantage of CQRS & Event Sourcing. To make my question clear let's take the following well known example of a web shop:

You may model an order as an aggregate root. An order accepts order lines which themselves are defined by a product and a quantity.

Since an order line is an entity which is constructed during the order process itself there is still the notion of a product which seems to be an entity as well. But where does the product or even the catalog of products come from? In my opinion there is no such a thing like a product aggregate root in the bounded context of an order. How then would the order context know about product entities? Are they maintained in another bounded context and somehow materialized in the read store of the order context?

like image 741
Patrick Ineichen Avatar asked Oct 20 '22 01:10

Patrick Ineichen


1 Answers

In the BC that contains Order it might indeed be the case that Product, as part of an OrderLine, is a value object, consisting of values such as ProductId, Name etc.

The order context does not need to know about product entities, since order lines usually only contain simple value-only properties (productId/SKU, a name, quantity and price-per/item). Therefore, Order could provide a function such as

void addOrderLine(ProductId productId, String productName, BigDecimal pricePerItem, int quantity).

It's actually not relevant for the "Order"-BC where these values for productId, productName etc originate from.

In practice, however, it's probably very likely that these values can be obtained from another bounded context, say the "Product"-BC, which is responsible for inventory management etc.

It's quite common to let the UI orchestrate these BCs:

  • The UI (a web shop for customers, for example) loads the products and their prices from the "Product-BC"
  • The user places products into a shopping basket (let's assume for simplicity that this is also the "Order-BC"). For, the UI fires commands such as AddToShoppingBasketCommand(productId, productName, quantity, price) which are handled by the "Order"-BC.
  • When the user wants to place the order for the current shopping basked, it fires a PlaceOrderCommand.
  • The command handler for PlaceOrderCommand takes the current shopping basket and constructs a corresponding Order; all it needs are, for each product, the respective properties of products that were already listed in the shopping basket (and were originally in the AddToShoppingBasketCommand). Note that it does not need to know about the notion of a Product entity from the Product-BC.
like image 160
Alexander Langer Avatar answered Oct 27 '22 09:10

Alexander Langer