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?
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:
AddToShoppingBasketCommand(productId, productName, quantity, price)
which are handled by the "Order"-BC.PlaceOrderCommand
.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.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With