private void Persist(TAggregate aggregate)
        {
            var uncommitedEvents = aggregate.GetUncommitedEvents();
            var snapshot         = aggregate.GetSnapshot();
            var originalVersion  = aggregate.Version - uncommitedEvents.Count;
            var persistEvents    = new PersistEventsCommand <TIdentity>(aggregate.Id, uncommitedEvents, _processingCommandId, originalVersion);
            var persistSnapshot  = new PersistSnapshotCommand <TSnapshot>(snapshot, _previouslyDispatchedMessages, uncommitedEvents.Count);
            var publishEvents    = new PublishEventsCommand(uncommitedEvents);

            CommandComposition
            .StartWith(persistEvents)
            .ContinueWith(persistSnapshot)
            .ContinueWith(publishEvents)
            .Run();
        }
Beispiel #2
0
        public void Execute(Envelope <C> command)
        {
            var messsageId   = Guid.NewGuid();
            var newAggregate = new Aggregate(command.Body.Identity);

            newAggregate.SetName(command.Body.Name);

            var persistEvents   = new PersistEventsCommand <AggregateIdentity>(newAggregate.Id, newAggregate.GetUncommitedEvents(), command.MessageId);
            var persistSnapshot = new PersistSnapshotCommand <AggregateSnapshot>(newAggregate.GetSnapshot(), Enumerable.Empty <Guid>().ToList(), 1);
            var publishEvents   = new PublishEventsCommand(newAggregate.GetUncommitedEvents());

            _commandComposition
            .StartWith(persistEvents)
            .ContinueWith(persistSnapshot)
            .ContinueWith(publishEvents)
            .Run();
        }
        public void Execute(Envelope <TCommand> command)
        {
            _processingCommandId = command.MessageId;
            var restoreAggregateCommand = new RestoreEventSourcedAggregateCommand <TIdentity, TSnapshot>(command.Body.Identity, command.Body.Version, command.MessageId);
            var restoreAggregateResult  = CommandComposition
                                          .StartWith <RestoreEventSourcedAggregateCommand <TIdentity, TSnapshot>, RestoreAggregateCommandResult <TAggregate> >(restoreAggregateCommand)
                                          .Run();

            _previouslyDispatchedMessages = restoreAggregateResult.PreviouslyDispatchedMessages;

            if (restoreAggregateResult.AlreadyDispatched)
            {
                var publishEvents = new PublishEventsCommand(restoreAggregateResult.NeedRedispatchEvents);
                CommandComposition
                .StartWith(publishEvents)
                .Run();
            }
            else
            {
                var aggregate = restoreAggregateResult.Aggregate;
                IdempotentExecute(aggregate, command.Body);
                Persist(aggregate);
            }
        }