Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for exposing domain events from aggregate roots as Spring application events [DATACMNS-928] #1381

Closed
spring-projects-issues opened this issue Oct 14, 2016 · 1 comment

Comments

@spring-projects-issues
Copy link

@spring-projects-issues spring-projects-issues commented Oct 14, 2016

Oliver Drotbohm opened DATACMNS-928 and commented

Context

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:

@RequiredArgsConstructor
class MyComponent {

  private final @NonNull MyRepository repository;
  private final @NonNull ApplicationEventPublisher publisher;

  public void doSomething(MyAggregateRoot entity) {

    MyDomainEvent event = entity.someBusinessFunctionality();
    publisher.publishEvent(event);
    repository.save(entity);
  }
}

Problem

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.

Proposed solution

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:

class MyAggregateRoot {

  @PublishedDomainEvents
  Collection<Object> domainEvents() {
    …
  }
}

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


Referenced from: commits a02174f, 0fc9770, 9ada55b, 2c0941b, cb6d2f7, c66cbb9, bdf4738, 6927c29, db17a28, afe95b4, 8062698, f6db714, 50ea2ca, c6f9c40, c52c457, 802eb7d, 4177fb4

Backported to: 1.13 RC1 (Ingalls)

2 votes, 5 watchers

@spring-projects-issues
Copy link
Author

@spring-projects-issues spring-projects-issues commented Oct 27, 2016

John Blum commented

I quite like this Ollie; I think this is a nice/clean design and approach

Loading

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants