diff --git a/plusone-basic/plusone-basic-domain/src/main/java/xyz/zhouxy/plusone/domain/DomainEvent.java b/plusone-basic/plusone-basic-domain/src/main/java/xyz/zhouxy/plusone/domain/DomainEvent.java index 3cbb914..d0d50b7 100644 --- a/plusone-basic/plusone-basic-domain/src/main/java/xyz/zhouxy/plusone/domain/DomainEvent.java +++ b/plusone-basic/plusone-basic-domain/src/main/java/xyz/zhouxy/plusone/domain/DomainEvent.java @@ -17,8 +17,8 @@ import lombok.Getter; */ @Getter public abstract class DomainEvent { - private String identifier; - private long happenedAt; + private final String identifier; + private final long happenedAt; protected DomainEvent() { this.identifier = UUID.randomUUID().toString(); diff --git a/plusone-basic/plusone-basic-domain/src/main/java/xyz/zhouxy/plusone/domain/Entity.java b/plusone-basic/plusone-basic-domain/src/main/java/xyz/zhouxy/plusone/domain/Entity.java index 06daaf6..f12dc07 100644 --- a/plusone-basic/plusone-basic-domain/src/main/java/xyz/zhouxy/plusone/domain/Entity.java +++ b/plusone-basic/plusone-basic-domain/src/main/java/xyz/zhouxy/plusone/domain/Entity.java @@ -5,6 +5,8 @@ import java.util.ArrayList; import java.util.List; import java.util.Optional; +import xyz.zhouxy.plusone.commons.annotation.Overridable; + /** * 实体 * @@ -26,7 +28,29 @@ public abstract class Entity { public abstract Optional getId(); - protected void addDomainEvent(DomainEvent domainEvent) { + protected final void addDomainEvent(DomainEvent domainEvent) { domainEvents.add(domainEvent); } + + public final List 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(); + } } diff --git a/plusone-basic/plusone-basic-domain/src/main/java/xyz/zhouxy/plusone/domain/IDomainEventPublisher.java b/plusone-basic/plusone-basic-domain/src/main/java/xyz/zhouxy/plusone/domain/IDomainEventPublisher.java new file mode 100644 index 0000000..c49f2da --- /dev/null +++ b/plusone-basic/plusone-basic-domain/src/main/java/xyz/zhouxy/plusone/domain/IDomainEventPublisher.java @@ -0,0 +1,8 @@ +package xyz.zhouxy.plusone.domain; + +public interface IDomainEventPublisher { + + void publish(DomainEvent domainEvent); + + void subsrcibe(IDomainEventSubscriber subscriber); +} diff --git a/plusone-basic/plusone-basic-domain/src/main/java/xyz/zhouxy/plusone/domain/IDomainEventSubscriber.java b/plusone-basic/plusone-basic-domain/src/main/java/xyz/zhouxy/plusone/domain/IDomainEventSubscriber.java new file mode 100644 index 0000000..f01f681 --- /dev/null +++ b/plusone-basic/plusone-basic-domain/src/main/java/xyz/zhouxy/plusone/domain/IDomainEventSubscriber.java @@ -0,0 +1,11 @@ +package xyz.zhouxy.plusone.domain; + +/** + * 领域事件消费者 + */ +public interface IDomainEventSubscriber { + + void handleEvent(final T event); + + Class subscribedToEventType(); +}