当前位置:主页 > 查看内容

聊聊event-sourcing-cqrs的model

发布时间:2021-04-16 00:00| 位朋友查看

简介:序 本文主要研究一下event-sourcing-cqrs的model Event public abstract class Event { private final UUID aggregateId; private final ZonedDateTime timestamp; private final int version; protected Event(UUID aggregateId, ZonedDateTime timestamp,……

本文主要研究一下event-sourcing-cqrs的model

Event

public abstract class Event {

  private final UUID aggregateId;
  private final ZonedDateTime timestamp;
  private final int version;

  protected Event(UUID aggregateId, ZonedDateTime timestamp, int version) {
    this.aggregateId = checkNotNull(aggregateId);
    this.timestamp = checkNotNull(timestamp);
    this.version = version;
  }

  public UUID getAggregateId() {
    return aggregateId;
  }

  public ZonedDateTime getTimestamp() {
    return this.timestamp;
  }

  public int getVersion() {
    return version;
  }
}
Event定义了aggregateId、timestamp、version属性

EventStore

public interface EventStore {

  void store(UUID aggregateId, List<Event> newEvents, int baseVersion)
      throws OptimisticLockingException;

  List<Event> load(UUID aggregateId);

}
EventStore接口定义了store、load方法

Aggregate

public abstract class Aggregate {

  private UUID id;
  private int baseVersion;
  private List<Event> newEvents;

  protected Aggregate(UUID id) {
    this(id, emptyList());
  }

  protected Aggregate(UUID id, List<Event> eventStream) {
    checkNotNull(id);
    checkNotNull(eventStream);
    this.id = id;
    eventStream.forEach(e -> {
      apply(e);
      this.baseVersion = e.getVersion();
    });
    this.newEvents = new ArrayList<>();
  }

  protected void applyNewEvent(Event event) {
    checkArgument(event.getVersion() == getNextVersion(),
        "New event version '%s' does not match expected next version '%s'",
        event.getVersion(), getNextVersion());
    apply(event);
    newEvents.add(event);
  }

  private void apply(Event event) {
    try {
      Method method = this.getClass().getDeclaredMethod("apply", event.getClass());
      method.setAccessible(true);
      method.invoke(this, event);
    } catch (InvocationTargetException e) {
      Throwables.propagate(e.getCause());
    } catch (NoSuchMethodException | IllegalAccessException e) {
      throw new UnsupportedOperationException(
          format("Aggregate '%s' doesn't apply event type '%s'", this.getClass(), event.getClass()),
          e);
    }
  }

  public UUID getId() {
    return id;
  }

  public int getBaseVersion() {
    return baseVersion;
  }

  public List<Event> getNewEvents() {
    return ImmutableList.copyOf(newEvents);
  }

  protected int getNextVersion() {
    return baseVersion + newEvents.size() + 1;
  }
}
Aggregate定义了id、baseVersion、newEvents属性;其applyNewEvent方法会执行apply(event)及newEvents.add(event);apply方法通过反射执行event的apply方法

ValueObject

public abstract class ValueObject {

  @Override
  public boolean equals(Object o) {
    return EqualsBuilder.reflectionEquals(this, o);
  }

  @Override
  public int hashCode() {
    return HashCodeBuilder.reflectionHashCode(this);
  }

  @Override
  public String toString() {
    return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE);
  }
}
ValueObject覆盖了equals、hashCode、toString方法

Specification

public interface Specification<T> {

  boolean isSatisfiedBy(T value);

}
Specification接口定义了isSatisfiedBy方法

小结

event-sourcing-cqrs-examples的model定义了Event、Aggregate、ValueObject抽象类以及EventStore、Specification接口。

doc


本文转自网络,版权归原作者所有,原文链接:https://segmentfault.com/a/1190000039832742
本站部分内容转载于网络,版权归原作者所有,转载之目的在于传播更多优秀技术内容,如有侵权请联系QQ/微信:153890879删除,谢谢!
上一篇:分布式锁注意点及其实现 下一篇:没有了

推荐图文


随机推荐