Search code examples
domain-driven-designdomain-events

Where does the message bus service live in Domain Driven Design


I am trying to further my understanding of DDD. More specifically, how to handle domain events via a message bus for asynchronous processing.

Lets say I have some architecture ->

 _____________________
|                     |
|        Client       |
|_____________________|
           |
 __________|__________
|                     |
| Application Service |
|_____________________|
           |
 __________|__________
|                     |
|        Domain       |
|_____________________|

When my domain raises some domain event, how do I get that event to a messaging service such as RabbitMQ?

My first thought is to inject a message bus service, IMessageBus, that knows how to send the events to RabbitMQ. The service would be used by domain event handlers to dispatch the event to the bus.

But then I thought, now my domain has to know how to handle its own events.

Can someone shed some light on the matter?


Solution

  • The Service Bus is part of the infrastructure, however the application services know about it (as an abstraction). It's ok to inject the bus into the app service, because the app service doesn't contain domain logic but acts as the coordinator and host of a business use case.

    But then I thought, now my domain has to know how to handle its own events.

    Yeah, but the bus only deliver the messages, the message handlers are basically application services.

    Rabbit and others are an implementation details, your app handlers should be invoked by the service bus (which should abstract the actual messaging process).