這篇文章主要介紹“event-sourcing-cqrs的model有哪些”,在日常操作中,相信很多人在event-sourcing-cqrs的model有哪些問(wèn)題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”event-sourcing-cqrs的model有哪些”的疑惑有所幫助!接下來(lái),請(qǐng)跟著小編一起來(lái)學(xué)習(xí)吧!
創(chuàng)新互聯(lián)建站從2013年開(kāi)始,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目成都網(wǎng)站建設(shè)、網(wǎng)站制作網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個(gè)夢(mèng)想脫穎而出為使命,1280元石拐做網(wǎng)站,已為上家服務(wù),為石拐各地企業(yè)和個(gè)人服務(wù),聯(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, ListnewEvents, int baseVersion) throws OptimisticLockingException; List load(UUID aggregateId); }
EventStore接口定義了store、load方法
public abstract class Aggregate { private UUID id; private int baseVersion; private ListnewEvents; protected Aggregate(UUID id) { this(id, emptyList()); } protected Aggregate(UUID id, List 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 getNewEvents() { return ImmutableList.copyOf(newEvents); } protected int getNextVersion() { return baseVersion + newEvents.size() + 1; } }
Aggregate定義了id、baseVersion、newEvents屬性;其applyNewEvent方法會(huì)執(zhí)行apply(event)及newEvents.add(event);apply方法通過(guò)反射執(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{ boolean isSatisfiedBy(T value); }
Specification接口定義了isSatisfiedBy方法
到此,關(guān)于“event-sourcing-cqrs的model有哪些”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)?lái)更多實(shí)用的文章!