Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added optional context parameter for Ouput operations. Fixes #1143 #1144

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/WorkflowCore/Interface/IStepBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,15 @@ public interface IStepBuilder<TData, TStepBody> : IWorkflowModifier<TData, TStep
/// <param name="value"></param>
/// <returns></returns>
IStepBuilder<TData, TStepBody> Output<TOutput>(Expression<Func<TData, TOutput>> dataProperty, Expression<Func<TStepBody, object>> value);
IStepBuilder<TData, TStepBody> Output<TOutput>(Expression<Func<TData, IStepExecutionContext, TOutput>> dataProperty, Expression<Func<TStepBody, object>> value);

/// <summary>
/// Manipulate properties on the data object after the step executes
/// </summary>
/// <param name="action"></param>
/// <returns></returns>
IStepBuilder<TData, TStepBody> Output(Action<TStepBody, TData> action);
IStepBuilder<TData, TStepBody> Output(Action<TStepBody, TData, IStepExecutionContext> action);

IStepBuilder<TData, TStep> End<TStep>(string name) where TStep : IStepBody;

Expand Down
5 changes: 3 additions & 2 deletions src/WorkflowCore/Models/MemberMapParameter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
12 changes: 12 additions & 0 deletions src/WorkflowCore/Services/FluentBuilders/StepBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -163,12 +163,24 @@ public IStepBuilder<TData, TStepBody> Output<TOutput>(Expression<Func<TData, TOu
return this;
}

public IStepBuilder<TData, TStepBody> Output<TOutput>(Expression<Func<TData, IStepExecutionContext, TOutput>> dataProperty, Expression<Func<TStepBody, object>> value)
{
Step.Outputs.Add(new MemberMapParameter(value, dataProperty));
return this;
}

public IStepBuilder<TData, TStepBody> Output(Action<TStepBody, TData> action)
{
Step.Outputs.Add(new ActionParameter<TStepBody, TData>(action));
return this;
}

public IStepBuilder<TData, TStepBody> Output(Action<TStepBody, TData, IStepExecutionContext> action)
{
Step.Outputs.Add(new ActionParameter<TStepBody, TData>(action));
return this;
}

public IStepBuilder<TData, WaitFor> WaitFor(string eventName, Expression<Func<TData, string>> eventKey, Expression<Func<TData, DateTime>> effectiveDate = null, Expression<Func<TData, bool>> cancelCondition = null)
{
var newStep = new WorkflowStep<WaitFor>();
Expand Down
17 changes: 17 additions & 0 deletions test/WorkflowCore.UnitTests/Models/MemberMapParameterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,23 @@ public void should_assign_output()

data.Value1.Should().Be(step.Value1);
}
[Fact]
public void should_assign_output_with_context()
{
Expression<Func<MyData, object>> memberExpr = (x => x.Value2);
Expression<Func<MyStep, StepExecutionContext, object>> 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()
Expand Down