When implementing Domain events should the event handlers be only used for purely domain concerns; something that you would discuss with the business experts, or are they open to be used by anything that has an interest in the domain model?
This is most probably best explained with a simple example, consider a Calendar application for scheduling work to employees.
We might have the following domain events...
AppointmentAdded AppointmentRemoved AppointmentContentChanged AppointmentMoved
We have handlers for these events, for example when an Appointment is moved to a time outside of the employees working hours we set a warning flag.
There are of course application concerns that are interested in these events, e.g. when an Appointment is added to the calendar, we should add it to the Unit of Work so we can commit the changes later.
Should these application concerns be consumers of the domain events, or should we raise and handle separate system events instead?
Use domain events to explicitly implement side effects of changes within your domain. In other words, and using DDD terminology, use domain events to explicitly implement side effects across multiple aggregates.
In my view, we should place domain event handlers directly in the domain layer itself: They could be domain services which receive both a domain event and an aggregate that might be affected by it, and transform the domain event into one or many method calls.
In C#, a domain event is simply a data-holding structure or class, like a DTO, with all the information related to what just happened in the domain, as shown in the following example: C# Copy.
Each key node has a handler that receives key events when the key has focus. The handler responds to the key-pressed and key-released events for the Enter key by changing the color of the key on the screen. The event is then consumed so that the keyboard node, which is the parent node, does not receive the event.
There are 2 well established ways of using events in a DDD solution.
The first one is based on Udi Dahan's articles about events. If you haven't read them already, I highly recommend that. In summary it says that you publish your events using static class in addition to normal ORM-style behavior. So you add an order to customer's order collection and you publish the event. Because your domain behavior is executed inside a transaction scope, so are event handlers. You could also find there and advice not to manually attach objects to a Unit of Work. New aggregate roots should be created by invoking behavior on existing ones.
There is another option which is promoted by Greg Young. It is based on event sourcing which basically is using events as means of persisting state. In this approach your aggregate roots usually use some infrastructure (e.g. base aggregate root class) to apply events. Apply does invoke an event handler on aggregate root class and publishes this event on a bus (whatever bus implementation you use).
If you mean cross-cutting concerns than you will be forced to use it anyway if your application logic requires it. So it will be mixed with other event-processing code.
But if you need to do several independent things when your domain event happened than you better to use separate event handlers (see Separation Of Concerns principle).
In the first case, by the way, try to avoid mixing domain logic with event-processing (infrastructure) logic. Left infrastructure/cross-cutting concerns code in event handlers calling domain methods. Move domain code inside domain objects' methods.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With