public async Task <TAggregate> GetAggregateAsync(string aggregateId)
        {
            var domainEvents = await _eventStore.GetDomainEventsAsync(aggregateId);

            var events = domainEvents.OrderBy(a => a.Index).ToList();

            if (!events.Any())
            {
                return(null);
            }

            var aggregate = new TAggregate
            {
                Id = ObjectId.Parse(aggregateId)
            };

            foreach (var @event in events)
            {
                var eventBody = _eventMapper.ConvertToObjectEvent(@event);
                aggregate.ApplyChange(eventBody);
            }

            aggregate.Commit = events.Max(x => x.Commit);
            aggregate.Index  = events.Max(x => x.Index);

            return(aggregate);
        }
Esempio n. 2
0
        public void Mapper_Should_ConvertToObjectEvent()
        {
            var domainEvent = new DomainEvent
            {
                Type    = "AnotherTestEvent",
                Version = 1,
                Json    = @"{""firstname"":""Jones"",""surname"":""Smith"", ""isvalid"": true}"
            };

            var result = (AnotherTestEventV1)_mapper.ConvertToObjectEvent(domainEvent);

            Assert.That(result.FirstName, Is.EqualTo("Jones"));
            Assert.That(result.SurName, Is.EqualTo("Smith"));
            Assert.That(result.IsValid, Is.EqualTo(true));
        }