3

I have a simple scenario regarding CQRS and DDD in mind and I can't figure out the right way to implement it:

Order and Buyer are two aggregate roots inside Ordering service. When a user checks out the basket:

  1. An integration event is raised in (basket) service.

  2. It's handler inside Ordering service gets called.

  3. Inside this handler, a CreateOrderCommand is created and dispatched.

  4. The command handler instantiates an "Order".

  5. As the result, a domain event is raised "OrderStartedDomainEvent".

  6. Inside it's handler, the side effects regarding other aggregates (e.g. Buyer) must be applied: A Buyer is instantiated (if not exists already).

So, after this scenario, I want to change the order state. Changing order state requires a command. Where am I supposed to dispatch this command? From what I read so far, it's not appropriate to create and dispatch the command inside domain event handler.

Also, if I need to raise a domain event here, how can I do that? Since the Buyer constructor may not get called (if it already exists). So is it correct to raise a domain event inside a domain event handler?

I searched a lot but the previous answers were so complicated for me . I'd appreciate if someone clarifies. Thanks in advance.

Gru97
  • 471
  • 5
  • 8
  • What you need is a saga. Here is 2 good articles: 1) https://lostechies.com/jimmybogard/2013/05/14/saga-patterns-wrap-up/, 2) https://blog.jonathanoliver.com/cqrs-sagas-with-event-sourcing-part-i-of-ii/ – Maxime Gélinas Dec 23 '19 at 18:19
  • @MaximeGélinas Thanks for the good references you mentioned regarding saga. But in my case, saga won't help since these command are all in one bounded context/microservice and should be handled in as single process. So I believe there's no need for saga. – Gru97 Jan 27 '20 at 17:26
  • 1
    You're right. A saga is overkill if you're in a single process. Then a process manager might be the solution (i.e. a stateless saga). A process manager task is to listen for an event and send a command. (It's a fancy name for an event handler which send a command.) – Maxime Gélinas Jan 28 '20 at 16:58

1 Answers1

1

From what I read so far, it's not appropriate to create and dispatch the command inside domain event handler.

You would probably dispatch the messages from the application event handler. The domain model is pure in memory bookkeeping; the application layer/component is responsible for moving information from one place to another.

A way of thinking about this sort of design is that the application handler is orchestrating messages between a number of components, one of which happens to be the domain model.

VoiceOfUnreason
  • 52,766
  • 5
  • 49
  • 91
  • Since application layer in my code is actually the command handlers, I don't think dispatching a command inside another command handler would be right? – Gru97 Dec 15 '19 at 19:52
  • "the application handler is orchestrating messages between a number of components". This is the definition of a saga. – Maxime Gélinas Dec 23 '19 at 18:21
  • Saga means something very different -- see Garcia-Molina and Salem 1987. "Process Manager" - as described by Hohpe and Woolf in _Enterprise Integration Patterns_ is a closer match. – VoiceOfUnreason Dec 23 '19 at 19:20