4 months ago

Robust domain event implimentation in Laravel - DDD

Posted 4 months ago by booni3

I am looking to set up a basic DDD model using a couple of different techniques on the Domain models. Communication between the domain models is important and I would intend to use domain events for this.

One part I am struggling with currently is the robust implementation of the domain events. For example, we have domains in separate bounded contexts:

  • Order
  • Inventory

The new order process is completed within the order domain and an event NewOrderReceived is emitted. At this point, the inventory domain must pick up this event and initiate a FulfilmentProcess.

Assuming everything runs OK then this is quite simple, but what happens if an exception is thrown either during the event emit or in the event recieve.

There could eventually be multiple other domains picking up events so I think the best course of action here would be to queue and run then asynchronously. However, in this case, there is no feedback to the process emitting the event as to whether the listeners were successful. If they fail first time then what would the retry/error handling mechanism look like? For example, An order was created with product x, but actually that product does not exist/could not be found by the inventory domain.

Similarly, there would be some events that need to go both ways. For example, an OrderCancellationWasRequested event is fired by the order domain. The inventory domain would have to both acknowledge, act and then respond with a success/fail. In this case, we actually want to catch the response like in a REST API, but using domain events.

For reference, here are some useful resources that have helped me with the overall ideas and some implementation so far:

The Many Meanings of Event-Driven Architecture

Domain Events vs. Event Sourcing

Laracasts - Commands & Domain Events

Laracats - Consider Domain Events

Please sign in or create an account to participate in this conversation.