Skip to content

Commit 07ccef4

Browse files
Domain events
1 parent 0126411 commit 07ccef4

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed

HibernateSpringBootDomainEvents/README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
**[How To Publish Domain Events From Aggregate Root](https://github.com/AnghelLeonard/Hibernate-SpringBoot/tree/master/HibernateSpringBootDomainEvents)**
22

3+
**Note:** Domain events should be used with extra-caution! The best practices for using them are revealed in my book, [Spring Boot Persistence Best Practices](https://www.apress.com/us/book/9781484256251).
4+
35
**Description:** Starting with Spring Data Ingalls release publishing domain events by aggregate roots becomes easier. Entities managed by repositories are aggregate roots. In a Domain-Driven Design application, these aggregate roots usually publish domain events. Spring Data provides an annotation `@DomainEvents` you can use on a method of your aggregate root to make that publication as easy as possible. A method annotated with `@DomainEvents` is automatically invoked by Spring Data whenever an entity is saved using the right repository. Moreover, Spring Data provides the `@AfterDomainEventsPublication` annotation to indicate the method that should be automatically called for clearing events after publication. Spring Data Commons comes with a convenient template base class (`AbstractAggregateRoot`) to help to register domain events and is using the publication mechanism implied by `@DomainEvents` and `@AfterDomainEventsPublication`. The events are registered by calling the `AbstractAggregateRoot.registerEvent()` method. The registered domain events are published if we call one of the *save* methods (e.g., `save()`) of the Spring Data repository and cleared after publication.
46

5-
This is a sample application that relies on `AbstractAggregateRoot` and its `registerEvent()` method. We have two entities, `Book` and `BookReview` involved in a lazy-bidirectional association. A new book review is saved in `CHECK` status and a `CheckReviewEvent` is published. This event handler is responsible to check the review grammar, content, etc and switch the review status from `CHECK` to `ACCEPT` or `REJECT` and send a corresponding e-mail to the reviewer. So, this event is registered before saving the book review in `CHECK` status and is published automatically after we call the `BookReviewRepository.save()` method. After publication, the event is cleared.
7+
This is a sample application that relies on `AbstractAggregateRoot` and its `registerEvent()` method. We have two entities, `Book` and `BookReview` involved in a lazy-bidirectional `@OneToMany` association. A new book review is saved in `CHECK` status and a `CheckReviewEvent` is published. This event handler is responsible to check the review grammar, content, etc and switch the review status from `CHECK` to `ACCEPT` or `REJECT` and propagate the new status to the database. So, this event is registered before saving the book review in `CHECK` status and is published automatically after we call the `BookReviewRepository.save()` method. After publication, the event is cleared.
68

79
**Key points:**
810
- the entity (aggregate root) that publish events should extend `AbstractAggregateRoot` and provide a method for registering events

0 commit comments

Comments
 (0)