DATACMNS-1143 - Improve design of AbstractAggregateRoot.

Methods for domain event registration and access are now not following bean semantics anymore to avoid those accidentally leaking into formats that use the bean spec (e.g. Jackson to produce JSON).
This commit is contained in:
Oliver Gierke
2017-08-23 15:28:59 +02:00
parent fc402184e2
commit ce7a2e8ea5

View File

@@ -15,18 +15,19 @@
*/
package org.springframework.data.domain;
import lombok.Getter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import org.springframework.data.annotation.Transient;
import org.springframework.util.Assert;
/**
* Convenience base class for aggregate roots that exposes a {@link #registerEvent(Object)} to capture domain events and
* expose them via {@link #getDomainEvents()}. The implementation is using the general event publication mechanism
* implied by {@link DomainEvents} and {@link AfterDomainEventPublication}. If in doubt or need to customize anything
* here, rather build your own base class and use the annotations directly.
* expose them via {@link #domainEvents())}. The implementation is using the general event publication mechanism implied
* by {@link DomainEvents} and {@link AfterDomainEventPublication}. If in doubt or need to customize anything here,
* rather build your own base class and use the annotations directly.
*
* @author Oliver Gierke
* @author Christoph Strobl
@@ -34,14 +35,10 @@ import org.springframework.util.Assert;
*/
public class AbstractAggregateRoot {
/**
* All domain events currently captured by the aggregate.
*/
@Getter(onMethod = @__(@DomainEvents)) //
private transient final List<Object> domainEvents = new ArrayList<>();
private transient final @Transient List<Object> domainEvents = new ArrayList<>();
/**
* Registers the given event object for publication on a call to a Spring Data repository's save method.
* Registers the given event object for publication on a call to a Spring Data repository's save methods.
*
* @param event must not be {@literal null}.
* @return
@@ -59,7 +56,15 @@ public class AbstractAggregateRoot {
* repositories.
*/
@AfterDomainEventPublication
public void clearDomainEvents() {
protected void clearDomainEvents() {
this.domainEvents.clear();
}
/**
* All domain events currently captured by the aggregate.
*/
@DomainEvents
protected Collection<Object> domainEvents() {
return Collections.unmodifiableList(domainEvents);
}
}