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

創(chuàng)新互聯(lián)建站從2013年開始,是專業(yè)互聯(lián)網(wǎng)技術服務公司,擁有項目成都網(wǎng)站建設、網(wǎng)站制作網(wǎng)站策劃,項目實施與項目整合能力。我們以讓每一個夢想脫穎而出為使命,1280元石拐做網(wǎng)站,已為上家服務,為石拐各地企業(yè)和個人服務,聯(lián)系電話:028-86922220
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方法會執(zhí)行apply(event)及newEvents.add(event);apply方法通過反射執(zhí)行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的model有哪些”的學習就結(jié)束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續(xù)學習更多相關知識,請繼續(xù)關注創(chuàng)新互聯(lián)網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>
網(wǎng)頁標題:event-sourcing-cqrs的model有哪些
網(wǎng)站地址:http://www.chinadenli.net/article16/gisodg.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站收錄、電子商務、網(wǎng)站制作、小程序開發(fā)、外貿(mào)建站、
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)