本文主要研究一下event-sourcing-cqrs的model
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属性
public interface EventStore {
void store(UUID aggregateId, List<Event> newEvents, int baseVersion)
throws OptimisticLockingException;
List<Event> load(UUID aggregateId);
}
EventStore接口定义了store、load方法
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方法
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方法
public interface Specification<T> {
boolean isSatisfiedBy(T value);
}
Specification接口定义了isSatisfiedBy方法
event-sourcing-cqrs-examples的model定义了Event、Aggregate、ValueObject抽象类以及EventStore、Specification接口。
输入字段可以制作让用户可输入信息的 WML 卡片,正如这个例子演示的. 输入字段 ...
XSLT是XSL的一部份,它是XML的一种应用,指定将一篇XML文档转换成另一种XML文档...
最近在做一评论功能,需要能够评论表情,那 contentEditable 这个属性就首当其冲...
命令简介 ldd 命令打印程序和库的共享库依赖项。注意:ldd 不是一个可执行程序,...
before/after伪类相当于在元素内部插入两个额外的标签,其最适合也是最推荐的应...
五月份最重量级的交互设计文!今年百度的设计师们从体验角度出发,结合大量现有报...
背景 目前Mac自带的终端不是很好用,好多常规的Linux命令都不支持 比如:ll命令...
WML 页面通常称为 \"deck\"。每个 deck 含有一系列的 card。card 元素可包含文本...
媒体设备类型使用详解: !DOCTYPE htmlhtml lang=enhead meta charset=UTF-8 tit...
HTML5单选框、复选框、下拉菜单、文本域 单选框的实例代码,代码如下所示: 1单...