Exemplo n.º 1
0
 public void AddCompleteAggregate(string aggregateRootId, AggregateEventAppendResult result)
 {
     if (_aggregateEventAppendResultDict.TryAdd(aggregateRootId, result))
     {
         var completedAggregateRootCount = _aggregateEventAppendResultDict.Keys.Count;
         if (completedAggregateRootCount == _expectedAggregateRootCount)
         {
             var eventAppendResult = new EventAppendResult();
             foreach (var entry in _aggregateEventAppendResultDict)
             {
                 if (entry.Value.EventAppendStatus == EventAppendStatus.Success)
                 {
                     eventAppendResult.AddSuccessAggregateRootId(entry.Key);
                 }
                 else if (entry.Value.EventAppendStatus == EventAppendStatus.DuplicateEvent)
                 {
                     eventAppendResult.AddDuplicateEventAggregateRootId(entry.Key);
                 }
                 else if (entry.Value.EventAppendStatus == EventAppendStatus.DuplicateCommand)
                 {
                     eventAppendResult.AddDuplicateCommandId(entry.Value.DuplicateCommandId);
                 }
             }
             TaskCompletionSource.TrySetResult(new AsyncTaskResult <EventAppendResult>(AsyncTaskStatus.Success, eventAppendResult));
         }
     }
 }
Exemplo n.º 2
0
        private void BatchAppend(string aggregateRootId, IList <DomainEventStream> eventStreamList, EventAppendResult eventAppendResult)
        {
            lock (_lockObj)
            {
                var aggregateInfo = _aggregateInfoDict.GetOrAdd(aggregateRootId, x => new AggregateInfo());

                //检查提交过来的第一个事件的版本号是否是当前聚合根的当前版本号的下一个版本号
                if (eventStreamList.First().Version != aggregateInfo.CurrentVersion + 1)
                {
                    eventAppendResult.AddDuplicateEventAggregateRootId(aggregateRootId);
                    return;
                }
                //检查提交过来的事件本身是否满足版本号的递增关系
                for (var i = 0; i < eventStreamList.Count - 1; i++)
                {
                    if (eventStreamList[i + 1].Version != eventStreamList[i].Version + 1)
                    {
                        eventAppendResult.AddDuplicateEventAggregateRootId(aggregateRootId);
                        return;
                    }
                }

                //检查重复处理的命令ID
                var duplicateCommandIds = new List <string>();
                foreach (DomainEventStream eventStream in eventStreamList)
                {
                    if (aggregateInfo.CommandDict.ContainsKey(eventStream.CommandId))
                    {
                        duplicateCommandIds.Add(eventStream.CommandId);
                    }
                }
                if (duplicateCommandIds.Count > 0)
                {
                    eventAppendResult.AddDuplicateCommandIds(aggregateRootId, duplicateCommandIds);
                    return;
                }

                foreach (DomainEventStream eventStream in eventStreamList)
                {
                    aggregateInfo.EventDict[eventStream.Version]     = eventStream;
                    aggregateInfo.CommandDict[eventStream.CommandId] = eventStream;
                    aggregateInfo.CurrentVersion = eventStream.Version;
                }

                eventAppendResult.AddSuccessAggregateRootId(aggregateRootId);
            }
        }