领域事件相关建模。

dev
ZhouXY108 2023-06-29 01:28:14 +08:00
parent 87942d708a
commit 30f67dfa18
4 changed files with 46 additions and 3 deletions

View File

@ -17,8 +17,8 @@ import lombok.Getter;
*/ */
@Getter @Getter
public abstract class DomainEvent { public abstract class DomainEvent {
private String identifier; private final String identifier;
private long happenedAt; private final long happenedAt;
protected DomainEvent() { protected DomainEvent() {
this.identifier = UUID.randomUUID().toString(); this.identifier = UUID.randomUUID().toString();

View File

@ -5,6 +5,8 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
import xyz.zhouxy.plusone.commons.annotation.Overridable;
/** /**
* *
* *
@ -26,7 +28,29 @@ public abstract class Entity<ID extends Serializable> {
public abstract Optional<ID> getId(); public abstract Optional<ID> getId();
protected void addDomainEvent(DomainEvent domainEvent) { protected final void addDomainEvent(DomainEvent domainEvent) {
domainEvents.add(domainEvent); domainEvents.add(domainEvent);
} }
public final List<DomainEvent> getDomainEvents() {
return List.copyOf(domainEvents);
}
@Override
@Overridable
public boolean equals(Object obj) {
return super.equals(obj);
}
@Override
@Overridable
public int hashCode() {
return super.hashCode();
}
@Override
@Overridable
public String toString() {
return super.toString();
}
} }

View File

@ -0,0 +1,8 @@
package xyz.zhouxy.plusone.domain;
public interface IDomainEventPublisher {
void publish(DomainEvent domainEvent);
<T extends DomainEvent> void subsrcibe(IDomainEventSubscriber<T> subscriber);
}

View File

@ -0,0 +1,11 @@
package xyz.zhouxy.plusone.domain;
/**
*
*/
public interface IDomainEventSubscriber<T extends DomainEvent> {
void handleEvent(final T event);
Class<T> subscribedToEventType();
}