Пример #1
0
        public List<DomainEvent> EventsForAggregate(AggregateKey key)
        {
            List<DomainEvent> eventsForAggregate = new List<DomainEvent>();

            var streamName = _aggregateIdToStreamName(key.AggregateType, key.AggregateId);

            StreamEventsSlice currentSlice;
            var nextSliceStart = 0;
            do
            {
                currentSlice = _eventStoreConnection
                                .ReadStreamEventsForwardAsync(streamName, nextSliceStart, ReadPageSize, false)
                                .Result;
                nextSliceStart = currentSlice.NextEventNumber;

                foreach (var evnt in currentSlice.Events)
                {
                    DomainEvent aggrEvent = (DomainEvent)DeserializeEvent(evnt.OriginalEvent.Metadata, evnt.OriginalEvent.Data);
                    eventsForAggregate.Add(aggrEvent);
                }

            } while (!currentSlice.IsEndOfStream);

            return eventsForAggregate;
        }
Пример #2
0
        /// <summary>
        /// Collects all processed events for given aggregate and return them as a list
        /// used to build up an aggregate from its history (Domain.ReconstituteFromHistory)
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public List<DomainEvent> EventsForAggregate(AggregateKey key)
        {
            List<EventDescriptor> eventDescriptors;

            if (!_current.TryGetValue(key.AggregateId, out eventDescriptors))
            {
                throw new AggregateNotFoundException();
            }

            return eventDescriptors.Select(desc => desc.EventData).ToList();
        }
Пример #3
0
        public List<DomainEvent> EventsForAggregate(AggregateKey key)
        {
            List<DomainEvent> eventsForAggregate = new List<DomainEvent>();

            var streamName = _aggregateIdToStreamName(key.AggregateType, key.AggregateId);

            using (ISession session = this.factory.OpenSession())
            using (ITransaction tx = session.BeginTransaction())
            {
                var events = session
                            .QueryOver<RecordedEvent>()
                            .Where(e => e.EventStreamId == streamName)
                            .OrderBy(e => e.EventNumber).Asc
                            .Future();

                foreach (var evnt in events)
                {
                    DomainEvent aggrEvent = (DomainEvent)DeserializeEvent(evnt.Metadata, evnt.Data);
                    eventsForAggregate.Add(aggrEvent);
                }
            }

            return eventsForAggregate;
        }
Пример #4
0
        /// <summary>
        /// Saves the events for an aggregate that might be unknown to the system.
        /// </summary>
        /// <param name="key"></param>
        /// <param name="expectedRevision"></param>
        /// <param name="events"></param>
        public void SaveEvents(AggregateKey key, int expectedRevision, IEnumerable<DomainEvent> events)
        {
            List<EventDescriptor> eventDescriptors;

            // try to get event descriptors list for given aggregate id
            // otherwise -> create empty dictionary
            if (!_current.TryGetValue(key.AggregateId, out eventDescriptors))
            {
                eventDescriptors = new List<EventDescriptor>();
                _current.Add(key.AggregateId, eventDescriptors);
            }
            // check whether latest event version matches current aggregate version
            // otherwise -> throw exception
            else if (eventDescriptors[eventDescriptors.Count - 1].Revision != expectedRevision && expectedRevision != -1)
            {
                throw new ConcurrencyException();
            }
            var i = expectedRevision;

            // iterate through current aggregate events increasing version with each processed event
            foreach (var @event in events)
            {
                i++;
                // @event.Version = i;

                // push event to the event descriptors list for current aggregate
                eventDescriptors.Add(new EventDescriptor(key.AggregateId, @event, i));
                // publish current event to the bus for further processing by subscribers
                _publisher.Publish(@event as Event);
            }
        }
Пример #5
0
        public void SaveEvents(AggregateKey key, int expectedRevision, IEnumerable<DomainEvent> events)
        {
            var commitHeaders = new Dictionary<string, object>
            {
                {CommitIdHeader, Guid.NewGuid()},
                {AggregateClrTypeHeader, key.AggregateType.AssemblyQualifiedName}
            };

            // updateHeaders(commitHeaders);

            var streamName = _aggregateIdToStreamName(key.AggregateType, key.AggregateId);
            var newEvents = events.Cast<object>().ToList();

            var eventNumber = expectedRevision;

            var preparedEvents = PrepareEvents(newEvents, commitHeaders).ToList();

            using (ISession session = this.factory.OpenSession())
            using (ITransaction tx = session.BeginTransaction())
            {
                foreach (var uncommittedEvent in preparedEvents)
                {
                    var entry = new RecordedEvent()
                    {
                        Created = DateTime.UtcNow,
                        CreatedEpoch = (long)(DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalMilliseconds,
                        Data = uncommittedEvent.Data,
                        EventId = uncommittedEvent.EventId,
                        EventStreamId = streamName,
                        IsJson = uncommittedEvent.IsJson,
                        EventType = uncommittedEvent.Type,
                        Metadata = uncommittedEvent.Metadata,
                        EventNumber = eventNumber
                    };
                    session.Save(entry);
                    eventNumber += 1;
                }

                tx.Commit();
            }

            foreach (var @event in events)
            {
                _publisher.Publish(@event);
            }
        }
Пример #6
0
        public void SaveEvents(AggregateKey key, int expectedRevision, IEnumerable<DomainEvent> uncommittedEvents)
        {
            var commitHeaders = new Dictionary<string, object>
            {
                {CommitIdHeader, Guid.NewGuid()},
                {AggregateClrTypeHeader, key.AggregateType.AssemblyQualifiedName}
            };

            var streamName = _aggregateIdToStreamName(key.AggregateType, key.AggregateId);
            var newEvents = uncommittedEvents.Cast<object>().ToList();
            var expectedVersion = expectedRevision <= 0 ? ExpectedVersion.NoStream : expectedRevision - 1;

            expectedVersion = ExpectedVersion.Any;

            var preparedEvents = PrepareEvents(newEvents, commitHeaders).ToList();

            if (preparedEvents.Count < WritePageSize)
            {
                _eventStoreConnection
                    .AppendToStreamAsync(streamName, expectedVersion, preparedEvents)
                    .Wait();
            }
            else
            {
                var transaction = _eventStoreConnection
                                        .StartTransactionAsync(streamName, expectedVersion)
                                        .Result;

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

                transaction.CommitAsync().Wait();
            }

            foreach (var @event in uncommittedEvents)
            {
                _publisher.Publish(@event);
            }
        }