Пример #1
0
        private bool AdjacentEventExecutedAndVersionPublished(EventStream @event)
        {
            var version = _eventPublishedVersionStore.GetPublishedVersion(@event.AggregateRootTypeCode, @event.AggregateRootId);

            //var version = _eventPublishedVersionStore.GetPublishedVersion(new EventPublishedVersionData {
            //    AggregateRootId = @event.AggregateRootId,
            //    AggregateRootTypeCode = @event.AggregateRootTypeCode
            //});

            if (version++ != @event.StartVersion)
            {
                _eventQueue.Enqueue(@event);
                return(false);
            }

            EventExecutedAndVersionPublished(@event);
            return(true);
        }
Пример #2
0
        private bool ProcessEvent(HandlerDescriptor handlerDescriptor, IVersionedEvent @event, string eventId,
                                  IDictionary <string, object> items)
        {
            var sourceInfo = (SourceInfo)items[StandardMetadata.SourceInfo];
            var traceInfo  = (TraceInfo)items[StandardMetadata.TraceInfo];

            if (@event.Version > 1)
            {
                var lastPublishedVersion = _publishedVersionStore.GetPublishedVersion(sourceInfo) + 1;
                if (lastPublishedVersion < @event.Version)
                {
                    var envelope = new Envelope <IEvent>(@event, eventId)
                    {
                        Items = items
                    };
                    _eventBus.Send(envelope);

                    if (LogManager.Default.IsDebugEnabled)
                    {
                        LogManager.Default.DebugFormat(
                            "The event cannot be process now as the version is not the next version, it will be handle later. aggregateRootType={0},aggregateRootId={1},lastPublishedVersion={2},eventVersion={3},eventType={4}.",
                            sourceInfo.Type.FullName,
                            sourceInfo.Id,
                            lastPublishedVersion,
                            @event.Version,
                            @event.GetType().FullName);
                    }

                    return(false);
                }

                if (lastPublishedVersion > @event.Version)
                {
                    if (LogManager.Default.IsDebugEnabled)
                    {
                        LogManager.Default.DebugFormat(
                            "The event is ignored because it is obsoleted. aggregateRootType={0},aggregateRootId={1},lastPublishedVersion={2},eventVersion={3},eventType={4}.",
                            sourceInfo.Type.FullName,
                            sourceInfo.Id,
                            lastPublishedVersion,
                            @event.Version,
                            @event.GetType().FullName);
                    }

                    return(false);
                }
            }

            var context = new EventContext(_commandBus, _resultBus);

            context.SourceInfo = sourceInfo;
            context.TraceInfo  = traceInfo;
            if (items.ContainsKey(StandardMetadata.CommandInfo))
            {
                context.CommandInfo = (SourceInfo)items[StandardMetadata.CommandInfo];
            }

            try {
                handlerDescriptor.Invode(context, @event);
                context.Commit();
            }
            catch (Exception ex)
            {
                var commandResult = new CommandResult(HandleStatus.SyncFailed, ex.Message)
                {
                    Result    = _serializer.Serialize(ex.Data),
                    ReplyType = CommandReturnMode.EventHandled
                };
                _resultBus.Send(commandResult, traceInfo);
                return(false);
            }

            _publishedVersionStore.AddOrUpdatePublishedVersion(sourceInfo, @event.Version);

            return(true);
        }