Esempio n. 1
0
        public virtual CommittedEvent <TAggregate, TIdentity, TAggregateEvent> From <TAggregateEvent>(TAggregateEvent aggregateEvent,
                                                                                                      long version, 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);

            return(committedEvent);
        }
Esempio n. 2
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);
                }
            }
        }