Esempio n. 1
0
        public async Task SaveSnapshotAsync_should_store_current_aggregate_state()
        {
            var aggregateId = Guid.NewGuid();
            var aggregate   = new BankAccount(aggregateId, "Joe Blogs");

            var expected = aggregate.TakeSnapshot();

            await _provider.SaveSnapshotAsync(aggregate.GetType(), expected)
            .ConfigureAwait(false);

            var actual = await _provider.GetSnapshotAsync(aggregate.GetType(), aggregateId)
                         .ConfigureAwait(false);

            actual.Id.Should().Be(expected.Id);
            actual.AggregateId.Should().Be(expected.AggregateId);
            actual.Version.Should().Be(expected.Version);

            actual.As <BankAccountSnapshot>().Name.Should().Be(expected.As <BankAccountSnapshot>().Name);
            actual.As <BankAccountSnapshot>().CurrentBalance.Should().Be(expected.As <BankAccountSnapshot>().CurrentBalance);
            actual.As <BankAccountSnapshot>().Transactions.Should().BeEquivalentTo(expected.As <BankAccountSnapshot>().Transactions);
        }
Esempio n. 2
0
        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);
        }
Esempio n. 3
0
        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);
        }
Esempio n. 4
0
 public Task <Snapshot> GetSnapshotAsync(Type aggregateType, Guid aggregateId, int version)
 {
     return(LogMethodCallAsync(() => _decorated.GetSnapshotAsync(aggregateType, aggregateId, version), new object[] { aggregateType, aggregateId, version }));
 }
Esempio n. 5
0
        private async Task <TSnapshot> GetLatestSnapshotAsync(TAggregateKey aggregateId)
        {
            var snapshot = await _snapshotStorageProvider.GetSnapshotAsync <TSnapshot, TAggregateKey>(aggregateId);

            return(snapshot);
        }