From 837860ac019227466980cf54b0e2abbea01a5d06 Mon Sep 17 00:00:00 2001 From: Christian Jundt Date: Fri, 24 Feb 2023 16:11:21 +0100 Subject: [PATCH 1/2] Added optional context parameter for Ouput operations --- src/WorkflowCore/Interface/IStepBuilder.cs | 2 ++ src/WorkflowCore/Models/MemberMapParameter.cs | 5 +++-- .../Services/FluentBuilders/StepBuilder.cs | 12 ++++++++++++ .../Models/MemberMapParameterTests.cs | 17 +++++++++++++++++ 4 files changed, 34 insertions(+), 2 deletions(-) diff --git a/src/WorkflowCore/Interface/IStepBuilder.cs b/src/WorkflowCore/Interface/IStepBuilder.cs index e20d585c5..945c984c5 100644 --- a/src/WorkflowCore/Interface/IStepBuilder.cs +++ b/src/WorkflowCore/Interface/IStepBuilder.cs @@ -89,6 +89,7 @@ public interface IStepBuilder : IWorkflowModifier /// IStepBuilder Output(Expression> dataProperty, Expression> value); + IStepBuilder Output(Expression> dataProperty, Expression> value); /// /// Manipulate properties on the data object after the step executes @@ -96,6 +97,7 @@ public interface IStepBuilder : IWorkflowModifier /// IStepBuilder Output(Action action); + IStepBuilder Output(Action action); IStepBuilder End(string name) where TStep : IStepBody; diff --git a/src/WorkflowCore/Models/MemberMapParameter.cs b/src/WorkflowCore/Models/MemberMapParameter.cs index e5273986d..1b1f902e8 100644 --- a/src/WorkflowCore/Models/MemberMapParameter.cs +++ b/src/WorkflowCore/Models/MemberMapParameter.cs @@ -43,8 +43,9 @@ private void Assign(object sourceObject, LambdaExpression sourceExpr, object tar } var valueExpr = Expression.Convert(Expression.Constant(resolvedValue), targetExpr.ReturnType); - var assign = Expression.Lambda(Expression.Assign(targetExpr.Body, valueExpr), targetExpr.Parameters.Single()); - assign.Compile().DynamicInvoke(targetObject); + var assign = Expression.Lambda(Expression.Assign(targetExpr.Body, valueExpr), targetExpr.Parameters); + if( targetExpr.Parameters.Count == 1 ) assign.Compile( ).DynamicInvoke( targetObject ); + else assign.Compile( ).DynamicInvoke( targetObject, context ); } public void AssignInput(object data, IStepBody body, IStepExecutionContext context) diff --git a/src/WorkflowCore/Services/FluentBuilders/StepBuilder.cs b/src/WorkflowCore/Services/FluentBuilders/StepBuilder.cs index 9cfeae852..9f5042fda 100644 --- a/src/WorkflowCore/Services/FluentBuilders/StepBuilder.cs +++ b/src/WorkflowCore/Services/FluentBuilders/StepBuilder.cs @@ -163,12 +163,24 @@ public IStepBuilder Output(Expression Output(Expression> dataProperty, Expression> value) + { + Step.Outputs.Add(new MemberMapParameter(value, dataProperty)); + return this; + } + public IStepBuilder Output(Action action) { Step.Outputs.Add(new ActionParameter(action)); return this; } + public IStepBuilder Output(Action action) + { + Step.Outputs.Add(new ActionParameter(action)); + return this; + } + public IStepBuilder WaitFor(string eventName, Expression> eventKey, Expression> effectiveDate = null, Expression> cancelCondition = null) { var newStep = new WorkflowStep(); diff --git a/test/WorkflowCore.UnitTests/Models/MemberMapParameterTests.cs b/test/WorkflowCore.UnitTests/Models/MemberMapParameterTests.cs index 81e667139..bffc0893a 100644 --- a/test/WorkflowCore.UnitTests/Models/MemberMapParameterTests.cs +++ b/test/WorkflowCore.UnitTests/Models/MemberMapParameterTests.cs @@ -44,6 +44,23 @@ public void should_assign_output() data.Value1.Should().Be(step.Value1); } + [Fact] + public void should_assign_output_with_context() + { + Expression> memberExpr = (x => x.Value2); + Expression> valueExpr = ((step, context) => ((string[])step.Value2)[(int)context.Item]); + var subject = new MemberMapParameter(valueExpr, memberExpr); + var data = new MyData(); + var step = new MyStep { + Value2 = new []{"A", "B", "C", "D"} + }; + + var context = new StepExecutionContext {Item = 2}; + + subject.AssignOutput(data, step, context); + + data.Value2.Should().Be("C"); + } [Fact] public void should_convert_input() From 16c75098548787ceb299373938751ee69bc6a090 Mon Sep 17 00:00:00 2001 From: Christian Jundt Date: Fri, 24 Feb 2023 18:24:44 +0100 Subject: [PATCH 2/2] fixed code style replaces tabs withs spaces in code indentation --- .../Models/MemberMapParameterTests.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/WorkflowCore.UnitTests/Models/MemberMapParameterTests.cs b/test/WorkflowCore.UnitTests/Models/MemberMapParameterTests.cs index bffc0893a..e11997340 100644 --- a/test/WorkflowCore.UnitTests/Models/MemberMapParameterTests.cs +++ b/test/WorkflowCore.UnitTests/Models/MemberMapParameterTests.cs @@ -51,9 +51,9 @@ public void should_assign_output_with_context() Expression> valueExpr = ((step, context) => ((string[])step.Value2)[(int)context.Item]); var subject = new MemberMapParameter(valueExpr, memberExpr); var data = new MyData(); - var step = new MyStep { - Value2 = new []{"A", "B", "C", "D"} - }; + var step = new MyStep { + Value2 = new []{"A", "B", "C", "D"} + }; var context = new StepExecutionContext {Item = 2};