An event is something that has happened in the past. A domain event is, something that happened in the domain that you want other parts of the same domain (in-process) to be aware of. The notified parts usually react somehow to the events.

Domain events: design and implementation

Benefits

Since domain events are… well, events, it comes with all the benefits and use cases that you would have if you implement some sort of event driven architecture:

  • Reduce coupling between the effect and the consequence: creating a user and notifying that a user was created have different levels of complexity, different responsibilities, different reasons to change…
  • You won’t have a long Signup method with all the stuff that needs to be done after a user has been created.
  • Your unit tests will be easier to implement, because dependencies have been simplified per each case.
  • Open-Closed principle is your friend: if you have to do something additional after a user is created, you just need to implement a new handler.
  • True asynchronous operation: if after user is created you have to count Mississippis, don’t worry, there’s no hurry because the rest of the handlers will be executed.
  • There’s a nice way to inspect all the consequences (handlers) that an effect (the event) has.

Drawbacks

Regarding the drawbacks, the biggest issue is that, if not done properly, following the execution of a process can become difficult. Since we decoupled the login from the rest, now debugging the whole login or trying to understand why something didn’t work requires more time.

Of course, if you split the process in the right way and you set up tracing mechanisms, then this shouldn’t be such a problem. But this is something to take into account when introducing events in a system.

Paying homage

Some years ago, we had to implement this in a project, and we were thankful to use the following resources:

I’ve just done the compilation of these resources, I’ve had to use my memory and I’ve searched the web. I will have probably forgotten some of the posts that we used, but at least, these other posts won’t be forgotten. Setting up with Autofac wasn’t certainly easy and we search for every stackoverflow entry like this one.