예제 #1
0
        public void SaveChanges()
        {
            var aggregatesAndEvents = from entry in unitOfWork
                                      let aggregateRoot = entry.AggregateRoot
                                                          let eventStream = entry.EventStream
                                                                            from @event in aggregateRoot.GetUncommittedChanges()
                                                                            orderby @event.TimeStamp
                                                                            select
                                                                            new
            {
                EventStream = eventStream,
                Event       = @event
            };
            var currentChangeSequence = new Lazy <int>(GenerateChangeSequence);

            foreach (var aggregatesAndEvent in aggregatesAndEvents)
            {
                IDomainEvent pendingEvent = aggregatesAndEvent.Event;
                EventStream  eventStream  = aggregatesAndEvent.EventStream;
                dynamic      asDynamic    = pendingEvent.AsDynamic();
                asDynamic.SetChangeSequence(currentChangeSequence.Value);
                dispatcher.Dispatch(pendingEvent, eventStream.Id);
                eventStream.History.Add(pendingEvent);
            }

            foreach (AggregateRoot aggregateRoot in unitOfWork.Select(x => x.AggregateRoot))
            {
                aggregateRoot.ClearUncommittedChanges();
            }

            documentSession.SaveChanges();
        }