public async Task <TAggregate> GetByIdAsync(TAggregateKey id) { TAggregate item; var snapshot = default(TSnapshot); var isSnapshottable = typeof(ISnapshottable <TSnapshot, TAggregateKey, TSnapshotKey>).IsAssignableFrom(typeof(TAggregate)); if (isSnapshottable) { snapshot = await GetLatestSnapshotAsync(id); } if (snapshot != null) { item = CreateNewInstance(); if (!(item is ISnapshottable <TSnapshot, TAggregateKey, TSnapshotKey> snapshottableItem)) { throw new Exception($"{nameof(snapshottableItem)} is not of ISnapshottable<{typeof(TSnapshot).Name}, {typeof(TAggregateKey).Name}, {typeof(TSnapshotKey).Name}>"); } item.HydrateFromSnapshot(snapshot); snapshottableItem.ApplySnapshot(snapshot); var events = await _eventStorageProvider.GetEventsAsync <TAggregate, TAggregateKey>(id, snapshot.Version + 1, long.MaxValue); await item.LoadsFromHistoryAsync(events); } else { var events = (await _eventStorageProvider.GetEventsAsync <TAggregate, TAggregateKey>(id, 0, long.MaxValue)).ToList(); if (events.Any()) { item = CreateNewInstance(); await item.LoadsFromHistoryAsync(events); } else { throw new AggregateNotFoundException($"No events for the aggregate with id={id} were found."); } } return(item); }
public async Task GetEventsAsync_should_return_all_events_by_default() { var aggregateId = Guid.NewGuid(); var aggregate = new BankAccount(aggregateId, "Account Name"); aggregate.Deposit(10); aggregate.Deposit(15); aggregate.WithDrawFunds(5); await CommitChangesAsync(aggregate) .ConfigureAwait(false); var actual = await _provider.GetEventsAsync(aggregate.GetType(), aggregateId) .ConfigureAwait(false); actual.Count().Should().Be(4); actual.First().Should().BeOfType <AccountCreatedEvent>(); }
public async Task <TAggregate> GetByIdAsync(TAggregateKey id) { var item = default(TAggregate); var isSnapshottable = typeof(ISnapshottable <TSnapshotKey, TAggregateKey, TSnapshot>).IsAssignableFrom(typeof(TAggregate)); var snapshot = default(TSnapshot); if ((isSnapshottable) && (_snapshotStorageProvider != null)) { snapshot = await _snapshotStorageProvider.GetSnapshotAsync(id); } if (snapshot != null) { item = CreateNewInstance(); var snapshottableItem = (item as ISnapshottable <TSnapshotKey, TAggregateKey, TSnapshot>); if (snapshottableItem == null) { throw new NullReferenceException(nameof(snapshottableItem)); } item.HydrateFromSnapshot(snapshot); snapshottableItem.ApplySnapshot(snapshot); var events = await _eventStorageProvider.GetEventsAsync(id, snapshot.Version + 1, int.MaxValue); await item.LoadsFromHistoryAsync(events); } else { var events = (await _eventStorageProvider.GetEventsAsync(id, 0, int.MaxValue)).ToList(); if (events.Any()) { item = CreateNewInstance(); await item.LoadsFromHistoryAsync(events); } } return(item); }
public async Task <TAggregate> GetByIdAsync <TAggregate>(Guid id) where TAggregate : Aggregate { var item = default(TAggregate); var isSnapshottable = typeof(ISnapshottable).IsAssignableFrom(typeof(TAggregate)); Snapshot snapshot = null; if (isSnapshottable) { snapshot = await _snapshotStorageProvider.GetSnapshotAsync(typeof(TAggregate), id) .ConfigureAwait(false); } if (snapshot != null) { Logger.Debug("Building aggregate from snapshot"); item = ConstructAggregate <TAggregate>(); ((ISnapshottable)item).ApplySnapshot(snapshot); var events = await _eventStorageProvider.GetEventsAsync(typeof(TAggregate), id, snapshot.Version + 1) .ConfigureAwait(false); item.LoadFromHistory(events); } else { var events = (await _eventStorageProvider.GetEventsAsync(typeof(TAggregate), id).ConfigureAwait(false)).ToList(); if (events.Any()) { item = ConstructAggregate <TAggregate>(); item.LoadFromHistory(events); } } return(item); }
public Task <IEnumerable <IEvent> > GetEventsAsync(Type aggregateType, Guid aggregateId, int start = 0, int count = int.MaxValue) { return(LogMethodCallAsync(() => _decorated.GetEventsAsync(aggregateType, aggregateId, start, count), new object[] { aggregateType, aggregateId, start, count })); }
public static Task <IEnumerable <IEvent> > GetEventsAsync(this IEventStorageProvider provider, Type aggregateType, Guid aggregateId) { return(provider.GetEventsAsync(aggregateType, aggregateId, 0, int.MaxValue)); }