예제 #1
0
 public void SetUp()
 {
     _model = new Model();
     using (var stream = new MemoryStream())
     {
         using (var writer = new BinaryWriter(stream))
         {
             new EventStub(1).Write(writer);
         }
         EmbeddedEventStore.Connection.AppendToStreamAsync(
             _model.KnownIdentifier,
             ExpectedVersion.NoStream,
             new EventData(
                 Guid.NewGuid(),
                 typeof(EventStub).AssemblyQualifiedName,
                 false,
                 stream.ToArray(),
                 new byte[0])).Wait();
     }
     _root       = SnapshotableAggregateRootEntityStub.Factory();
     _unitOfWork = new UnitOfWork();
     _resolver   = A.Fake <IStreamNameResolver>();
     _reader     = A.Fake <ISnapshotReader>();
     A.CallTo(() => _resolver.Resolve(_model.KnownIdentifier)).Returns(_model.KnownIdentifier);
     A.CallTo(() => _resolver.Resolve(_model.UnknownIdentifier)).Returns(_model.UnknownIdentifier);
     A.CallTo(() => _reader.ReadOptional(_model.KnownIdentifier)).Returns(Optional <Snapshot> .Empty);
     A.CallTo(() => _reader.ReadOptional(_model.UnknownIdentifier)).Returns(Optional <Snapshot> .Empty);
     _sut = new SnapshotableRepository <SnapshotableAggregateRootEntityStub>(
         () => _root,
         _unitOfWork,
         EmbeddedEventStore.Connection,
         EventReaderConfigurationFactory.CreateWithResolver(_resolver),
         _reader);
 }
 public void SetUp()
 {
     EmbeddedEventStore.Connection.DeleteAllStreams();
     _model      = new Model();
     _unitOfWork = new UnitOfWork();
     _resolver   = A.Fake <IStreamNameResolver>();
     _reader     = A.Fake <ISnapshotReader>();
     A.CallTo(() => _resolver.Resolve(_model.KnownIdentifier)).Returns(_model.KnownIdentifier);
     A.CallTo(() => _resolver.Resolve(_model.UnknownIdentifier)).Returns(_model.UnknownIdentifier);
     A.CallTo(() => _reader.ReadOptional(_model.KnownIdentifier)).Returns(Optional <Snapshot> .Empty);
     A.CallTo(() => _reader.ReadOptional(_model.UnknownIdentifier)).Returns(Optional <Snapshot> .Empty);
     _sut = new SnapshotableRepository <SnapshotableAggregateRootEntityStub>(
         SnapshotableAggregateRootEntityStub.Factory,
         _unitOfWork,
         EmbeddedEventStore.Connection,
         EventReaderConfigurationFactory.CreateWithResolver(_resolver),
         _reader);
 }
