public TAggregate GetById <TAggregate>(Guid id, int version) where TAggregate : class, IEventSource
        {
            if (version <= 0)
            {
                throw new InvalidOperationException("Cannot get version <= 0");
            }

            var streamName = _streamNameBuilder.GenerateForAggregate(typeof(TAggregate), id);
            var aggregate  = ConstructAggregate <TAggregate>();


            long sliceStart = 0;
            StreamEventsSlice currentSlice;
            var appliedEventCount = 0;

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

                currentSlice = _streamStoreConnection.ReadStreamForward(streamName, sliceStart, (int)sliceCount);

                if (currentSlice is StreamNotFoundSlice)
                {
                    throw new AggregateNotFoundException(id, typeof(TAggregate));
                }

                if (currentSlice is StreamDeletedSlice)
                {
                    throw new AggregateDeletedException(id, typeof(TAggregate));
                }

                sliceStart = currentSlice.NextEventNumber;

                appliedEventCount += currentSlice.Events.Length;
                aggregate.RestoreFromEvents(currentSlice.Events.Select(evt => _eventSerializer.Deserialize(evt)));
            } while (version > currentSlice.NextEventNumber && !currentSlice.IsEndOfStream);

            if (version != Int32.MaxValue && version != appliedEventCount)
            {
                throw new AggregateVersionException(id, typeof(TAggregate), version, aggregate.ExpectedVersion);
            }

            if (version != Int32.MaxValue && aggregate.ExpectedVersion != version - 1)
            {
                throw new AggregateVersionException(id, typeof(TAggregate), version, aggregate.ExpectedVersion);
            }

            return(aggregate);
        }
Пример #2
0
        public void ThrowsOnGetDeletedAggregate()
        {
            var aggregateId = SaveTestAggregateWithoutCustomHeaders(_repo, 10);
            var streamName  = _streamNameBuilder.GenerateForAggregate(typeof(TestWoftamAggregate), aggregateId);

            _connection.DeleteStream(new StreamName(streamName), 10);

            // Assert.Throws<AggregateDeletedException>(() => _repo.GetById<TestAggregate>(aggregateId));
            //Looks like an api change
            Assert.Throws <AggregateNotFoundException>(() => _repo.GetById <TestWoftamAggregate>(aggregateId));
        }
Пример #3
0
 /// <summary>
 /// Aggregate Stream listener
 /// i.e. [AggregateType]-[id]
 /// </summary>
 /// <typeparam name="TAggregate">The Aggregate type used to generate the stream name</typeparam>
 /// <param name="id"></param>
 /// <param name="checkpoint"></param>
 /// <param name="blockUntilLive"></param>
 /// <param name="timeout">timeout in milliseconds default = 1000</param>
 public void Start <TAggregate>(
     Guid id,
     long?checkpoint     = null,
     bool blockUntilLive = false,
     int timeout         = 1000) where TAggregate : class, IEventSource
 {
     Start(
         _streamNameBuilder.GenerateForAggregate(typeof(TAggregate), id),
         checkpoint,
         blockUntilLive,
         timeout);
 }
 /// <summary>
 /// Aggregate Stream listener
 /// i.e. [AggregateType]-[id]
 /// </summary>
 /// <typeparam name="TAggregate">The Aggregate type used to generate the stream name</typeparam>
 /// <param name="id"></param>
 /// <param name="checkpoint"></param>
 /// <param name="blockUntilLive"></param>
 /// <param name="cancelWaitToken">Cancellation token to cancel waiting if blockUntilLive is true</param>
 public void Start <TAggregate>(
     Guid id,
     long?checkpoint     = null,
     bool blockUntilLive = false,
     CancellationToken cancelWaitToken = default(CancellationToken)) where TAggregate : class, IEventSource
 {
     Start(
         _streamNameBuilder.GenerateForAggregate(typeof(TAggregate), id),
         checkpoint,
         blockUntilLive,
         cancelWaitToken);
 }
Пример #5
0
 /// <summary>
 /// Aggregate-[id] Stream Reader
 /// i.e. [AggregateType]-[id]
 /// </summary>
 /// <typeparam name="TAggregate">The Aggregate type used to generate the stream name</typeparam>
 /// <param name="id">Aggregate id to generate stream name.</param>
 /// <param name="checkpoint">The starting point to read from.</param>
 /// <param name="count">The count of items to read</param>
 /// <param name="readBackwards">Read the stream backwards</param>
 /// <returns>Returns true if any events were read from the stream</returns>
 public bool Read <TAggregate>(
     Guid id,
     long?checkpoint    = null,
     long?count         = null,
     bool readBackwards = false) where TAggregate : class, IEventSource
 {
     return(Read(
                _streamNameBuilder.GenerateForAggregate(typeof(TAggregate), id),
                checkpoint,
                count,
                readBackwards));
 }
        private void CreateUser()
        {
            var evt = MessageBuilder.New(
                () => new UserMsgs.UserCreated(
                    _userId,
                    _userSidFromAuthProvider,
                    AuthProvider,
                    AuthDomain,
                    UserName,
                    FullName,
                    GivenName,
                    Surname,
                    Email));
            var stream = _streamNamer.GenerateForAggregate(typeof(User), _userId);

            _fixture.StreamStoreConnection.AppendToStream(
                stream,
                ExpectedVersion.Any,
                null,
                _fixture.EventSerializer.Serialize(evt));
            _fixture.RepositoryEvents.WaitForMsgId(evt.MsgId, TimeSpan.FromMilliseconds(100));
            _fixture.ClearQueues();
        }
Пример #7
0
        public StreamReaderTests(ITestOutputHelper toh, StreamStoreConnectionFixture fixture)
        {
            _toh = toh;
            _streamNameBuilder = new PrefixedCamelCaseStreamNameBuilder("UnitTest");
            var mockStreamStore = new MockStreamStoreConnection("Test-" + Guid.NewGuid());

            mockStreamStore.Connect();

            _stores.Add(mockStreamStore);
            _stores.Add(fixture.Connection);

            _streamName = _streamNameBuilder.GenerateForAggregate(typeof(TestAggregate), Guid.NewGuid());


            foreach (var store in _stores)
            {
                AppendEvents(NUM_OF_EVENTS, store, _streamName);
            }
        }
        public StreamStoreSubscriptionTests(StreamStoreConnectionFixture fixture)
        {
            _admin             = fixture.AdminCredentials;
            _streamNameBuilder = new PrefixedCamelCaseStreamNameBuilder("UnitTest");
            var mockStreamStore = new MockStreamStoreConnection(nameof(MockStreamStoreConnection));

            mockStreamStore.Connect();
            fixture.Connection.Connect();
            _stores.Add(mockStreamStore);
            _stores.Add(fixture.Connection);

            _streamName = _streamNameBuilder.GenerateForAggregate(typeof(TestAggregate), Guid.NewGuid());
            var eventCount = 10;

            foreach (var store in _stores)
            {
                AppendEvents(eventCount, store, _streamName);
            }
        }