Пример #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 ComittedSnapshot <TestAggregate, TestAggregateId, TestAggregateSnapshot>(
                    aggregateId,
                    aggregateSnapshot,
                    snapshotMetadata,
                    now,
                    aggregateSequenceNumber);

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

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

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

            AggregateTestProbe.ExpectMsg <SaveSnapshotSuccess>(x =>
                                                               x.Metadata.SequenceNr == sequenceNumber &&
                                                               x.Metadata.PersistenceId == aggregateId.ToString());
        }
Пример #3
0
        protected void  ApplyCommittedEvent <TAggregateEvent>(ICommittedEvent <TAggregate, TIdentity, TAggregateEvent> committedEvent)
            where TAggregateEvent : IAggregateEvent <TAggregate, TIdentity>
        {
            var applyMethods = GetEventApplyMethods(committedEvent.AggregateEvent);

            applyMethods(committedEvent.AggregateEvent);

            Logger.Info($"[{Name}] With Id={Id} Commited and Applied [{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))
            {
                var aggregateSnapshot = CreateSnapshot();
                if (aggregateSnapshot != null)
                {
                    _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 commitedSnapshot =
                        new ComittedSnapshot <TAggregate, TIdentity, IAggregateSnapshot <TAggregate, TIdentity> >(
                            Id,
                            aggregateSnapshot,
                            snapshotMetadata,
                            committedEvent.Timestamp, Version);

                    SaveSnapshot(commitedSnapshot);
                }
            }
        }
Пример #4
0
        public virtual void Emit <TAggregateEvent>(TAggregateEvent aggregateEvent, IMetadata metadata = null)
            where TAggregateEvent : IAggregateEvent <TAggregate, TIdentity>
        {
            if (aggregateEvent == null)
            {
                throw new ArgumentNullException(nameof(aggregateEvent));
            }
            _eventDefinitionService.Load(typeof(TAggregateEvent));
            var eventDefinition         = _eventDefinitionService.GetDefinition(typeof(TAggregateEvent));
            var aggregateSequenceNumber = Version + 1;
            var eventId = EventId.NewDeterministic(
                GuidFactories.Deterministic.Namespaces.Events,
                $"{Id.Value}-v{aggregateSequenceNumber}");
            var now           = DateTimeOffset.UtcNow;
            var eventMetadata = new Metadata
            {
                Timestamp = now,
                AggregateSequenceNumber = aggregateSequenceNumber,
                AggregateName           = Name.Value,
                AggregateId             = Id.Value,
                EventId      = eventId,
                EventName    = eventDefinition.Name,
                EventVersion = eventDefinition.Version
            };

            eventMetadata.Add(MetadataKeys.TimestampEpoch, now.ToUnixTime().ToString());
            if (metadata != null)
            {
                eventMetadata.AddRange(metadata);
            }

            var committedEvent = new CommittedEvent <TAggregate, TIdentity, TAggregateEvent>(Id, aggregateEvent, eventMetadata, now, Version);

            Persist(committedEvent, ApplyCommittedEvents);

            Logger.Info($"[{Name}] With Id={Id} Commited [{typeof(TAggregateEvent).PrettyPrint()}]");

            Version++;

            var domainEvent = new DomainEvent <TAggregate, TIdentity, TAggregateEvent>(Id, aggregateEvent, eventMetadata, now, Version);

            Publish(domainEvent);

            if (SnapshotStrategy.ShouldCreateSnapshot(this))
            {
                var aggregateSnapshot = CreateSnapshot();
                if (aggregateSnapshot != null)
                {
                    var t = aggregateSnapshot.GetType();
                    _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 commitedSnapshot =
                        new ComittedSnapshot <TAggregate, TIdentity, IAggregateSnapshot <TAggregate, TIdentity> >(
                            Id,
                            aggregateSnapshot,
                            snapshotMetadata,
                            now, Version);
                    SaveSnapshot(commitedSnapshot);
                }
            }
        }