コード例 #1
0
        public async Task SaveAsync(TAggregate aggregate)
        {
            if (aggregate == null)
            {
                throw new ArgumentNullException(nameof(aggregate));
            }

            if (!aggregate.UncommittedEvents.Any())
            {
                return;
            }

            var stream = GetStream(aggregate.Id);
            var result = await stream.Append(aggregate.UncommittedEvents, aggregate.Version);

            aggregate.ClearUncommittedEvents(result.NextExpectedVersion);

            var threshold = _thresholdResolver.ResolveByAggregate <TAggregate>();

            if (result.NextExpectedVersion - aggregate.StoredSnapshotVersion < threshold)
            {
                return;
            }

            var _ = _snapshotStore.SaveSnapshotAsync <TAggregate, TId>(aggregate.GetSnapshot(), stream.Id);
        }
コード例 #2
0
        public async Task <bool> SaveOrderSnapshot(OrderSnapshot snapshot)
        {
            var streamId = $"Order:{snapshot.Id.ToString()}";
            await _snapshotStore
            .SaveSnapshotAsync(streamId, snapshot.Version, snapshot);

            return(true);
        }
コード例 #3
0
 public Task SaveSnapshotAsync <T, TId>(IAggregateSnapshot snapshot, StreamId stream)
     where T : IAggregate <TId>
 {
     try
     {
         return(_store.SaveSnapshotAsync <T, TId>(snapshot, stream));
     }
     catch (Exception e)
     {
         _logger.LogError(e, "Snapshot saving error.");
         throw;
     }
 }
コード例 #4
0
        public static async Task TakeSnapshot(this Aggregate aggregate, ISnapshotStore snapshotStore)
        {
            var snapshot = ((ISnapshotAggregate)aggregate).CreateSnapshot();

            var metadatas = new[]
            {
                new KeyValuePair <string, object>(MetadataKeys.AggregateId, aggregate.Id),
                new KeyValuePair <string, object>(MetadataKeys.AggregateSequenceNumber, aggregate.Sequence),
                new KeyValuePair <string, object>(MetadataKeys.SnapshotId, Guid.NewGuid()),
                new KeyValuePair <string, object>(MetadataKeys.SnapshotClrType, snapshot.GetType().AssemblyQualifiedName),
                new KeyValuePair <string, object>(MetadataKeys.SnapshotName, snapshot.GetType().Name),
            };

            var metadata = new MetadataCollection(metadatas);

            var uncommittedSnapshot = new UncommittedSnapshot(aggregate.Id, aggregate.Sequence, snapshot, metadata);

            await snapshotStore.SaveSnapshotAsync(uncommittedSnapshot).ConfigureAwait(false);
        }
コード例 #5
0
        public Task SaveAsync <T>(T aggregate) where T : Aggregate
        {
            var task = aggregateStore.Save(aggregate);

            #region Snapshot check & save it!

            Console.WriteLine("snapshot check");
            if (aggregate is ISnapshottable <T> snapshottable)
            {
                Console.WriteLine("snapshot check:true");

                if (snapshottable.SnapshotFrequency().Invoke())
                {
                    Console.WriteLine("taking snapshot...");
                    snapshotStore.SaveSnapshotAsync(snapshottable.TakeSnapshot());
                }
            }
            #endregion

            return(task);
        }
コード例 #6
0
ファイル: Repository.cs プロジェクト: RevStackCore/CQRS
        private async Task CommitChanges <T>(T aggregate) where T : AggregateRoot
        {
            var changesToCommit = aggregate.GetUncommittedChanges().ToList();

            foreach (var e in changesToCommit)
            {
                DoPreCommitTasks(e);
            }

            //CommitAsync events to storage provider
            await _eventStore.SaveAsync <T>(aggregate.GetUncommittedChanges());

            //If the Aggregate implements snaphottable
            var snapshottable = aggregate as ISnapshottable;

            if ((snapshottable != null) && (_snapshotStore != null))
            {
                await _snapshotStore.SaveSnapshotAsync(aggregate.GetType(), snapshottable.TakeSnapshot());
            }

            aggregate.MarkChangesAsCommitted();
        }
コード例 #7
0
        public async Task SaveSnapshotAsync(IUncommittedSnapshot uncommittedSnapshot)
        {
            await _store.SaveSnapshotAsync(uncommittedSnapshot).ConfigureAwait(false);

            Verifier.CalledMethods |= EventStoreMethods.SaveSnapshotAsync;
        }