Esempio n. 1
0
        /// <summary>
        /// Attempts to get the aggregate root entity associated with the aggregate identifier.
        /// </summary>
        /// <param name="identifier">The aggregate identifier.</param>
        /// <returns>The found <typeparamref name="TAggregateRoot"/>, or empty if not found.</returns>
        public async Task <Optional <TAggregateRoot> > GetOptionalAsync(string identifier)
        {
            Aggregate aggregate;

            if (_unitOfWork.TryGet(identifier, out aggregate))
            {
                return(new Optional <TAggregateRoot>((TAggregateRoot)aggregate.Root));
            }
            var streamUserCredentials = _configuration.StreamUserCredentialsResolver.Resolve(identifier);
            var streamName            = _configuration.StreamNameResolver.Resolve(identifier);
            var slice =
                await
                _connection.ReadStreamEventsForwardAsync(streamName, StreamPosition.Start, _configuration.SliceSize,
                                                         false, streamUserCredentials);

            if (slice.Status == SliceReadStatus.StreamDeleted || slice.Status == SliceReadStatus.StreamNotFound)
            {
                return(Optional <TAggregateRoot> .Empty);
            }
            var root = _rootFactory();

            root.Initialize(slice.Events.Select(resolved => _configuration.Deserializer.Deserialize(resolved)));
            while (!slice.IsEndOfStream)
            {
                slice =
                    await
                    _connection.ReadStreamEventsForwardAsync(streamName, slice.NextEventNumber, _configuration.SliceSize,
                                                             false, streamUserCredentials);

                root.Initialize(slice.Events.Select(resolved => _configuration.Deserializer.Deserialize(resolved)));
            }
            aggregate = new Aggregate(identifier, slice.LastEventNumber, root);
            _unitOfWork.Attach(aggregate);
            return(new Optional <TAggregateRoot>(root));
        }
Esempio n. 2
0
 public void Setup()
 {
     _aggregate1 = AggregateStubs.Create(new ChangedAggregateRootEntityStub());
     _aggregate2 = AggregateStubs.Create(new ChangedAggregateRootEntityStub());
     _sut        = new ConcurrentUnitOfWork();
     _sut.Attach(_aggregate1);
     _sut.Attach(_aggregate2);
 }
        /// <summary>
        ///     Attempts to get the aggregate root entity associated with the aggregate identifier.
        /// </summary>
        /// <param name="identifier">The aggregate identifier.</param>
        /// <returns>The found <typeparamref name="TAggregateRoot" />, or empty if not found.</returns>
        public async Task <Optional <TAggregateRoot> > GetOptionalAsync(string identifier)
        {
            if (_unitOfWork.TryGet(identifier, out Aggregate aggregate))
            {
                return(new Optional <TAggregateRoot>((TAggregateRoot)aggregate.Root));
            }

            Optional <Snapshot> snapshot = await _reader.ReadOptionalAsync(identifier);

            var version = StreamPosition.Start;

            if (snapshot.HasValue)
            {
                version = snapshot.Value.Version + 1;
            }

            UserCredentials   streamUserCredentials = _configuration.StreamUserCredentialsResolver.Resolve(identifier);
            string            streamName            = _configuration.StreamNameResolver.Resolve(identifier);
            StreamEventsSlice slice =
                await
                _connection.ReadStreamEventsForwardAsync(streamName, version, _configuration.SliceSize, false,
                                                         streamUserCredentials);

            if (slice.Status == SliceReadStatus.StreamDeleted || slice.Status == SliceReadStatus.StreamNotFound)
            {
                return(Optional <TAggregateRoot> .Empty);
            }

            TAggregateRoot root = _rootFactory();

            if (snapshot.HasValue)
            {
                root.RestoreSnapshot(snapshot.Value.State);
            }

            root.Initialize(slice.Events.Select(resolved => _configuration.Deserializer.Deserialize(resolved)));
            while (!slice.IsEndOfStream)
            {
                slice =
                    await
                    _connection.ReadStreamEventsForwardAsync(streamName, slice.NextEventNumber, _configuration.SliceSize,
                                                             false, streamUserCredentials);

                root.Initialize(slice.Events.Select(resolved => _configuration.Deserializer.Deserialize(resolved)));
            }

            aggregate = new Aggregate(identifier, (int)slice.LastEventNumber, root);
            _unitOfWork.Attach(aggregate);
            return(new Optional <TAggregateRoot>(root));
        }
