Пример #1
0
        /// <inheritdoc />
        public async Task Handle(TCommand command)
        {
            var root = Create(command);

            var events    = root.GetUncommittedEvents().OfType <Event>().ToList();
            var eventType = events.Select(e => e.MessageType).SingleOrDefault();

            if (eventType == null)
            {
                throw new InvalidOperationException("More than one event type produced by a command");
            }
            command.EventType = eventType;

            foreach (var e in events)
            {
                e.CommandId     = command.MessageId;
                e.AncestorId    = command.MessageId;
                e.CorrelationId = command.CorrelationId;
            }

            if (command.UseTimestamp)
            {
                root.TimestampEvents(command.Timestamp);
            }

            await _repository.Save(root);
        }
Пример #2
0
        /// <inheritdoc />
        public virtual async Task Handle(TCommand command)
        {
            if (command == null)
            {
                throw new ArgumentNullException(typeof(TCommand).GetFriendlyName());
            }

            var root = await _repository.Find <TRoot>(command.Target, ComputeHash);

            if (root == null)
            {
                throw new ArgumentNullException(typeof(TRoot).GetFriendlyName());
            }
            if (command.Timestamp < root.Timestamp)
            {
                throw new InvalidOperationException(
                          $"{typeof(TCommand).Name} command ({command.Timestamp}) updating the past of the aggregate {typeof(TRoot).Name}:{command.Target} ({root.Timestamp}) ");
            }

            Act(root, command);

            if (command.UseTimestamp)
            {
                root.TimestampEvents(command.Timestamp);
            }

            var events    = root.GetUncommittedEvents().OfType <Event>().ToList();
            var eventType = events.Select(e => e.MessageType).SingleOrDefault();

            if (eventType == null)
            {
                throw new InvalidOperationException("More than one event type produced by a command");
            }
            foreach (var e in events)
            {
                e.CommandId     = command.MessageId;
                e.AncestorId    = command.MessageId;
                e.CorrelationId = command.CorrelationId;
            }

            command.EventType = eventType;

            await _repository.Save(root);
        }
Пример #3
0
            private async Task Handle(IEvent e)
            {
                // _log.Trace($"{typeof(TSaga).Name}.When({e.EventType}[{e.Stream}])");
                _log.Trace($"{e.MessageType}", typeof(TSaga).GetFriendlyName());

                try
                {
                    var   emptySaga     = new TSaga();
                    var   id            = emptySaga.SagaId(e);
                    var   isInitializer = emptySaga.IsInitializer(e);
                    TSaga saga;
                    if (isInitializer)
                    {
                        saga = await _repository.GetOrAdd <TSaga>(id);
                    }
                    else
                    {
                        saga = await _repository.Find <TSaga>(id);
                    }

                    if (saga == null)
                    {
                        return;
                    }
                    var saveEvent = ((Event)e).Copy();

                    saga.When(saveEvent);

                    var commands = saga.GetUncommittedCommands().OfType <Command>();
                    foreach (var c in commands)
                    {
                        c.AncestorId    = e.MessageId;
                        c.CorrelationId = id;
                    }

                    await _repository.Save(saga);
                }
                catch (Exception exception)
                {
                    _log.Errors.Add(exception);
                }
            }