0

One aggregate has to reference other aggregate by ids, for instance: order stores userId. So if I need the user entity to do something in the order aggregate I should pass it like this: order.doSomthing(user). But where should I retrieve the user in the application service or domain service?

Agustin Castro
  • 439
  • 1
  • 6
  • 20
  • This is way too context-dependent to throw a general answer at. The mention of a domain service and manipulating an aggregate from another aggregate also suggest design issues. You should be more specific about what the `Order` is trying to do here. – guillaume31 May 30 '18 at 11:53

1 Answers1

0

You don't.

An Aggregate operates/depends only on the data that it owns. This applies to write but also to read.

If an Aggregate, for example User needs some data from another Aggregate, for example Order, then the Application service (or most probable a Saga/Process manager) gets the data from the Order and it passes it to the User:

user.doSomething(order.some, order.info)
Constantin Galbenu
  • 16,951
  • 3
  • 38
  • 54
  • so you mean that in the user application service I will have the orderRepository to get order? for example: UserApplicationService { method() { orderId = user.orderId(); order = orderRepository->findById(orderId); user.doSomething(order) } } – Agustin Castro May 21 '18 at 18:16
  • @AgustinCastro no. The User does not own the Order. You will have something like: `user.doSomething(order.some, order.info)` – Constantin Galbenu May 21 '18 at 18:17
  • I see, but how can retrieve the order? and in the follow link: https://stackoverflow.com/questions/4919687/aggregate-root-references-other-aggregate-roots/4922100 there is a guy (David Masters) that says: "Then, if there is domain logic that involves both aggregates this can be extracted to a domain service and look something like this:" he retrieve both aggregates from different repositories and do agg1.DoSomething(agg2); – Agustin Castro May 21 '18 at 18:25
  • @AgustinCastro you simply load both the Aggregates from the Repository. Regarding that link: I disagree; that would create a non-necesary coupling between the 2 Aggregates. – Constantin Galbenu May 21 '18 at 18:28
  • Therefore I should do something like that: UserApplicationService { method() { orderId = user.orderId(); order = orderRepository->findById(orderId); user.doSomething(order.some, order.info) } } right? – Agustin Castro May 21 '18 at 18:33
  • @AgustinCastro yes. – Constantin Galbenu May 21 '18 at 18:34