/// <summary> /// 事件重演时,验证某个给定的事件是否有效 /// </summary> private void VerifySourcableEvent(long currentVersion, SourcableEvent sourcableEvent) { if (sourcableEvent.Version <= DefaultVersion) { throw new EventSourcingException(string.Format("事件的版本号无效,必须大于等于{0}", DefaultVersion + 1)); } if (currentVersion == DefaultVersion) { if (sourcableEvent.Version != DefaultVersion + 1) { throw new EventSourcingException(string.Format("应用到聚合根上的第一个事件的版本必须为{0}.", DefaultVersion + 1)); } } else { if (sourcableEvent.AggregateRootId != _uniqueId) { var message = string.Format("不允许将其他聚合根(aggregateRootId:{0})的事件(详细信息:{1})应用到当前聚合根(aggregateRootId:{2}).", sourcableEvent.AggregateRootId, sourcableEvent.ToString(), _uniqueId); throw new EventSourcingException(message); } if (sourcableEvent.Version != currentVersion + 1) { var message = string.Format("不允许将版本为{0}事件应用到聚合根(aggregateRootId:{1}). 因为该聚合根的当前版本是{2}, 只有版本为{3}的事件才可以被应用到该聚合根.", sourcableEvent.Version, _uniqueId, currentVersion, currentVersion + 1); throw new EventSourcingException(message); } } }
/// <summary> /// 追加一个新的溯源事件到队列 /// </summary> private void AppendSourcableEvent(SourcableEvent sourcableEvent) { if (_sourcableEvents == null) { _sourcableEvents = new Queue <SourcableEvent>(); } _sourcableEvents.Enqueue(sourcableEvent); }