Domain event handlers - Should they be used for Application layer concerns?
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 开发者_Python百科changes later.
Should these application concerns be consumers of the domain events, or should we raise and handle separate system events instead?
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.
精彩评论