예제 #3
0
            public async Task SetUp()
            {
                await EmbeddedEventStore.Connection.DeleteAllStreamsAsync();

                _model      = new Model();
                _root       = SnapshotableAggregateRootEntityStub.Factory();
                _unitOfWork = new UnitOfWork();
                _unitOfWork.Attach(new Aggregate(_model.KnownIdentifier, 0, _root));
                _resolver = A.Fake <IStreamNameResolver>();
                _reader   = A.Fake <ISnapshotReader>();
                _state    = new object();
                A.CallTo(() => _resolver.Resolve(_model.KnownIdentifier)).Returns(_model.KnownIdentifier);
                A.CallTo(() => _resolver.Resolve(_model.UnknownIdentifier)).Returns(_model.UnknownIdentifier);
                A.CallTo(() => _reader.ReadOptional(_model.KnownIdentifier))
                .Returns(new Optional <Snapshot>(new Snapshot(100, _state)));
                A.CallTo(() => _reader.ReadOptional(_model.UnknownIdentifier))
                .Returns(new Optional <Snapshot>(new Snapshot(100, _state)));
                _sut = new SnapshotableRepository <SnapshotableAggregateRootEntityStub>(
                    SnapshotableAggregateRootEntityStub.Factory,
                    _unitOfWork,
                    EmbeddedEventStore.Connection,
                    EventReaderConfigurationFactory.CreateWithResolver(_resolver),
                    _reader);
            }
        /// <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 Optional <TAggregateRoot> GetOptional(string identifier)
        {
            if (_unitOfWork.TryGet(identifier, out Aggregate aggregate))
            {
                return(new Optional <TAggregateRoot>((TAggregateRoot)aggregate.Root));
            }
            Optional <Snapshot> snapshot = _reader.ReadOptional(identifier);
            var version = 1;

            if (snapshot.HasValue)
            {
                version = snapshot.Value.Version + 1;
            }
            UserCredentials   streamUserCredentials = _configuration.StreamUserCredentialsResolver.Resolve(identifier);
            string            streamName            = _configuration.StreamNameResolver.Resolve(identifier);
            StreamEventsSlice slice = _connection.
                                      ReadStreamEventsForwardAsync(
                streamName, version, _configuration.SliceSize, false, streamUserCredentials).
                                      Result;

            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 = _connection.
                        ReadStreamEventsForwardAsync(
                    streamName, slice.NextEventNumber, _configuration.SliceSize, false, streamUserCredentials).
                        Result;

                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));
        }
 public void SetUp()
 {
     EmbeddedEventStore.Instance.Connection.DeleteAllStreams();
     _model = new Model();
     _unitOfWork = new UnitOfWork();
     _resolver = A.Fake<IStreamNameResolver>();
     _reader = A.Fake<ISnapshotReader>();
     A.CallTo(() => _resolver.Resolve(_model.KnownIdentifier)).Returns(_model.KnownIdentifier);
     A.CallTo(() => _resolver.Resolve(_model.UnknownIdentifier)).Returns(_model.UnknownIdentifier);
     A.CallTo(() => _reader.ReadOptional(_model.KnownIdentifier)).Returns(Optional<Snapshot>.Empty);
     A.CallTo(() => _reader.ReadOptional(_model.UnknownIdentifier)).Returns(Optional<Snapshot>.Empty);
     _sut = new SnapshotableRepository<SnapshotableAggregateRootEntityStub>(
         SnapshotableAggregateRootEntityStub.Factory,
         _unitOfWork,
         EmbeddedEventStore.Instance.Connection,
         EventReaderConfigurationFactory.CreateWithResolver(_resolver),
         _reader);
 }
 public void SetUp()
 {
     _model = new Model();
     using (var stream = new MemoryStream())
     {
         using (var writer = new BinaryWriter(stream))
         {
             new EventStub(1).Write(writer);
         }
         EmbeddedEventStore.Instance.Connection.AppendToStream(
             _model.KnownIdentifier,
             ExpectedVersion.NoStream,
             new EventData(
                 Guid.NewGuid(),
                 typeof (EventStub).AssemblyQualifiedName,
                 false,
                 stream.ToArray(),
                 new byte[0]));
         EmbeddedEventStore.Instance.Connection.AppendToStream(
             _model.KnownIdentifier,
             ExpectedVersion.Any,
             new EventData(
                 Guid.NewGuid(),
                 typeof (EventStub).AssemblyQualifiedName,
                 false,
                 stream.ToArray(),
                 new byte[0]));
     }
     _root = SnapshotableAggregateRootEntityStub.Factory();
     _state = new object();
     _unitOfWork = new UnitOfWork();
     _resolver = A.Fake<IStreamNameResolver>();
     _reader = A.Fake<ISnapshotReader>();
     A.CallTo(() => _resolver.Resolve(_model.KnownIdentifier)).Returns(_model.KnownIdentifier);
     A.CallTo(() => _resolver.Resolve(_model.UnknownIdentifier)).Returns(_model.UnknownIdentifier);
     A.CallTo(() => _reader.ReadOptional(_model.KnownIdentifier))
      .Returns(new Optional<Snapshot>(new Snapshot(1, _state)));
     A.CallTo(() => _reader.ReadOptional(_model.UnknownIdentifier))
      .Returns(new Optional<Snapshot>(new Snapshot(1, _state)));
     _sut = new SnapshotableRepository<SnapshotableAggregateRootEntityStub>(
         () => _root,
         _unitOfWork,
         EmbeddedEventStore.Instance.Connection,
         EventReaderConfigurationFactory.CreateWithResolver(_resolver),
         _reader);
 }
예제 #7
0
            public void GetReadsSnapshotOfUnknownId()
            {
                Catch.ExceptionOf(() => _sut.Get(_model.UnknownIdentifier));

                A.CallTo(() => _reader.ReadOptional(_model.UnknownIdentifier)).MustHaveHappened();
            }
 public async Task SetUp()
 {
     await EmbeddedEventStore.Connection.DeleteAllStreamsAsync();
     _model = new Model();
     _root = SnapshotableAggregateRootEntityStub.Factory();
     _unitOfWork = new UnitOfWork();
     _unitOfWork.Attach(new Aggregate(_model.KnownIdentifier, 0, _root));
     _resolver = A.Fake<IStreamNameResolver>();
     _reader = A.Fake<ISnapshotReader>();
     _state = new object();
     A.CallTo(() => _resolver.Resolve(_model.KnownIdentifier)).Returns(_model.KnownIdentifier);
     A.CallTo(() => _resolver.Resolve(_model.UnknownIdentifier)).Returns(_model.UnknownIdentifier);
     A.CallTo(() => _reader.ReadOptional(_model.KnownIdentifier))
      .Returns(new Optional<Snapshot>(new Snapshot(100, _state)));
     A.CallTo(() => _reader.ReadOptional(_model.UnknownIdentifier))
      .Returns(new Optional<Snapshot>(new Snapshot(100, _state)));
     _sut = new SnapshotableRepository<SnapshotableAggregateRootEntityStub>(
         SnapshotableAggregateRootEntityStub.Factory,
         _unitOfWork,
         EmbeddedEventStore.Connection,
         EventReaderConfigurationFactory.CreateWithResolver(_resolver),
         _reader);
 }