Hi,
I hope someone can help me with this problem. I’ve seen similar questions asked on the DDD/CQRS group, but the answers were rather obscure or zen-like.
My domain model seems to want 2 or more aggregate-roots (AR) to share the same entity instance (E)… or perhaps one instance each but representing the same real-world entity:
AR1 <-> E1
AR2 <-> E1
The association is also bidirectional: usually an AR asks E1 to do something, but sometimes a change to E1 will mean that AR1 and AR2 have to react in some way. I’m having trouble figuring out how to make this work in Axon.
(1) One possibility: move some logic out of AR and E into a separate command-handler or service, which then has to do lots of boilerplate code for looking things up in repositories. This feels like the entities(/ARs) becoming more anaemic.
(2) Another possibility: make E an aggregate-root in its own right. AR1 and AR2 just have the ID of E1, and E1 has a list of IDs {AR1, AR2}. But now AR and E need repositories injected into them, and they need to do lookups to get real instances to talk to… but people say they’re not supposed to do that because they would be reaching across into a different ‘bounded context’… or would they?
(3) Another idea: let AR1 and AR2 each have a separate copy of E1, and when AR1’s copy of E1 fires events, somehow arrange for AR2’s copy of E1 to apply the same events to keep the state in sync… but how to make that happen in Axon? Via some separate event-handler which then looks things up in repositories and issues commands to those things?
(4) Alternatively: I’ve seen some discussion that hints at ARs dispatching commands to other ARs as part of their own command-handling. Is that ok? It seems like a round-about way of just calling a method, but it does take care of finding the right object to call the method one (a lookup instead of an injection). This seems like an improvement on (2).
I’m aware of the rule that says you must not fire events from inside an event handler (although it seems like a reasonable thing to want to do). Does a similar restriction apply to dispatching commands from inside a command handler?
I guess my problem is that I’m used to doing entity/association modelling in the ORM/JPA world. I tend to think in terms of ‘real objects’ that “just” have references to other ‘real objects’ (ok, they get them injected really) and then simply call their methods to collaborate directly with them.
I haven’t seen a useful collection of patterns that show how to do similar things in the DDD/CQRS/ES world. People tend to say “just keep modelling your domain, you haven’t done it right yet” without really explaining how to do that in a suitable way… or what to do next if your modelling keeps coming up with the same awkward structures…
Anyway, all advice and suggestions gratefully received! I think I am going round in circles
Many thanks,
Richard