Skip to content

Commit d511b04

Browse files
committed
Add fluent methods for input handling in workflow builder
Updated the WorkflowBuilder and its interface, IWorkflowBuilder, to include a series of fluent methods for handling inputs. These methods allow easier set up and addition of input definitions to workflows. An extension method to get input names was also added to the ExpressionExecutionContextExtensions.
1 parent ef1d736 commit d511b04

File tree

3 files changed

+91
-1
lines changed

3 files changed

+91
-1
lines changed

src/modules/Elsa.Workflows.Core/Builders/WorkflowBuilder.cs

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,60 @@ public IWorkflowBuilder WithVariable(Variable variable)
112112
/// <inheritdoc />
113113
public IWorkflowBuilder WithVariables(params Variable[] variables)
114114
{
115-
foreach (var variable in variables) Variables.Add(variable);
115+
foreach (var variable in variables)
116+
Variables.Add(variable);
117+
return this;
118+
}
119+
120+
/// <inheritdoc />
121+
public InputDefinition WithInput<T>(string name, string? description = default)
122+
{
123+
return WithInput(inputDefinition =>
124+
{
125+
inputDefinition.Name = name;
126+
127+
if (description != null)
128+
inputDefinition.Description = description;
129+
});
130+
}
131+
132+
/// <inheritdoc />
133+
public InputDefinition WithInput(string name, Type type, string? description = default)
134+
{
135+
return WithInput(inputDefinition =>
136+
{
137+
inputDefinition.Name = name;
138+
inputDefinition.Type = type;
139+
140+
if (description != null)
141+
inputDefinition.Description = description;
142+
});
143+
}
144+
145+
/// <inheritdoc />
146+
public InputDefinition WithInput(string name, Type type, Action<InputDefinition>? setup = default)
147+
{
148+
return WithInput(inputDefinition =>
149+
{
150+
inputDefinition.Name = name;
151+
inputDefinition.Type = type;
152+
setup?.Invoke(inputDefinition);
153+
});
154+
}
155+
156+
/// <inheritdoc />
157+
public InputDefinition WithInput(Action<InputDefinition> setup)
158+
{
159+
var inputDefinition = new InputDefinition();
160+
setup(inputDefinition);
161+
Inputs.Add(inputDefinition);
162+
return inputDefinition;
163+
}
164+
165+
/// <inheritdoc />
166+
public IWorkflowBuilder WithInput(InputDefinition inputDefinition)
167+
{
168+
Inputs.Add(inputDefinition);
116169
return this;
117170
}
118171

src/modules/Elsa.Workflows.Core/Contracts/IWorkflowBuilder.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,31 @@ public interface IWorkflowBuilder
109109
/// A fluent method for adding a variable to <see cref="Variables"/>.
110110
/// </summary>
111111
IWorkflowBuilder WithVariables(params Variable[] variables);
112+
113+
/// <summary>
114+
/// A fluent method for adding an input to <see cref="Inputs"/>.
115+
/// </summary>
116+
InputDefinition WithInput<T>(string name, string? description = default);
117+
118+
/// <summary>
119+
/// A fluent method for adding an input to <see cref="Inputs"/>.
120+
/// </summary>
121+
InputDefinition WithInput(string name, Type type, string? description = default);
122+
123+
/// <summary>
124+
/// A fluent method for adding an input to <see cref="Inputs"/>.
125+
/// </summary>
126+
InputDefinition WithInput(string name, Type type, Action<InputDefinition>? setup = default);
127+
128+
/// <summary>
129+
/// A fluent method for adding an input to <see cref="Inputs"/>.
130+
/// </summary>
131+
InputDefinition WithInput(Action<InputDefinition> setup);
132+
133+
/// <summary>
134+
/// A fluent method for adding an input to <see cref="Inputs"/>.
135+
/// </summary>
136+
IWorkflowBuilder WithInput(InputDefinition inputDefinition);
112137

113138
/// <summary>
114139
/// A fluent method for adding a property to <see cref="CustomProperties"/>.

src/modules/Elsa.Workflows.Core/Extensions/ExpressionExecutionContextExtensions.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,18 @@ public static IEnumerable<Variable> EnumerateVariablesInScope(this ExpressionExe
338338
}
339339
}
340340

341+
/// <summary>
342+
/// Returns the input value associated with the specified <see cref="InputDefinition"/> in the given <see cref="ExpressionExecutionContext"/>.
343+
/// </summary>
344+
/// <typeparam name="T">The type of the input value.</typeparam>
345+
/// <param name="context">The <see cref="ExpressionExecutionContext"/> containing the input.</param>
346+
/// <param name="inputDefinition">The <see cref="InputDefinition"/> specifying the input to retrieve.</param>
347+
/// <returns>The input value associated with the specified <see cref="InputDefinition"/> in the <see cref="ExpressionExecutionContext"/>.</returns>
348+
public static T? GetInput<T>(this ExpressionExecutionContext context, InputDefinition inputDefinition)
349+
{
350+
return context.GetInput<T>(inputDefinition.Name);
351+
}
352+
341353
/// <summary>
342354
/// Returns the value of the specified input.
343355
/// </summary>

0 commit comments

Comments
 (0)