protected override void SaveAggregate(Aggregate aggregate, Guid transactionId)
        {
            //Taken from https://github.com/pgermishuys/getting-started-with-event-store/blob/master/src/GetEventStoreRepository/GetEventStoreRepository.cs
            var commitHeaders = new Dictionary <string, object>
            {
                { CommitIdHeader, transactionId },
                { AggregateClrTypeHeader, aggregate.GetType().AssemblyQualifiedName }
            };

            IList <Event> newEvents       = aggregate.GetUncommittedEvents();
            int           originalVersion = aggregate.Version - newEvents.Count;
            int           expectedVersion = originalVersion; //http://stackoverflow.com/a/20204729

            if (originalVersion == 0)
            {
                expectedVersion = ExpectedVersion.NoStream;
            }
            IEnumerable <EventData> eventsToSave = newEvents.Select(e => ToEventData(Guid.NewGuid(), e, commitHeaders));

            string streamName = GetStreamName(aggregate.GetType(), aggregate.Id);

            if (eventsToSave.Count() < WritePageSize)
            {
                connection.AppendToStreamAsync(streamName, ExpectedVersion.Any, eventsToSave).Wait();
            }
            else
            {
                var transaction = connection.StartTransactionAsync(streamName, expectedVersion).Result;

                var position = 0;
                while (position < eventsToSave.Count())
                {
                    var pageEvents = eventsToSave.Skip(position).Take(WritePageSize);
                    transaction.WriteAsync(pageEvents).Wait();
                    position += WritePageSize;
                }

                transaction.CommitAsync().Wait();
            }

            aggregate.ClearUncommittedEvents();
        }
        /// <inheritdoc />
        public async Task StoreAsync(Aggregate aggregate, IDocumentSession transientSession = null)
        {
            using (var session = transientSession ?? _store.OpenSession())
            {
                // Take non-persisted events, push them to the event stream, indexed by the aggregate ID
                var events = aggregate.GetUncommittedEvents().ToArray();

                session.Events.Append(aggregate.Id.Value, events);

                //Only save and publish changes if it is our own session (meaning none supplied)
                if (transientSession == null)
                {
                    await session.SaveChangesAsync();
                    await PublishEventsAsync(events);

                    // Once successfully persisted, clear events from list of uncommitted events
                    aggregate.ClearUncommittedEvents();
                }
            }
        }
示例#3
0
 public void SaveChanges(Aggregate aggregate)
 {
     SaveEvents(new AggregateKey(aggregate.GetType(), aggregate.AggregateId), aggregate.Revision, aggregate.UncommittedChanges());
     aggregate.ClearUncommittedEvents();
 }
示例#4
0
 public void SaveChanges(Aggregate aggregate)
 {
     SaveEvents(new AggregateKey(aggregate.GetType(), aggregate.AggregateId), aggregate.Revision, aggregate.UncommittedChanges());
     aggregate.ClearUncommittedEvents();
 }