コード例 #1
0
ファイル: StoreContext.cs プロジェクト: mvcguy/eventstoreplay
        public TAggregate GetById(Guid id, long version)
        {
            if (version < 0)
            {
                return(default(TAggregate));
            }

            if (id == Guid.Empty)
            {
                return(default(TAggregate));
            }

            var streamName = AggregateIdToStreamName(typeof(TAggregate), id);
            var aggregate  = new TAggregate();

            aggregate.Id      = id;
            aggregate.Version = version;

            long sliceStart = 0;

            aggregate.Events = new List <TEventModel>();
            StreamEventsSlice currentSlice;

            do
            {
                var sliceCount = sliceStart + ReadPageSize <= version
                    ? ReadPageSize
                    : version - sliceStart + 1;

                currentSlice = EventStoreConnection.ReadStreamEventsForwardAsync(streamName, sliceStart, (int)sliceCount, false).Result;

                if (currentSlice.Status == SliceReadStatus.StreamNotFound ||
                    currentSlice.Status == SliceReadStatus.StreamDeleted)
                {
                    return(default(TAggregate));
                }

                sliceStart = currentSlice.NextEventNumber;

                foreach (var @event in currentSlice.Events)
                {
                    aggregate.Events.Add(new TEventModel
                    {
                        Data           = @event.OriginalEvent.Data,
                        Metadata       = @event.OriginalEvent.Metadata,
                        SequenceNumber = @event.OriginalEventNumber,
                        Version        = @event.OriginalEvent.EventNumber,
                        EventType      = @event.OriginalEvent.EventType,
                        IsJson         = @event.OriginalEvent.IsJson,
                    });
                }
            } while (version >= currentSlice.NextEventNumber && !currentSlice.IsEndOfStream);

            if (!aggregate.Events.Any())
            {
                return(default(TAggregate));
            }

            return(aggregate);
        }
コード例 #2
0
        public async Task RebuildModel()
        {
            StreamEventsSlice currentSlice;
            long sliceStart = StreamPosition.Start;

            do
            {
                currentSlice = await EventStoreConnection.ReadStreamEventsForwardAsync(
                    GetStreamName(),
                    sliceStart,
                    50,
                    true
                    );

                if (!_subscribeFromEvent.HasValue && currentSlice.LastEventNumber > -1)
                {
                    _subscribeFromEvent = currentSlice.LastEventNumber;
                }
                else if (_subscribeFromEvent.HasValue)
                {
                    _subscribeFromEvent = Math.Max(_subscribeFromEvent.Value, currentSlice.LastEventNumber);
                }
                sliceStart = currentSlice.NextEventNumber;

                foreach (var e in currentSlice.Events)
                {
                    await DispatchEvents(e);
                }
            } while (!currentSlice.IsEndOfStream);
        }
コード例 #3
0
        protected async Task <TAggregate> GetById(string streamName, int version)
        {
            if (version <= 0)
            {
                throw new InvalidOperationException("Cannot get version <= 0");
            }

            long sliceStart = StreamPosition.Start;
            StreamEventsSlice currentSlice;
            AggregateRoot     aggregate = null;

            do
            {
                var sliceCount = sliceStart + READ_PAGE_SIZE <= version
                                    ? READ_PAGE_SIZE
                                    : (int)(version - sliceStart + 1);

                currentSlice = await EventStoreConnection.ReadStreamEventsForwardAsync(streamName, sliceStart, sliceCount, false);

                if (currentSlice.Status == SliceReadStatus.StreamNotFound)
                {
                    throw new AggregateNotFoundException(streamName, typeof(TAggregate));
                }

                if (currentSlice.Status == SliceReadStatus.StreamDeleted)
                {
                    throw new AggregateDeletedException(streamName, typeof(TAggregate));
                }

                sliceStart = currentSlice.NextEventNumber;

                var domainEvents = currentSlice.Events.Select(e => EventSerializer.DeserializeEvent(e.OriginalEvent)).ToList();
                if (aggregate == null)
                {
                    aggregate = AggregateFactory.CreateAggregate <TAggregate>();
                }
                else
                {
                    await ApplyChangesToAggregate(aggregate, domainEvents);
                }
            } while (version >= currentSlice.NextEventNumber && !currentSlice.IsEndOfStream);

            if (version < Int32.MaxValue && aggregate.Version != version)
            {
                throw new AggregateVersionException(streamName, typeof(TAggregate), aggregate.Version, version);
            }

            return((TAggregate)aggregate);
        }
コード例 #4
0
        public async Task <Post> GetById(Guid id)
        {
            var events = new List <IEvent>();
            StreamEventsSlice currentSlice;
            var nextSliceStart = (long)StreamPosition.Start;
            var streamName     = GetStreamName <Post>(id);

            do
            {
                currentSlice = await EventStoreConnection
                               .ReadStreamEventsForwardAsync(streamName, nextSliceStart, 200, false);

                nextSliceStart = currentSlice.NextEventNumber;
                events.AddRange(currentSlice.Events.Select(x => x.DeserializeEvent()));
            } while (!currentSlice.IsEndOfStream);
            var aggregate = Post.Rehydrate(events);

            return(aggregate);
        }
コード例 #5
0
 protected virtual async Task <StreamEventsSlice> ReadStreamEventsForwardAsync(string streamName, long version) =>
 await EventStoreConnection.ReadStreamEventsForwardAsync(streamName, version, this.EventStoreConfigurations.ReadBatchSize, false);