溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

event-sourcing-cqrs的model有哪些

發(fā)布時間:2021-10-13 14:13:44 來源:億速云 閱讀:145 作者:iii 欄目:編程語言

這篇文章主要介紹“event-sourcing-cqrs的model有哪些”,在日常操作中,相信很多人在event-sourcing-cqrs的model有哪些問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”event-sourcing-cqrs的model有哪些”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!

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方法會執(zhí)行apply(event)及newEvents.add(event);apply方法通過反射執(zhí)行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方法

到此,關(guān)于“event-sourcing-cqrs的model有哪些”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識,請繼續(xù)關(guān)注億速云網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI