DDD: refer to an entity inside an aggregate root by its identity

前端 未结 3 2144
我寻月下人不归
我寻月下人不归 2021-02-14 23:48

I\'m stuck on finding the proper way to refer to entities located inside an aggregate root, when we only got their identities coming from URL paramete

相关标签:
3条回答
  • 2021-02-15 00:31

    Aggregate Roots are bound to context, in your Context the Order is the AR so it is OK to update it directly since you are exposing it directly, if that code affects other entities they should live in the Order AR.

    If you want a more purist approach you either have to make a findByOrderId in the AR and load it entirely or expose the OrderLine and OrderId in your application (then using your second approach).

    0 讨论(0)
  • 2021-02-15 00:44

    In my opinion there is nothing wrong with this approach:

    Order order = orderRepository.find(orderId);
    order.updateQuantity(orderLineId, 2);
    

    orderLineId is a 'local identity'. It is specific to aggregate root and does not make sense outside of it. You don't have to call it an 'id', it can be 'order line number'. From Eric Evan's book:

    ENTITIES inside the boundary have local identity, unique only within the AGGREGATE.

    ...only AGGREGATE roots can be obtained directly with database queries. All other objects must be found by traversal of associations.

    0 讨论(0)
  • 2021-02-15 00:45

    OrderLineId is what exactly? It has no meaning. You're updating the quantity of a PRODUCT and that's what should be used as the id.

    Order order = orderRepository.find(orderID);
    order.updateQuantity(productID, 2);
    
    0 讨论(0)
提交回复
热议问题