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);
        }
Exemplo n.º 2
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);
                }
            }
        }
Exemplo n.º 3
0
        public ComittedSnapshot(
            TIdentity aggregateIdentity,
            TAggregateSnapshot aggregateSnapshot,
            SnapshotMetadata metadata,
            DateTimeOffset timestamp,
            long aggregateSequenceNumber)
        {
            if (aggregateSnapshot == null)
            {
                throw new ArgumentNullException(nameof(aggregateSnapshot));
            }
            if (metadata == null)
            {
                throw new ArgumentNullException(nameof(metadata));
            }
            if (timestamp == default(DateTimeOffset))
            {
                throw new ArgumentNullException(nameof(timestamp));
            }
            if (aggregateSnapshot == null)
            {
                throw new ArgumentNullException(nameof(aggregateSnapshot));
            }
            if (aggregateIdentity == null || string.IsNullOrEmpty(aggregateIdentity.Value))
            {
                throw new ArgumentNullException(nameof(aggregateIdentity));
            }


            AggregateIdentity       = aggregateIdentity;
            AggregateSequenceNumber = aggregateSequenceNumber;
            AggregateIdentity       = aggregateIdentity;
            AggregateSnapshot       = aggregateSnapshot;
            Metadata  = metadata;
            Timestamp = timestamp;
        }
Exemplo n.º 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);
                }
            }
        }