예제 #1
0
        public async void GetByIdAsync_WithExistingInvestor_ShouldReturnInvestorAggregate()
        {
            var eventToReturn = GetEvents();

            _eventStore.GetFromAsync <Investor>(_aggregateId).ReturnsForAnyArgs(eventToReturn);

            Snapshot snapshot = null;

            _snapshotService.GetLastAsync(Arg.Any <Guid>()).ReturnsForAnyArgs(snapshot);

            _aggregateRootFactory
            .CreateAsync <Investor>(eventToReturn)
            .Returns(_investor);

            var aggregate = await _eventRepository.GetByIdAsync <Investor>(_aggregateId);

            aggregate.Should().Be(_investor);
            aggregate.Should().NotBeNull();
            _aggregateRootFactory.Received().CreateAsync <Investor>(eventToReturn);
        }
예제 #2
0
        public async Task <T> GetByIdAsync <T>(Guid id)
        {
            if (id == Guid.Empty)
            {
                throw new StocqresException("Aggregate Id cannot be empty");
            }

            var snapshot = await _snapshotService.GetLastAsync(id);

            if (snapshot != null)
            {
                return(await RecreateAggregateFromSnapshot <T>(snapshot));
            }

            var events = await _eventStore.GetFromAsync <T>(id);

            if (events == null || !events.Any())
            {
                throw new StocqresException("Aggregate doesn't exist");
            }

            return((T)_factory.CreateAsync <T>(events));
        }