Esempio n. 1
0
        public async Task <IExecutionResult> TakeSnapshotAsync(string stream)
        {
            Optional <TAggregateRoot> root = await _rootRepository.GetAsync(stream);

            root.ThrowsIf(r => !r.HasValue, new AggregateNotFoundException(stream));

            Aggregate aggregate;

            this._changeTracker.TryGet(stream, out aggregate);

            var changes = new EventData(
                Guid.NewGuid(),
                typeof(TSnapshot).TypeQualifiedName(),
                true,
                Encoding.UTF8.GetBytes(this._eventSerializer.Serialize(((ISnapshotable)root.Value).TakeSnapshot())),
                Encoding.UTF8.GetBytes(this._eventSerializer.Serialize(new EventMetadata
            {
                AggregateAssemblyQualifiedName = typeof(TAggregateRoot).AssemblyQualifiedName,
                AggregateType = typeof(TAggregateRoot).Name,
                TimeStamp     = DateTime.Now,
                IsSnapshot    = true,
                Version       = Convert.ToInt32(aggregate.ExpectedVersion)
            })
                                       ));

            var snapShotStreamName = _streamNameProvider.GetSnapshotStreamName(root, stream);

            await _connection.AppendToStreamAsync(snapShotStreamName, ExpectedVersion.Any, changes);

            return(ExecutionResult.Success);
        }
Esempio n. 2
0
        public async Task <Optional <TAggregateRoot> > GetAsync(string identifier)
        {
            Optional <Aggregate> existingAggregate = GetAggregateFromChangeTrackerIfExits(identifier);

            if (existingAggregate.HasValue)
            {
                return(new Optional <TAggregateRoot>((TAggregateRoot)existingAggregate.Value.Root));
            }

            var snapshotStreamName = _streamNameProvider.GetSnapshotStreamName(typeof(TAggregateRoot), identifier);

            Optional <Snapshot> snapshot = await _snapshotStore.GetLastSnapshot(snapshotStreamName);

            var version = StreamPosition.Start;

            if (snapshot.HasValue)
            {
                version = snapshot.Value.Version + 1;
            }

            var aggregate = await _aggregateStore.Load <TAggregateRoot>(identifier, version);

            AttachAggregateToChangeTracker(aggregate);

            return(new Optional <TAggregateRoot>((TAggregateRoot)aggregate.Root));
        }