Пример #1
0
        public void CommittedSnapshot_AfterSerialization_IsValidAfterDeserialization()
        {
            var aggregateSequenceNumber = 3;
            var aggregateId             = TestAggregateId.New;
            var aggregateSnapshot       = new TestAggregateSnapshot(Enumerable.Range(0, aggregateSequenceNumber - 1).Select(x => new TestAggregateSnapshot.TestModel(Guid.NewGuid())).ToList());
            var now        = DateTimeOffset.UtcNow;
            var snapshotId = SnapshotId.NewDeterministic(
                GuidFactories.Deterministic.Namespaces.Snapshots,
                $"{aggregateId.Value}-v{aggregateSequenceNumber}");
            var snapshotMetadata = new SnapshotMetadata
            {
                SnapshotId = snapshotId,
                AggregateSequenceNumber = aggregateSequenceNumber,
                AggregateName           = typeof(TestAggregate).GetAggregateName().Value,
                AggregateId             = aggregateId.Value,
            };
            var committedEvent =
                new CommittedSnapshot <TestAggregate, TestAggregateId, TestAggregateSnapshot>(
                    aggregateId,
                    aggregateSnapshot,
                    snapshotMetadata,
                    now,
                    aggregateSequenceNumber);

            committedEvent.SerializeDeserialize().Should().BeEquivalentTo(committedEvent);
        }
Пример #2
0
        private void InitializeSnapshotStore <TAggregateSnapshot>(TIdentity aggregateId, TAggregateSnapshot aggregateSnapshot, Int64 sequenceNumber)
            where TAggregateSnapshot : IAggregateSnapshot <TAggregate, TIdentity>
        {
            var snapshotStore     = Persistence.Instance.Apply(_testKit.Sys).SnapshotStoreFor(null);
            var committedSnapshot = new CommittedSnapshot <TAggregate, TIdentity, TAggregateSnapshot>(aggregateId, aggregateSnapshot, new SnapshotMetadata(), DateTimeOffset.UtcNow, sequenceNumber);

            var metadata = new AkkaSnapshotMetadata(aggregateId.ToString(), sequenceNumber);

            snapshotStore.Tell(new SaveSnapshot(metadata, committedSnapshot), AggregateEventTestProbe.Ref);

            AggregateEventTestProbe.ExpectMsg <SaveSnapshotSuccess>(x =>
                                                                    x.Metadata.SequenceNr == sequenceNumber &&
                                                                    x.Metadata.PersistenceId == aggregateId.ToString());
        }
        protected void ApplyCommittedEvent <TAggregateEvent>(ICommittedEvent <TAggregate, TIdentity, TAggregateEvent> committedEvent)
            where TAggregateEvent : class, IAggregateEvent <TAggregate, TIdentity>
        {
            var applyMethods = GetEventApplyMethods(committedEvent.AggregateEvent);

            applyMethods(committedEvent.AggregateEvent);

            Log.Info("Aggregate of Name={0}, and Id={1}; committed and applied an AggregateEvent of Type={2}.", Name, Id, typeof(TAggregateEvent).PrettyPrint());

            Version++;

            var domainEvent = new DomainEvent <TAggregate, TIdentity, TAggregateEvent>(Id, committedEvent.AggregateEvent, committedEvent.Metadata, committedEvent.Timestamp, Version);

            Publish(domainEvent);
            ReplyIfAvailable();

            if (!SnapshotStrategy.ShouldCreateSnapshot(this))
            {
                return;
            }
            var aggregateSnapshot = CreateSnapshot();

            if (aggregateSnapshot == null)
            {
                return;
            }
            _snapshotDefinitionService.Load(aggregateSnapshot.GetType());
            var snapshotDefinition = _snapshotDefinitionService.GetDefinition(aggregateSnapshot.GetType());
            var snapshotMetadata   = new SnapshotMetadata
            {
                AggregateId             = Id.Value,
                AggregateName           = Name.Value,
                AggregateSequenceNumber = Version,
                SnapshotName            = snapshotDefinition.Name,
                SnapshotVersion         = snapshotDefinition.Version
            };

            var committedSnapshot =
                new CommittedSnapshot <TAggregate, TIdentity, IAggregateSnapshot <TAggregate, TIdentity> >(
                    Id,
                    aggregateSnapshot,
                    snapshotMetadata,
                    committedEvent.Timestamp, Version);

            SaveSnapshot(committedSnapshot);
        }