public async Task SaveAsync(TAggregate aggregate)
        {
            IEventSourcingAggregate <TAggregateId> aggregatePersistence = aggregate;

            foreach (var @event in aggregatePersistence.GetUncommittedEvents())
            {
                await eventStore.AppendEventAsync(@event);
            }
            aggregatePersistence.ClearUncommittedEvents();
        }
Beispiel #2
0
        public async Task SaveAsync(TAggregate aggregate)
        {
            try
            {
                IEventSourcingAggregate <TAggregateId> aggregatePersistence = aggregate;

                foreach (var @event in aggregatePersistence.GetUncommittedEvents())
                {
                    await eventStore.AppendEventAsync(@event);

                    await publisher.PublishAsync((dynamic)@event);
                }
                aggregatePersistence.ClearUncommittedEvents();
            }
            catch (EventStoreCommunicationException ex)
            {
                throw new RepositoryException("Unable to access persistence layer", ex);
            }
        }
Beispiel #3
0
        public async Task Save(IEventSourcingAggregate eventSourcingAggregate, Guid commitId, Action <IDictionary <string, object> > updateHeaders)
        {
            var commitHeaders = new Dictionary <string, object>
            {
                { CreatedByHeader, Environment.UserName },
                { CommitIdHeader, commitId },
                { AggregateClrTypeHeader, eventSourcingAggregate.GetType().AssemblyQualifiedName }
            };

            updateHeaders(commitHeaders);
            var newEvents       = eventSourcingAggregate.GetUncommittedEvents().ToList();
            var originalVersion = eventSourcingAggregate.StreamVersion - newEvents.Count;
            var expectedVersion = originalVersion == -1 ? ExpectedVersion.NoStream : ExpectedVersion.Any;

            var eventsToSave = newEvents.Select(e => ToNewStreamMessage(Guid.NewGuid(), e, commitHeaders)).ToArray();

            try
            {
                if (eventsToSave.Length < WritePageSize)
                {
                    await _store.AppendToStream(eventSourcingAggregate.StreamId, expectedVersion, eventsToSave);
                }
                else
                {
                    //TODO
                }
            }
            catch (WrongExpectedVersionException weve)
            {
                var sqlE = weve.InnerException as SqlException;
                if (sqlE?.Number == 2601)
                {
                    throw new DuplicateEventStoreAggregateException(eventSourcingAggregate.StreamId, weve);
                }
            }
            eventSourcingAggregate.ClearUncommittedEvents();
        }