Skip to content

Commit cdecaa4

Browse files
committed
context in input parameters
1 parent 96b4b82 commit cdecaa4

File tree

5 files changed

+42
-6
lines changed

5 files changed

+42
-6
lines changed

src/WorkflowCore/Interface/IStepBuilder.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,15 @@ public interface IStepBuilder<TData, TStepBody>
6262
/// <returns></returns>
6363
IStepBuilder<TData, TStepBody> Input<TInput>(Expression<Func<TStepBody, TInput>> stepProperty, Expression<Func<TData, TInput>> value);
6464

65+
/// <summary>
66+
/// Map properties on the step to properties on the workflow data object before the step executes
67+
/// </summary>
68+
/// <typeparam name="TInput"></typeparam>
69+
/// <param name="stepProperty"></param>
70+
/// <param name="value"></param>
71+
/// <returns></returns>
72+
IStepBuilder<TData, TStepBody> Input<TInput>(Expression<Func<TStepBody, TInput>> stepProperty, Expression<Func<TData, IStepExecutionContext, TInput>> value);
73+
6574
/// <summary>
6675
/// Map properties on the workflow data object to properties on the step after the step executes
6776
/// </summary>

src/WorkflowCore/Services/StepBuilder.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,15 @@ public IStepBuilder<TData, TStepBody> Input<TInput>(Expression<Func<TStepBody, T
8282
return this;
8383
}
8484

85+
public IStepBuilder<TData, TStepBody> Input<TInput>(Expression<Func<TStepBody, TInput>> stepProperty, Expression<Func<TData, IStepExecutionContext, TInput>> value)
86+
{
87+
var mapping = new DataMapping();
88+
mapping.Source = value;
89+
mapping.Target = stepProperty;
90+
Step.Inputs.Add(mapping);
91+
return this;
92+
}
93+
8594
public IStepBuilder<TData, TStepBody> Output<TOutput>(Expression<Func<TData, TOutput>> dataProperty, Expression<Func<TStepBody, TOutput>> value)
8695
{
8796
var mapping = new DataMapping();

src/WorkflowCore/Services/WorkflowExecutor.cs

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,7 @@ public WorkflowExecutorResult Execute(WorkflowInstance workflow, WorkflowOptions
7575
});
7676
continue;
7777
}
78-
79-
ProcessInputs(workflow, step, body);
80-
78+
8179
IStepExecutionContext context = new StepExecutionContext()
8280
{
8381
Workflow = workflow,
@@ -87,6 +85,8 @@ public WorkflowExecutorResult Execute(WorkflowInstance workflow, WorkflowOptions
8785
Item = pointer.ContextItem
8886
};
8987

88+
ProcessInputs(workflow, step, body, context);
89+
9090
switch (step.BeforeExecute(wfResult, context, pointer, body))
9191
{
9292
case ExecutionPipelineDirective.Defer:
@@ -198,12 +198,26 @@ private void ProcessExecutionResult(WorkflowInstance workflow, WorkflowDefinitio
198198
}
199199
}
200200

201-
private void ProcessInputs(WorkflowInstance workflow, WorkflowStep step, IStepBody body)
201+
private void ProcessInputs(WorkflowInstance workflow, WorkflowStep step, IStepBody body, IStepExecutionContext context)
202202
{
203+
//TODO: Move to own class
203204
foreach (var input in step.Inputs)
204205
{
205206
var member = (input.Target.Body as MemberExpression);
206-
var resolvedValue = input.Source.Compile().DynamicInvoke(workflow.Data);
207+
object resolvedValue = null;
208+
209+
switch (input.Source.Parameters.Count)
210+
{
211+
case 1:
212+
resolvedValue = input.Source.Compile().DynamicInvoke(workflow.Data);
213+
break;
214+
case 2:
215+
resolvedValue = input.Source.Compile().DynamicInvoke(workflow.Data, context);
216+
break;
217+
default:
218+
throw new ArgumentException();
219+
}
220+
207221
step.BodyType.GetProperty(member.Member.Name).SetValue(body, resolvedValue);
208222
}
209223
}

src/samples/WorkflowCore.Sample09/ForEachWorkflow.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public void Build(IWorkflowBuilder<object> builder)
1818
.ForEach(data => new List<int>() { 1, 2, 3, 4 })
1919
.Do(x => x
2020
.StartWith<DisplayContext>()
21+
.Input(step => step.Item, (data, context) => context.Item)
2122
.Then<DoSomething>())
2223
.Then<SayGoodbye>();
2324
}

src/samples/WorkflowCore.Sample09/Steps/DisplayContext.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,12 @@ namespace WorkflowCore.Sample09
88
{
99
public class DisplayContext : StepBody
1010
{
11+
12+
public object Item { get; set; }
13+
1114
public override ExecutionResult Run(IStepExecutionContext context)
1215
{
13-
Console.WriteLine($"Working on item {context.Item}");
16+
Console.WriteLine($"Working on item {Item}");
1417
return ExecutionResult.Next();
1518
}
1619
}

0 commit comments

Comments
 (0)