Exemple #1
0
        public static async Task <long?> Handle <S, C>(CommandEnvelope <C> commandEnvelope, IAggregate <S> aggregate, IEventStore eventStore, CancellationToken cancel = default) where C : class where S : class
        {
            if (commandEnvelope.AggregateId == null || commandEnvelope.Command == null)
            {
                throw new Exception();
            }

            var streamName = aggregate.GetStreamName(commandEnvelope.AggregateId);

            var(version, state) = await Load(streamName, aggregate, eventStore, cancel);

            var events = aggregate.Handle(state, commandEnvelope.Command)
                         .Select((e, i) => e.CreateEventEnvelope(commandEnvelope.AggregateId, version + i + 1))
                         .ToArray();

            return(await eventStore.Save(streamName, cancel, events));
        }