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

I am a little confused about the origin of entities in an environment that takes advantage of CQRS and Event Sourcing. To make my question clear, take the following famous web store example:

You can model the order as a cumulative root. Order takes lines of order that are themselves determined by product and quantity.

Since the order line is an entity that is built during the order process itself, there is still a concept of a product that appears to be an entity. But where did the product or even the product catalog come from? In my opinion, in the limited context of the order, there is no such thing as the aggregate root of the product. How then does the order context learn about the nature of the product? Are they supported in another limited context and somehow materialized in the read context store of the order?

+5
source share
1 answer

In the BC that the Order contains, it may indeed be that Product , as part of the OrderLine , is a value object consisting of values โ€‹โ€‹such as ProductId , Name , etc.

The order content does not need to be aware of the nature of the product, since order lines usually only contain simple properties for values โ€‹โ€‹only (productId / SKU, name, quantity and price per item). Therefore, Order can provide a function such as

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

This does not actually apply to Order -BC, where these values โ€‹โ€‹are for ProductId , productName , etc. come from.

However, in practice, it is probably very likely that these values โ€‹โ€‹can be obtained from another limited context, for example, Product -BC, which is responsible for inventory management, etc.

It is generally accepted that the UI organizes these BCs:

  • A user interface (such as a web store for customers) downloads products and their prices from "product-BC"
  • The user places the products in the shopping cart (for simplicity, this is also an โ€œOrder-BC"). For the UI runs commands such as AddToShoppingBasketCommand(productId, productName, quantity, price) , which are processed by "Order" -BC.
  • When a user wants to place an order for the current purchase, he launches PlaceOrderCommand .
  • The command handler for PlaceOrderCommand accepts the current shopping cart and creates the corresponding Order ; all that he needs for each product is the corresponding properties of the products that were already indicated in the shopping cart (and were originally in AddToShoppingBasketCommand ). Note that he does not need to know about the Product object from Product-BC.
+3
source

Source: https://habr.com/ru/post/1203043/


All Articles