6

What is the difference between using domain services and domain events? The thing I can see is that domain events allow you to further decouple your domain model and get away from using domain services. It seems the simple implementation would be to use a service and then eventually refactor to using domain events.

Additionally, it seems odd that when using Domain Events I have to wire them up in the Application Code (IOC container or with Poor Man’s DI). For example let’s say I have an Order Aggregate that calls OrderCreated which triggers an update to they Inventory System. From everything I’ve seen, I have to wire this event and event handler in application code. Again, domain services seem simpler and more encapsulated. Domain events do not seem adequate for domain-to-domain events but rather domain-to-application events.

Am I understanding this correctly or is there a much higher-level concept I'm missing?

0

1 Answer 1

3

I think you are looking at this with the wrong perspective. The two concepts are generally NOT mutually exclusive, they are typically two entirely different areas of your system. For instance, domain services are intended to contain your applications business logic and core functionality. While utilizing domain events is intended to function as the "service to service communication layer".

When you are using event based architectures, it is primarily intended to de-couple the producer from the consumer. In addition to this benefit you also gain the ability to publish a single event from some producer service, which may have any number of consumer services listening for the event. This allows you to substantially reduce the amount of messaging between related services, while also eliminating the need for methods such as server-side long pulling etc.

When you are designing an application, utilizing the event based architecture, you generally don't have much concern for what the exact implementation is of the event consumers. You just ensure that you have a well-defined event interface, which can then be consumed by a service written in any language, with any implementation type (could be a serverless function, megalithic application or microservice... it doesn't really matter!)

Hopefully this clears it up a bit for you :) If you still feel like you have a gap in your understanding, I would love to clarify any specific aspect of my answer!

3
  • 1
    I believe this is not correct. Domain Events are supposed to work within Bounded context. Application events do the service to service communication. So coming back, what is the difference? Commented May 15, 2021 at 18:24
  • @CodeNameJack what I was attempting to get across was that they are essentialy two parts of the same thing. You will have a domain service, which can communicate with other domain services via domain events. Commented May 16, 2021 at 21:49
  • @CodeNameJack true but most of the times, what you call "application events" (sometimes also called "integration" or "interdomain" events) are just domain events that pass through a platform bus. I'd see this as a technical concern Commented May 18, 2021 at 7:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.