Domain Events: Efficient System Adaptation
Explore how to implement domain events to manage system changes and side effects, separating event handling from triggering.
We'll cover the following...
A domain event is a domain-driven design pattern that encapsulates a change in the system that is important to the domain experts. When important events happen in our system, they are often accompanied by rules or side effects. We may have a rule that when the OrderCreated
event happens in our system, we send a notification to the customer.
If we put this rule into the handler for CreateOrder
so that the notification happens implicitly, it might look something like this:
// orderCreationif err = h.orders.Save(ctx, order); err != nil {return errors.Wrap(err, "order creation")}// notifyCustomerif err = h.notifications.NotifyOrderCreated(ctx, order.ID, order.CustomerID,); err != nil {return errors.Wrap(err, "customer notification")}
If it were to remain as just one rule, we may be fine doing it this way. However, real-world applications rarely stay simple or have simple rules. Later, in the life of the application, we want to add a Rewards module to our application, we add the code for the rule to the same handler, and later, still we want more side effects to occur. What we had before, CreateOrder
, should now be renamed CreateOrderAndNotifyAndReward...;
otherwise, it won’t properly reflect its responsibility. Also, consider there will be other rules and handlers that might be implemented, so finding the implementations for a rule can become a problem. ...