Example #1
0
        public virtual async Task <TAggregate> LoadAggregateAsync <TAggregate>(
            string id,
            CancellationToken cancellationToken)
            where TAggregate : IAggregateRoot
        {
            var aggregateType = typeof(TAggregate);

            Log.Verbose(
                "Loading aggregate '{0}' with ID '{1}'",
                aggregateType.Name,
                id);

            var domainEvents = await LoadEventsAsync(id, cancellationToken).ConfigureAwait(false);

            var aggregate = await AggregateFactory.CreateNewAggregateAsync <TAggregate>(id).ConfigureAwait(false);

            aggregate.ApplyEvents(domainEvents.Select(e => e.GetAggregateEvent()));

            Log.Verbose(
                "Done loading aggregate '{0}' with ID '{1}' after applying {2} events",
                aggregateType.Name,
                id,
                domainEvents.Count);

            return(aggregate);
        }