Beispiel #1
0
        private FlowDefinition <TFlowRequest, TFlowResponse> GetFlowDefinitionInternal()
        {
            var flowDefinition = new FlowDefinition <TFlowRequest, TFlowResponse>();

            ConfigureDefinition(flowDefinition);
            return(flowDefinition);
        }
Beispiel #2
0
        private static TFlowResponse BuildFlowResponse(FlowContext flowContext, FlowDefinition <TFlowRequest, TFlowResponse> flowDefinition,
                                                       FlowTrace flowTrace, FlowValues flowValues)
        {
            var flowResponseType = typeof(TFlowResponse);
            var flowResponse     = (TFlowResponse)Activator.CreateInstance(flowResponseType);
            var flowObjectType   = flowResponseType.GetFlowObjectType();

            var missingMandatoryPropertyNames = new List <string>();

            foreach (var flowObjectProperty in flowObjectType.Properties)
            {
                var binding =
                    flowDefinition.Finalizer?.Inputs.Find(b => b.Property.Name == flowObjectProperty.Name)
                    ?? new FlowValueInputBinding(flowObjectProperty)
                {
                    FlowValueSelector = new FlowValueSingleSelector(flowObjectProperty.Name)
                };

                if (binding.TryGetRequestValue(flowValues, flowResponse, out var responseValue))
                {
                    flowObjectProperty.PropertyInfo.SetValue(flowResponse, responseValue);
                }

                CheckMandatoryFlowObjectProperty(flowResponse, flowObjectProperty, missingMandatoryPropertyNames);
            }

            if (missingMandatoryPropertyNames.Count > 0)
            {
                throw new FlowException(
                          $"The following mandatory properties were not populated on {flowResponseType.FullName}: " +
                          $"{string.Join(", ", missingMandatoryPropertyNames.ToArray())}");
            }

            if (flowResponse is FlowResponse baseFlowResponse)
            {
                baseFlowResponse.CorrelationId  = flowContext.CorrelationId;
                baseFlowResponse.RequestId      = flowContext.RequestId;
                baseFlowResponse.FlowInstanceId = flowContext.FlowInstanceId;
                baseFlowResponse.Trace          = flowTrace;
            }

            return(flowResponse);
        }
Beispiel #3
0
        private async Task <int> PerformFlowStep(FlowContext stepFlowContext, FlowDefinition <TFlowRequest, TFlowResponse> flowDefinition, FlowStep flowStep,
                                                 int flowStepIndex, FlowValues flowValues, FlowTrace flowTrace, CancellationToken cancellationToken)
        {
            int nextFlowStepIndex;

            switch (flowStep)
            {
            case ActivityFlowStep activityFlowStep:
                await DoActivity(
                    activityFlowStep, stepFlowContext, flowValues,
                    flowTrace, cancellationToken);

                nextFlowStepIndex = flowStepIndex + 1;
                break;

            case DecisionFlowStepBase decisionFlowStep:
                nextFlowStepIndex =
                    CheckDecision(
                        flowStepIndex, decisionFlowStep, flowDefinition, stepFlowContext, flowValues,
                        flowTrace);
                break;

            case GotoFlowStep gotoFlowStep:
                _logger.LogGoto(stepFlowContext, gotoFlowStep.NextStepName);
                nextFlowStepIndex = flowDefinition.GetStepIndex(gotoFlowStep.NextStepName);
                break;

            case LabelFlowStep _:
                RecordLabelStep(flowTrace, stepFlowContext);
                nextFlowStepIndex = flowStepIndex + 1;
                break;

            case EndFlowStep _:
                nextFlowStepIndex = int.MaxValue;
                break;

            default:
                throw new FlowException(
                          $"Unexpected flow activityFlowStep type {flowStep.GetType().FullName}");
            }

            return(nextFlowStepIndex);
        }
