One important element of Domain-Driven Design is exposing events important to the domain to other components. These events are produced by aggregates and usually are tied to state transitions of the aggregate. Right now, a typical pattern to produce those events is this:
As you can see, the client needs to use ApplicationEventPublisher explicitly. If we provided a way for the aggregate root to expose the events to be published, the repository could take over the burden to publish the events.
First of all we need means to let the aggregate root expose the events to be published. This could be an annotation based approach like this:
The annotation name to be open for discussion, of course. We could support to return a Collection of events or a single one. The same could be achieved with an interface, of course.
The repository infrastructure could then inspect the aggregate root type for a method declaration with said annotation (or a type check for the interface pendant) and add an interceptor to the repository proxy that holds a reference to the ApplicationEventPublisher, grabs the events to publish from the aggregate and publishes them before forwarding the call