public virtual async Task <IReadOnlyCollection <IDomainEvent <TAggregate, TIdentity> > > StoreAsync <TAggregate, TIdentity>( TIdentity id, IReadOnlyCollection <IUncommittedEvent> uncommittedDomainEvents, ISourceId sourceId, CancellationToken cancellationToken) where TAggregate : IAggregateRoot <TIdentity> where TIdentity : IIdentity { if (id == null) { throw new ArgumentNullException(nameof(id)); } if (sourceId.IsNone()) { throw new ArgumentNullException(nameof(sourceId)); } if (uncommittedDomainEvents == null || !uncommittedDomainEvents.Any()) { return(new IDomainEvent <TAggregate, TIdentity>[] {}); } var aggregateType = typeof(TAggregate); _log.Verbose( "Storing {0} events for aggregate '{1}' with ID '{2}'", uncommittedDomainEvents.Count, aggregateType.Name, id); var batchId = Guid.NewGuid().ToString(); var storeMetadata = new[] { new KeyValuePair <string, string>(MetadataKeys.BatchId, batchId), new KeyValuePair <string, string>(MetadataKeys.SourceId, sourceId.Value) }; var serializedEvents = uncommittedDomainEvents .Select(e => { var md = _metadataProviders .SelectMany(p => p.ProvideMetadata <TAggregate, TIdentity>(id, e.AggregateEvent, e.Metadata)) .Concat(e.Metadata) .Concat(storeMetadata); return(_eventJsonSerializer.Serialize(e.AggregateEvent, md)); }) .ToList(); var committedDomainEvents = await _eventPersistence.CommitEventsAsync( id, serializedEvents, cancellationToken) .ConfigureAwait(false); var domainEvents = committedDomainEvents .Select(e => _eventJsonSerializer.Deserialize <TAggregate, TIdentity>(id, e)) .ToList(); return(domainEvents); }
public async Task <IReadOnlyCollection <IDomainEvent> > CommitAsync( IEventJsonSerializer _eventJsonSerializer, ISourceId sourceId, CancellationToken cancellationToken) { if (!_exists) { await Persistence.Save(this, CancellationToken.None); } else { await Persistence.Update(this, CancellationToken.None); } await Persistence.Dispose(cancellationToken); if (_occuredEvents.HasItems()) { var domainEvents = _occuredEvents .Select(e => { return(_eventJsonSerializer.Serialize(e.AggregateEvent, e.Metadata)); }) .Select((e, i) => { var committedDomainEvent = new CommittedDomainEvent { AggregateId = Id.Value, AggregateName = e.Metadata[MetadataKeys.AggregateName], AggregateSequenceNumber = e.AggregateSequenceNumber, Data = e.SerializedData, Metadata = e.SerializedMetadata, GlobalSequenceNumber = i + 1, }; return(committedDomainEvent); }) .Select(e => _eventJsonSerializer.Deserialize <TAggregate, TIdentity>(Id, e)) .ToList(); _occuredEvents.Clear(); return(domainEvents); } else { _occuredEvents.Clear(); return(new IDomainEvent <TAggregate, TIdentity>[] { }); } }
public RabbitMqMessage CreateMessage(IDomainEvent domainEvent) { var serializedEvent = _eventJsonSerializer.Serialize( domainEvent.GetAggregateEvent(), domainEvent.Metadata); var routingKey = new RoutingKey(string.Format( "eventflow.domainevent.{0}.{1}.{2}", domainEvent.Metadata[MetadataKeys.AggregateName].ToSlug(), domainEvent.Metadata.EventName.ToSlug(), domainEvent.Metadata.EventVersion)); var exchange = new Exchange(_rabbitMqConfiguration.Exchange); var rabbitMqMessage = new RabbitMqMessage( serializedEvent.SerializedData, domainEvent.Metadata, exchange, routingKey, new MessageId(domainEvent.Metadata[MetadataKeys.EventId])); _log.Verbose("Create RabbitMQ message {0}", rabbitMqMessage); return(rabbitMqMessage); }