protected virtual async Task ApplyAsync( EventCentricAggregateRoot aggregate, IEnumerable <DomainEvent> events, IAggregateReconciliationProxy proxy, Reference reference, CancellationToken?cancellationToken = default) { if (events.Any()) { try { aggregate.LoadFromHistory(events); await proxy .SaveAsync(aggregate, cancellationToken : cancellationToken) .ConfigureAwait(false); await OnAggregateReconciledAsync(reference, events, cancellationToken : cancellationToken) .ConfigureAwait(false); } catch (AggregateHistoryInvalidForStateException invalid) { await OnAggregateConflictDetectedAsync( invalid.Aggregate, events, invalid.StartingVersion, invalid.Aggregate.Version, cancellationToken : cancellationToken) .ConfigureAwait(false); } catch (AggregateConflictDetectedException conflict) { await OnAggregateConflictDetectedAsync( reference, events, conflict.Received, conflict.Persisted, cancellationToken : cancellationToken) .ConfigureAwait(false); } } }
public async Task <EventCentricAggregateRoot> CreateAsync( IEnumerable <DomainEvent> events, CancellationToken?cancellationToken = default) { if (!events.SafeAny()) { throw new DomainEventsMissingException(); } Reference aggregate = events.First().Aggregate; EventCentricAggregateRoot instance = await CreateAsync( aggregate, cancellationToken : cancellationToken) .ConfigureAwait(false); instance.LoadFromHistory(events); return(instance); }