I am working with a project following DDD
and has consumers
to a Kafka Queue
. My question is straight forward, where do the consumers reside in the Onion Architecture
and Hexagonal Architecture
? Are they event handlers or should they be a part of the infrastructure?
I am using
Kafka Consumers
to listen to change events of other aggregate roots and want to store the data in my currentaggregate
. Basically replicating the data from one microservice to another.
The way I see it is:
Your aggregates are the Core
The message handlers are Use Cases, which use dependencies (like repository interfaces) and the core to execute the business use case.
There is infrastructure code that peeks messages from the queue and triggers use cases
With this approach, you can unit test your use cases without worrying about infrastructure and you can replace the whole messaging queue technology. The same approach works for handling API requests. In practice, the only difference is that with the API you can return a response synchronously, and with messaging you can't.
As a practical note, in .NET I use a library called Mediatr to implement and trigger the use cases. In java, I found PipelinR which looks similar at first sight. This type of approach allows you to implement all use cases the same way for all your synchronous and asynchronous usages.