event-sourcing-cqrs的model有哪些
这篇文章主要介绍“event-sourcing-cqrs的model有哪些”,在日常操作中,相信很多人在event-sourcing-cqrs的model有哪些问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”event-sourcing-cqrs的model有哪些”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
创新互联建站从2013年开始,是专业互联网技术服务公司,拥有项目成都网站建设、网站制作网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元石拐做网站,已为上家服务,为石拐各地企业和个人服务,联系电话:028-86922220
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, ListnewEvents, int baseVersion) throws OptimisticLockingException; List load(UUID aggregateId); }
EventStore接口定义了store、load方法
Aggregate
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方法会执行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{ boolean isSatisfiedBy(T value); }
Specification接口定义了isSatisfiedBy方法
到此,关于“event-sourcing-cqrs的model有哪些”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注创新互联网站,小编会继续努力为大家带来更多实用的文章!
新闻标题:event-sourcing-cqrs的model有哪些
当前链接:http://myzitong.com/article/gisodg.html