Esempio n. 4
0
 public void SetUp()
 {
     EmbeddedEventStore.Connection.DeleteAllStreams();
     _model      = new Model();
     _root       = SnapshotableAggregateRootEntityStub.Factory();
     _unitOfWork = new ConcurrentUnitOfWork();
     _unitOfWork.Attach(new Aggregate(_model.KnownIdentifier, 0, _root));
     _resolver = A.Fake <IStreamNameResolver>();
     _reader   = A.Fake <IAsyncSnapshotReader>();
     A.CallTo(() => _resolver.Resolve(_model.KnownIdentifier)).Returns(_model.KnownIdentifier);
     A.CallTo(() => _resolver.Resolve(_model.UnknownIdentifier)).Returns(_model.UnknownIdentifier);
     A.CallTo(() => _reader.ReadOptionalAsync(_model.KnownIdentifier))
     .Returns(Task.FromResult(new Optional <Snapshot>(new Snapshot(100, new object()))));
     A.CallTo(() => _reader.ReadOptionalAsync(_model.UnknownIdentifier))
     .Returns(Task.FromResult(new Optional <Snapshot>(new Snapshot(100, new object()))));
     _sut = new AsyncSnapshotableRepository <SnapshotableAggregateRootEntityStub>(
         SnapshotableAggregateRootEntityStub.Factory,
         _unitOfWork,
         EmbeddedEventStore.Connection,
         EventReaderConfigurationFactory.CreateWithResolver(_resolver),
         _reader);
 }
Esempio n. 5
0
 public void AttachThrowsWithSameAggregate()
 {
     Assert.Throws <ArgumentException>(() => _sut.Attach(_aggregate));
 }
Esempio n. 6
0
 public void Setup()
 {
     _aggregate = AggregateStubs.Stub1;
     _sut       = new ConcurrentUnitOfWork();
     _sut.Attach(_aggregate);
 }
Esempio n. 7
0
        public void AttachAggregateDoesNotThrow()
        {
            var aggregate = AggregateStubs.Stub1;

            Assert.DoesNotThrow(() => _sut.Attach(aggregate));
        }
Esempio n. 8
0
 public void AttachNullThrows()
 {
     Assert.Throws <ArgumentNullException>(() => _sut.Attach(null));
 }
 public void SetUp()
 {
     EmbeddedEventStore.Instance.Connection.DeleteAllStreams();
     _model = new Model();
     _root = SnapshotableAggregateRootEntityStub.Factory();
     _unitOfWork = new ConcurrentUnitOfWork();
     _unitOfWork.Attach(new Aggregate(_model.KnownIdentifier, 0, _root));
     _resolver = A.Fake<IStreamNameResolver>();
     _reader = A.Fake<IAsyncSnapshotReader>();
     A.CallTo(() => _resolver.Resolve(_model.KnownIdentifier)).Returns(_model.KnownIdentifier);
     A.CallTo(() => _resolver.Resolve(_model.UnknownIdentifier)).Returns(_model.UnknownIdentifier);
     A.CallTo(() => _reader.ReadOptionalAsync(_model.KnownIdentifier))
      .Returns(Task.FromResult(new Optional<Snapshot>(new Snapshot(100, new object()))));
     A.CallTo(() => _reader.ReadOptionalAsync(_model.UnknownIdentifier))
      .Returns(Task.FromResult(new Optional<Snapshot>(new Snapshot(100, new object()))));
     _sut = new AsyncSnapshotableRepository<SnapshotableAggregateRootEntityStub>(
         SnapshotableAggregateRootEntityStub.Factory,
         _unitOfWork,
         EmbeddedEventStore.Instance.Connection,
         EventReaderConfigurationFactory.CreateWithResolver(_resolver),
         _reader);
 }
 public void Setup()
 {
     _aggregate = AggregateStubs.Stub1;
     _sut = new ConcurrentUnitOfWork();
     _sut.Attach(_aggregate);
 }
 public void Setup()
 {
     _aggregate1 = AggregateStubs.Create(new ChangedAggregateRootEntityStub());
     _aggregate2 = AggregateStubs.Create(new ChangedAggregateRootEntityStub());
     _sut = new ConcurrentUnitOfWork();
     _sut.Attach(_aggregate1);
     _sut.Attach(_aggregate2);
 }