Ejemplo n.º 1
0
        public async Task Save <TAggregate>(TAggregate aggregate) where TAggregate : Aggregate
        {
            if (aggregate == null)
            {
                throw new ArgumentNullException("aggregate");
            }

            // test for uncommitted save unsaved changes
            var uncommittedEvents = aggregate.GetUncommittedEvents().ToArray();

            if (uncommittedEvents.Any() == false)
            {
                return; // Task.FromResult(0);
            }
            string streamName = _eventStreamNameResolver.Resolve <TAggregate>(aggregate.AggregateId);

            //// append events to stream
            //var task = _eventStore.AppendToEventStream(streamName, aggregate.CommittedVersion, uncommittedEvents);
            //return task.ContinueWith((t) => {
            //    _eventPublisher.Publish(aggregate.AggregateId, uncommittedEvents);

            //    // commit aggregate (e.g. reset)
            //    aggregate.Commit();

            //    //return Task.FromResult(0);
            //});
            //// publish events


            await _eventStore.AppendToEventStream(streamName, aggregate.CommittedVersion, uncommittedEvents);

            // publish events
            _eventPublisher.Publish(aggregate.AggregateId, uncommittedEvents);

            // commit aggregate (e.g. reset)
            aggregate.Commit();
        }