Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 360
drizzie Avatar asked Apr 10 '16 21:04

drizzie


2 Answers

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).

like image 156
MikeSW Avatar answered Sep 18 '22 15:09

MikeSW


This question is analogous to "how does your domain communicate with an external system (other than for data persistence)"?

A service bus is no different from a physical sensor or other external piece of hardware. One typically represents those physical things in code as an object that abstracts the physical concept. They are outside the problem domain (PD) and can be considered System Integration (SI) objects.

If your domain needs to communicate with an external system, it simply makes a call on its equivalent SI object. Equally, an SI object can call on your domain.

Note: this answer assumes your domain is not anemic.

like image 38
aryeh Avatar answered Sep 20 '22 15:09

aryeh