Example #1
0
        public void TryGetReturnsFalseAndNullAsAggregateForUnknownId()
        {
            var result = _sut.TryGet(Model.UnknownIdentifier, out var aggregate);

            Assert.That(result, Is.False);
            Assert.That(aggregate, Is.Null);
        }
Example #2
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));
        }
Example #3
0
            public void AddAttachesToUnitOfWork()
            {
                var root = SnapshotableAggregateRootEntityStub.Factory();

                _sut.Add(_model.KnownIdentifier, root);

                Aggregate aggregate;
                var       result = _unitOfWork.TryGet(_model.KnownIdentifier, out aggregate);

                Assert.That(result, Is.True);
                Assert.That(aggregate.Identifier, Is.EqualTo(_model.KnownIdentifier));
                Assert.That(aggregate.Root, Is.SameAs(root));
            }
        /// <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));
        }
Example #5
0
        public void TryGetIdentifierCannotBeNull()
        {
            Aggregate aggregate;

            Assert.Throws <ArgumentNullException>(() => _sut.TryGet(null, out aggregate));
        }