Skip to content

Commit 717eb16

Browse files
committed
feat: add IStepExecutionContext to If When ForEach
1 parent fc3c66a commit 717eb16

File tree

3 files changed

+97
-14
lines changed

3 files changed

+97
-14
lines changed

src/WorkflowCore/Interface/IWorkflowModifier.cs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,28 +83,49 @@ IStepBuilder<TData, WaitFor> WaitFor(string eventName,
8383
/// <param name="collection">Resolves a collection for iterate over</param>
8484
/// <returns></returns>
8585
IContainerStepBuilder<TData, Foreach, Foreach> ForEach(Expression<Func<TData, IEnumerable>> collection);
86-
86+
8787
/// <summary>
8888
/// Execute a block of steps, once for each item in a collection in a RunParallel foreach
8989
/// </summary>
9090
/// <param name="collection">Resolves a collection for iterate over</param>
9191
/// <returns></returns>
9292
IContainerStepBuilder<TData, Foreach, Foreach> ForEach(Expression<Func<TData, IEnumerable>> collection, Expression<Func<TData, bool>> runParallel);
9393

94+
/// <summary>
95+
/// Execute a block of steps, once for each item in a collection in a RunParallel foreach
96+
/// </summary>
97+
/// <param name="collection">Resolves a collection for iterate over</param>
98+
/// <returns></returns>
99+
IContainerStepBuilder<TData, Foreach, Foreach> ForEach(Expression<Func<TData, IStepExecutionContext, IEnumerable>> collection, Expression<Func<TData, bool>> runParallel);
100+
94101
/// <summary>
95102
/// Repeat a block of steps until a condition becomes true
96103
/// </summary>
97104
/// <param name="condition">Resolves a condition to break out of the while loop</param>
98105
/// <returns></returns>
99106
IContainerStepBuilder<TData, While, While> While(Expression<Func<TData, bool>> condition);
100107

108+
/// <summary>
109+
/// Repeat a block of steps until a condition becomes true
110+
/// </summary>
111+
/// <param name="condition">Resolves a condition to break out of the while loop</param>
112+
/// <returns></returns>
113+
IContainerStepBuilder<TData, While, While> While(Expression<Func<TData, IStepExecutionContext, bool>> condition);
114+
101115
/// <summary>
102116
/// Execute a block of steps if a condition is true
103117
/// </summary>
104118
/// <param name="condition">Resolves a condition to evaluate</param>
105119
/// <returns></returns>
106120
IContainerStepBuilder<TData, If, If> If(Expression<Func<TData, bool>> condition);
107121

122+
/// <summary>
123+
/// Execute a block of steps if a condition is true
124+
/// </summary>
125+
/// <param name="condition">Resolves a condition to evaluate</param>
126+
/// <returns></returns>
127+
IContainerStepBuilder<TData, If, If> If(Expression<Func<TData, IStepExecutionContext, bool>> condition);
128+
108129
/// <summary>
109130
/// Configure an outcome for this step, then wire it to a sequence
110131
/// </summary>

src/WorkflowCore/Services/FluentBuilders/StepBuilder.cs

Lines changed: 60 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public IStepBuilder<TData, TStep> Then<TStep>(IStepBuilder<TData, TStep> newStep
5959
}
6060

6161
public IStepBuilder<TData, InlineStepBody> Then(Func<IStepExecutionContext, ExecutionResult> body)
62-
{
62+
{
6363
WorkflowStepInline newStep = new WorkflowStepInline();
6464
newStep.Body = body;
6565
WorkflowBuilder.AddStep(newStep);
@@ -100,7 +100,7 @@ public IStepOutcomeBuilder<TData> When(object outcomeValue, string label = null)
100100
var outcomeBuilder = new StepOutcomeBuilder<TData>(WorkflowBuilder, result);
101101
return outcomeBuilder;
102102
}
103-
103+
104104
public IStepBuilder<TData, TStepBody> Branch<TStep>(object outcomeValue, IStepBuilder<TData, TStep> branch) where TStep : IStepBody
105105
{
106106
if (branch.WorkflowBuilder.Steps.Count == 0)
@@ -123,10 +123,10 @@ public IStepBuilder<TData, TStepBody> Branch<TStep>(Expression<Func<TData, objec
123123
if (branch.WorkflowBuilder.Steps.Count == 0)
124124
return this;
125125

126-
WorkflowBuilder.AttachBranch(branch.WorkflowBuilder);
126+
WorkflowBuilder.AttachBranch(branch.WorkflowBuilder);
127127

128128
Step.Outcomes.Add(new ExpressionOutcome<TData>(outcomeExpression)
129-
{
129+
{
130130
NextStep = branch.WorkflowBuilder.Steps[0].Id
131131
});
132132

@@ -155,7 +155,7 @@ public IStepBuilder<TData, TStepBody> Input(Action<TStepBody, TData, IStepExecut
155155
{
156156
Step.Inputs.Add(new ActionParameter<TStepBody, TData>(action));
157157
return this;
158-
}
158+
}
159159

160160
public IStepBuilder<TData, TStepBody> Output<TOutput>(Expression<Func<TData, TOutput>> dataProperty, Expression<Func<TStepBody, object>> value)
161161
{
@@ -206,7 +206,7 @@ public IStepBuilder<TData, WaitFor> WaitFor(string eventName, Expression<Func<TD
206206
Step.Outcomes.Add(new ValueOutcome { NextStep = newStep.Id });
207207
return stepBuilder;
208208
}
209-
209+
210210
public IStepBuilder<TData, TStep> End<TStep>(string name) where TStep : IStepBody
211211
{
212212
var ancestor = IterateParents(Step.Id, name);
@@ -294,12 +294,12 @@ public IStepBuilder<TData, Decide> Decide(Expression<Func<TData, object>> expres
294294
public IContainerStepBuilder<TData, Foreach, Foreach> ForEach(Expression<Func<TData, IEnumerable>> collection)
295295
{
296296
var newStep = new WorkflowStep<Foreach>();
297-
297+
298298
Expression<Func<Foreach, IEnumerable>> inputExpr = (x => x.Collection);
299-
newStep.Inputs.Add(new MemberMapParameter(collection, inputExpr));
299+
newStep.Inputs.Add(new MemberMapParameter(collection, inputExpr));
300300

301301
WorkflowBuilder.AddStep(newStep);
302-
var stepBuilder = new StepBuilder<TData, Foreach>(WorkflowBuilder, newStep);
302+
var stepBuilder = new StepBuilder<TData, Foreach>(WorkflowBuilder, newStep);
303303

304304
Step.Outcomes.Add(new ValueOutcome { NextStep = newStep.Id });
305305

@@ -324,6 +324,23 @@ public IContainerStepBuilder<TData, Foreach, Foreach> ForEach(Expression<Func<TD
324324
return stepBuilder;
325325
}
326326

327+
public IContainerStepBuilder<TData, Foreach, Foreach> ForEach(Expression<Func<TData, IStepExecutionContext, IEnumerable>> collection, Expression<Func<TData, bool>> runParallel)
328+
{
329+
var newStep = new WorkflowStep<Foreach>();
330+
331+
Expression<Func<Foreach, IEnumerable>> inputExpr = (x => x.Collection);
332+
newStep.Inputs.Add(new MemberMapParameter(collection, inputExpr));
333+
334+
Expression<Func<Foreach, bool>> pExpr = (x => x.RunParallel);
335+
newStep.Inputs.Add(new MemberMapParameter(runParallel, pExpr));
336+
337+
WorkflowBuilder.AddStep(newStep);
338+
var stepBuilder = new StepBuilder<TData, Foreach>(WorkflowBuilder, newStep);
339+
340+
Step.Outcomes.Add(new ValueOutcome { NextStep = newStep.Id });
341+
342+
return stepBuilder;
343+
}
327344

328345
public IContainerStepBuilder<TData, While, While> While(Expression<Func<TData, bool>> condition)
329346
{
@@ -340,6 +357,21 @@ public IContainerStepBuilder<TData, While, While> While(Expression<Func<TData, b
340357
return stepBuilder;
341358
}
342359

360+
public IContainerStepBuilder<TData, While, While> While(Expression<Func<TData, IStepExecutionContext, bool>> condition)
361+
{
362+
var newStep = new WorkflowStep<While>();
363+
364+
Expression<Func<While, bool>> inputExpr = (x => x.Condition);
365+
newStep.Inputs.Add(new MemberMapParameter(condition, inputExpr));
366+
367+
WorkflowBuilder.AddStep(newStep);
368+
var stepBuilder = new StepBuilder<TData, While>(WorkflowBuilder, newStep);
369+
370+
Step.Outcomes.Add(new ValueOutcome { NextStep = newStep.Id });
371+
372+
return stepBuilder;
373+
}
374+
343375
public IContainerStepBuilder<TData, If, If> If(Expression<Func<TData, bool>> condition)
344376
{
345377
var newStep = new WorkflowStep<If>();
@@ -354,7 +386,22 @@ public IContainerStepBuilder<TData, If, If> If(Expression<Func<TData, bool>> con
354386

355387
return stepBuilder;
356388
}
357-
389+
390+
public IContainerStepBuilder<TData, If, If> If(Expression<Func<TData, IStepExecutionContext, bool>> condition)
391+
{
392+
var newStep = new WorkflowStep<If>();
393+
394+
Expression<Func<If, bool>> inputExpr = (x => x.Condition);
395+
newStep.Inputs.Add(new MemberMapParameter(condition, inputExpr));
396+
397+
WorkflowBuilder.AddStep(newStep);
398+
var stepBuilder = new StepBuilder<TData, If>(WorkflowBuilder, newStep);
399+
400+
Step.Outcomes.Add(new ValueOutcome { NextStep = newStep.Id });
401+
402+
return stepBuilder;
403+
}
404+
358405
public IContainerStepBuilder<TData, When, OutcomeSwitch> When(Expression<Func<TData, object>> outcomeValue, string label = null)
359406
{
360407
var newStep = new WorkflowStep<When>();
@@ -378,7 +425,7 @@ public IContainerStepBuilder<TData, When, OutcomeSwitch> When(Expression<Func<TD
378425
{
379426
switchBuilder = (this as IStepBuilder<TData, OutcomeSwitch>);
380427
}
381-
428+
382429
WorkflowBuilder.AddStep(newStep);
383430
var stepBuilder = new ReturnStepBuilder<TData, When, OutcomeSwitch>(WorkflowBuilder, newStep, switchBuilder);
384431
switchBuilder.Step.Children.Add(newStep.Id);
@@ -512,10 +559,10 @@ public IStepBuilder<TData, Activity> Activity(string activityName, Expression<Fu
512559
WorkflowBuilder.AddStep(newStep);
513560
var stepBuilder = new StepBuilder<TData, Activity>(WorkflowBuilder, newStep);
514561
stepBuilder.Input((step) => step.ActivityName, (data) => activityName);
515-
562+
516563
if (parameters != null)
517564
stepBuilder.Input((step) => step.Parameters, parameters);
518-
565+
519566
if (effectiveDate != null)
520567
stepBuilder.Input((step) => step.EffectiveDate, effectiveDate);
521568

src/WorkflowCore/Services/FluentBuilders/WorkflowBuilder.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,16 +241,31 @@ public IContainerStepBuilder<TData, Foreach, Foreach> ForEach(Expression<Func<TD
241241
return Start().ForEach(collection, runParallel);
242242
}
243243

244+
public IContainerStepBuilder<TData, Foreach, Foreach> ForEach(Expression<Func<TData, IStepExecutionContext, IEnumerable>> collection, Expression<Func<TData, bool>> runParallel)
245+
{
246+
return Start().ForEach(collection, runParallel);
247+
}
248+
244249
public IContainerStepBuilder<TData, While, While> While(Expression<Func<TData, bool>> condition)
245250
{
246251
return Start().While(condition);
247252
}
248253

254+
public IContainerStepBuilder<TData, While, While> While(Expression<Func<TData, IStepExecutionContext, bool>> condition)
255+
{
256+
return Start().While(condition);
257+
}
258+
249259
public IContainerStepBuilder<TData, If, If> If(Expression<Func<TData, bool>> condition)
250260
{
251261
return Start().If(condition);
252262
}
253263

264+
public IContainerStepBuilder<TData, If, If> If(Expression<Func<TData, IStepExecutionContext, bool>> condition)
265+
{
266+
return Start().If(condition);
267+
}
268+
254269
public IContainerStepBuilder<TData, When, OutcomeSwitch> When(Expression<Func<TData, object>> outcomeValue, string label = null)
255270
{
256271
return ((IWorkflowModifier<TData, InlineStepBody>) Start()).When(outcomeValue, label);

0 commit comments

Comments
 (0)