Skip to main content

Application Events and Domain Events

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.

Application events

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:

  • the system raises an Event at specific points in the application, e.g. immediately before and immediately after creating a new user
  • other parts of the system listen to this Event and react accordingly, e.g. send a notification email after a new user is created
  • you can write your own handler that listens to the Event and performs additional operations

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.

Domain Events

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:

  • a new Customer registration has been requested
  • a new Order has been sent
  • some money has been paid

There may be some confusion between the two event types, but there are many key differences. A Domain Event:

  • has usually a rich structure since there may be many data associated with it
  • describes business-related events rather then technical-related events
  • has already happened in reality and cannot be modified
  • is not usually raised to implement the Observer pattern, but persisted and/or passed as a DTO in a Command chain

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.

Usage suggestion

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.