예제 #1
0
        async Task TryMountSagaDataOnInvoker(HandlerInvoker sagaInvoker, object body, string label, List <RelevantSagaInfo> loadedSagaData, List <RelevantSagaInfo> newlyCreatedSagaData, ITransactionContext transactionContext)
        {
            var foundExistingSagaData = false;

            var correlationProperties = _sagaHelper.GetCorrelationProperties(body, sagaInvoker.Saga);
            var correlationPropertiesRelevantForMessage = correlationProperties.ForMessage(body).ToArray();

            foreach (var correlationProperty in correlationPropertiesRelevantForMessage)
            {
                var valueFromMessage = correlationProperty.ValueFromMessage(new MessageContext(transactionContext), body);
                var sagaData         = await _sagaStorage.Find(sagaInvoker.Saga.GetSagaDataType(), correlationProperty.PropertyName, valueFromMessage);

                if (sagaData == null)
                {
                    continue;
                }

                sagaInvoker.SetSagaData(sagaData);
                foundExistingSagaData = true;
                loadedSagaData.Add(new RelevantSagaInfo(sagaData, correlationProperties, sagaInvoker.Saga));

                _log.Debug("Found existing saga data with ID {sagaDataId} for message {messageLabel}", sagaData.Id, label);
                break;
            }

            if (!foundExistingSagaData)
            {
                var messageType = body.GetType();
                var canBeInitiatedByThisMessageType = sagaInvoker.CanBeInitiatedBy(messageType);

                if (canBeInitiatedByThisMessageType)
                {
                    var newSagaData = _sagaHelper.CreateNewSagaData(sagaInvoker.Saga);

                    // if there's exacly one correlation property that points to a property on the saga data, we can set it
                    if (correlationPropertiesRelevantForMessage.Length == 1)
                    {
                        TrySetCorrelationPropertyValue(newSagaData, correlationPropertiesRelevantForMessage[0], body, transactionContext);
                    }

                    sagaInvoker.SetSagaData(newSagaData);

                    _log.Debug("Created new saga data with ID {sagaDataId} for message {messageLabel}", newSagaData.Id, label);

                    newlyCreatedSagaData.Add(new RelevantSagaInfo(newSagaData, correlationProperties, sagaInvoker.Saga));
                }
                else
                {
                    _log.Debug("Could not find existing saga data for message {messageLabel}", label);
                    sagaInvoker.SkipInvocation();
                }
            }
        }
예제 #2
0
        async Task TryMountSagaDataOnInvoker(HandlerInvoker sagaInvoker, object body, string label, List <RelevantSagaInfo> loadedSagaData, List <RelevantSagaInfo> newlyCreatedSagaData)
        {
            var foundExistingSagaData = false;

            var correlationProperties = _sagaHelper.GetCorrelationProperties(body, sagaInvoker.Saga);
            var correlationPropertiesRelevantForMessage = correlationProperties.ForMessage(body);

            foreach (var correlationProperty in correlationPropertiesRelevantForMessage)
            {
                var valueFromMessage = correlationProperty.ValueFromMessage(body);
                var sagaData         =
                    await
                    _sagaStorage.Find(sagaInvoker.Saga.GetSagaDataType(), correlationProperty.PropertyName, valueFromMessage);

                if (sagaData == null)
                {
                    continue;
                }

                sagaInvoker.SetSagaData(sagaData);
                foundExistingSagaData = true;
                loadedSagaData.Add(new RelevantSagaInfo(sagaData, correlationProperties, sagaInvoker.Saga));

                _log.Debug("Found existing saga data with ID {0} for message {1}", sagaData.Id, label);
                break;
            }

            if (!foundExistingSagaData)
            {
                var messageType = body.GetType();
                var canBeInitiatedByThisMessageType = sagaInvoker.CanBeInitiatedBy(messageType);

                if (canBeInitiatedByThisMessageType)
                {
                    var newSagaData = _sagaHelper.CreateNewSagaData(sagaInvoker.Saga);
                    sagaInvoker.SetSagaData(newSagaData);
                    _log.Debug("Created new saga data with ID {0} for message {1}", newSagaData.Id, label);
                    newlyCreatedSagaData.Add(new RelevantSagaInfo(newSagaData, correlationProperties, sagaInvoker.Saga));
                }
                else
                {
                    _log.Debug("Could not find existing saga data for message {0}", label);
                    sagaInvoker.SkipInvocation();
                }
            }
        }
예제 #3
0
 public override void SetSagaData(ISagaData sagaData)
 {
     _handlerInvokerImplementation.SetSagaData(sagaData);
 }