public static IEnumerable <IEventMessage <DomainAggregateEvent> > Upgrade <TSource>(
     this IEventMessage <TSource> oldMessage, params DomainAggregateEvent[] newEvents)
     where TSource : DomainAggregateEvent
 {
     return(newEvents.Select(newEvent =>
                             (IEventMessage <DomainAggregateEvent>)UpgradedEventMessage.Create(oldMessage, newEvent)));
 }
        public static IEnumerable <IEventMessage <DomainAggregateEvent> > Replace <TSource>(
            this IEnumerable <IEventMessage <DomainAggregateEvent> > eventStream,
            Func <IEventMessage <TSource>, IEnumerable <DomainAggregateEvent> > upgradeFunction)
            where TSource : DomainAggregateEvent
        {
            foreach (var message in eventStream)
            {
                if (message.Event is TSource)
                {
                    var newEvents = upgradeFunction(message as IEventMessage <TSource>);
                    foreach (var newEvent in newEvents)
                    {
                        yield return((IEventMessage <DomainAggregateEvent>)UpgradedEventMessage.Create(message, newEvent));
                    }
                }

                yield return(message);
            }
        }
Exemplo n.º 3
0
        public IEnumerable <IEventMessage <DomainAggregateEvent> > UpgradeSequenceNumbers(
            IEnumerable <IEventMessage <DomainAggregateEvent> > eventStream)
        {
            int sequenceNumber = 1;

            foreach (var message in eventStream)
            {
                long messageNumber          = sequenceNumber;
                long?originalSequenceNumber = message.Metadata.GetStreamSequenceNumber();

                UpgradedEventMessage upgradedMessage = message as UpgradedEventMessage;

                if (originalSequenceNumber != messageNumber)
                {
                    upgradedMessage ??= UpgradedEventMessage.Create(message, message.Event);
                    upgradedMessage.MetadataOverrides[BasicEventMetadataNames.StreamSequenceNumber] = () => messageNumber.ToString();
                }

                if (upgradedMessage != null)
                {
                    if (!upgradedMessage.Metadata.ContainsKey(BasicEventMetadataNames.AggregateVersion) &&
                        originalSequenceNumber != null)
                    {
                        upgradedMessage.MetadataOverrides[BasicEventMetadataNames.AggregateVersion] = () => originalSequenceNumber.ToString();
                    }

                    yield return((IEventMessage <DomainAggregateEvent>)upgradedMessage);
                }
                else
                {
                    yield return(message);
                }

                sequenceNumber++;
            }
        }