关于java:聊聊eventsourcingcqrs的model

7次阅读

共计 2598 个字符,预计需要花费 7 分钟才能阅读完成。

本文次要钻研一下 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

  • event-sourcing-cqrs-examples
正文完
 0