In this post we’re going to provide a short introduction to what are Application Events and Domain Events, and what are their similarities and differences. Spoiler: except fot the name, they are really different.
All developers probably meet the Observer pattern in some of its variations many times during their career, and possibly very early. In fact, most widely known open source systems offer an “hook” or “plugin” system which basically works like this:
Using events makes the system flexible, as it is usually really easy to write a tiny, separated piece of code to add or even modify the existing behaviour.
Application Events are, well, related to the application. They usually describes actions that happens within the code: a new record created, some error happened, a query is about to be performed.
If you want to slowly move toward Domain-Driven Design, you have to also understand something about Domain Events. They are a representation of something that happened in the business:
There may be some confusion between the two event types, but there are many key differences. A Domain Event:
In its most basic form, using Domain Events is a better, semantically richer and more useful way to track history than writing things into a log. But you can also move full Event Sourcing where persisted Domain Events become the source of truth for your application, which make much sense for certain types of applications.
Shortly: use both.
Application Events are a great way to reduce the complexity and increase the flexibility of your code.
Domain Events are a great way to describe and persist things that happen in the business.