Example #1
0
        private void InternalStore(string tag, Guid aggregateId, long version, IEnumerable <IEvent> events)
        {
            IEventStream stream = Load(aggregateId);

            if (stream.Version != version)
            {
                throw new InvalidOperationException("Stream has already been modified");
            }

            streams[aggregateId] = stream.Append(events);
            UpdateAggregateIndex(tag, aggregateId);
        }
        /// <summary>
        ///     Persists the state of the specified aggregate by adding new events to the event store.
        /// </summary>
        /// <param name="aggregate">The aggregate to persist.</param>
        public void Save(TAggregate aggregate)
        {
            var events = aggregate.PendingEvents.ToArray();

            foreach (var e in events)
            {
                eventStream.Append(new[]
                {
                    e.ToStoredEvent()
                }).Wait();

                e.SetAggregate(aggregate);
            }

            // move pending events to the event history
            aggregate.IfTypeIs <EventSourcedAggregate>()
            .ThenDo(a => a.ConfirmSave());

            // publish the events
            bus.PublishAsync(events)
            .Do(onNext: e => { },
                onError: ex => Console.WriteLine(ex.ToString()))
            .Wait();
        }