private object FromObject(object aggregateEvent, long version, IMetadata metadata = null)
        {
            if (aggregateEvent is IAggregateEvent)
            {
                _eventDefinitionService.Load(aggregateEvent.GetType());
                var eventDefinition         = _eventDefinitionService.GetDefinition(aggregateEvent.GetType());
                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,
                    SourceId     = PinnedCommand.SourceId,
                    EventId      = eventId,
                    EventName    = eventDefinition.Name,
                    EventVersion = eventDefinition.Version
                };
                eventMetadata.Add(MetadataKeys.TimestampEpoch, now.ToUnixTime().ToString());
                if (metadata != null)
                {
                    eventMetadata.AddRange(metadata);
                }
                var genericType = typeof(CommittedEvent <, ,>)
                                  .MakeGenericType(typeof(TAggregate), typeof(TIdentity), aggregateEvent.GetType());


                var committedEvent = Activator.CreateInstance(
                    genericType,
                    Id,
                    aggregateEvent,
                    eventMetadata,
                    now,
                    aggregateSequenceNumber);

                return(committedEvent);
            }

            throw new InvalidOperationException("could not perform the required mapping for committed event.");
        }
        public SerializedEvent Serialize(IAggregateEvent aggregateEvent, IEnumerable <KeyValuePair <string, string> > metadatas)
        {
            var eventDefinition = _eventDefinitionService.GetDefinition(aggregateEvent.GetType());

            var metadata = new Metadata(metadatas
                                        .Where(kv => kv.Key != MetadataKeys.EventName && kv.Key != MetadataKeys.EventVersion) // TODO: Fix this
                                        .Concat(new[]
            {
                new KeyValuePair <string, string>(MetadataKeys.EventName, eventDefinition.Name),
                new KeyValuePair <string, string>(MetadataKeys.EventVersion, eventDefinition.Version.ToString(CultureInfo.InvariantCulture)),
            }));

            var dataJson = _jsonSerializer.Serialize(aggregateEvent);
            var metaJson = _jsonSerializer.Serialize(metadata);

            return(new SerializedEvent(
                       metaJson,
                       dataJson,
                       metadata.AggregateSequenceNumber,
                       metadata));
        }
        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(aggregateEvent.GetType());
            var eventDefinition         = _eventDefinitionService.GetDefinition(aggregateEvent.GetType());
            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,
                SourceId     = PinnedCommand.SourceId,
                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, aggregateSequenceNumber);

            return(committedEvent);
        }
Exemple #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);
                }
            }
        }