Beispiel #4
0
        private static FlowValues GetInitialFlowValues(FlowDefinition <TFlowRequest, TFlowResponse> flowDefinition,
                                                       TFlowRequest flowRequest)
        {
            var flowValues = new FlowValues();

            var flowRequestType       = typeof(TFlowRequest);
            var flowRequestProperties = flowRequestType.GetFlowObjectType();

            var missingMandatoryPropertyNames = new List <string>();

            foreach (var flowRequestProperty in flowRequestProperties.Properties)
            {
                var binding = flowDefinition.Initializer?.Outputs.Find(b => b.Property.Name == flowRequestProperty.Name);

                var requestValue = flowRequestProperty.PropertyInfo.GetValue(flowRequest);

                if (binding == null)
                {
                    flowValues.SetValue(flowRequestProperty.Name, requestValue);
                }
                else
                {
                    var outputValues = binding.GetOutputValues(requestValue, flowRequest);

                    foreach (var outputValueName in outputValues.Keys)
                    {
                        flowValues.SetValue(outputValueName, outputValues[outputValueName]);
                    }
                }

                CheckMandatoryFlowObjectProperty(flowRequest, flowRequestProperty, missingMandatoryPropertyNames);
            }

            if (missingMandatoryPropertyNames.Count > 0)
            {
                throw new FlowException(
                          $"The following mandatory properties were not populated on {flowRequestType.FullName}: " +
                          $"{string.Join(", ", missingMandatoryPropertyNames.ToArray())}");
            }

            return(flowValues);
        }
Beispiel #5
0
        private int CheckDecision(int flowStepIndex, DecisionFlowStepBase decisionFlowStep, FlowDefinition <TFlowRequest, TFlowResponse> flowDefinition,
                                  FlowContext stepFlowContext, FlowValues flowValues, FlowTrace flowTrace)
        {
            var decisionRequest = (FlowDecisionBase)CreateRequest(stepFlowContext, decisionFlowStep, flowValues);

            // TODO: Pass the targets directly into GetMatchingBranchIndex
            decisionFlowStep.Branches.ForEach(b => decisionRequest.AddBranch(b.Targets));

            _logger?.LogDecisionRequest(stepFlowContext, decisionRequest);

            var branchIndex = decisionRequest.GetMatchingBranchIndex();

            if (branchIndex < 0 || branchIndex >= decisionFlowStep.Branches.Count)
            {
                throw new FlowException($"The branch index returned was out of bounds of the branch array: {branchIndex}");
            }

            var branch = decisionFlowStep.Branches[branchIndex];

            flowTrace.AddStep(new FlowTraceStep
            {
                StepType = FlowTraceStepType.Decision, Name = decisionFlowStep.Name, BranchTargets = branch.Targets
            });

            _logger?.LogDecisionResponse(stepFlowContext, branch);

            if (branch.IsEnd)
            {
                return(int.MaxValue);
            }

            if (branch.IsUnhandled)
            {
                throw new FlowUnhandledElseException($"Unhandled ELSE for decision '{decisionFlowStep.Name}'");
            }

            var isContinue = (branch.NextStepName == null);

            if (isContinue)
            {
                return(flowStepIndex + 1);
            }

            var nextFlowStepIndex = flowDefinition.GetStepIndex(branch.NextStepName);

            return(nextFlowStepIndex);
        }
Beispiel #6
0
 protected virtual void ConfigureDefinition(FlowDefinition <TFlowRequest, TFlowResponse> flowDefinition)
 {
 }
Beispiel #7
0
 public DecisionFlowStep(FlowDefinition <TFlowRequest, TFlowResponse> flowDefinition)
 {
     _flowDefinition = flowDefinition;
 }
Beispiel #8
0
 public DecisionStepElse(IList <Branch> branches, FlowDefinition <TFlowRequest, TFlowResponse> flowDefinition)
 {
     _branches       = branches;
     _flowDefinition = flowDefinition;
 }