Skip to content

Commit

Permalink
Add fluent methods for input handling in workflow builder
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
sfmskywalker committed Feb 5, 2024
1 parent ef1d736 commit d511b04
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 1 deletion.
55 changes: 54 additions & 1 deletion src/modules/Elsa.Workflows.Core/Builders/WorkflowBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,60 @@ public IWorkflowBuilder WithVariable(Variable variable)
/// <inheritdoc />
public IWorkflowBuilder WithVariables(params Variable[] variables)
{
foreach (var variable in variables) Variables.Add(variable);
foreach (var variable in variables)
Variables.Add(variable);
return this;
}

/// <inheritdoc />
public InputDefinition WithInput<T>(string name, string? description = default)
{
return WithInput(inputDefinition =>
{
inputDefinition.Name = name;
if (description != null)
inputDefinition.Description = description;
});
}

/// <inheritdoc />
public InputDefinition WithInput(string name, Type type, string? description = default)
{
return WithInput(inputDefinition =>
{
inputDefinition.Name = name;
inputDefinition.Type = type;
if (description != null)
inputDefinition.Description = description;
});
}

/// <inheritdoc />
public InputDefinition WithInput(string name, Type type, Action<InputDefinition>? setup = default)
{
return WithInput(inputDefinition =>
{
inputDefinition.Name = name;
inputDefinition.Type = type;
setup?.Invoke(inputDefinition);
});
}

/// <inheritdoc />
public InputDefinition WithInput(Action<InputDefinition> setup)
{
var inputDefinition = new InputDefinition();
setup(inputDefinition);
Inputs.Add(inputDefinition);
return inputDefinition;
}

/// <inheritdoc />
public IWorkflowBuilder WithInput(InputDefinition inputDefinition)
{
Inputs.Add(inputDefinition);
return this;
}

Expand Down
25 changes: 25 additions & 0 deletions src/modules/Elsa.Workflows.Core/Contracts/IWorkflowBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,31 @@ public interface IWorkflowBuilder
/// A fluent method for adding a variable to <see cref="Variables"/>.
/// </summary>
IWorkflowBuilder WithVariables(params Variable[] variables);

/// <summary>
/// A fluent method for adding an input to <see cref="Inputs"/>.
/// </summary>
InputDefinition WithInput<T>(string name, string? description = default);

/// <summary>
/// A fluent method for adding an input to <see cref="Inputs"/>.
/// </summary>
InputDefinition WithInput(string name, Type type, string? description = default);

/// <summary>
/// A fluent method for adding an input to <see cref="Inputs"/>.
/// </summary>
InputDefinition WithInput(string name, Type type, Action<InputDefinition>? setup = default);

/// <summary>
/// A fluent method for adding an input to <see cref="Inputs"/>.
/// </summary>
InputDefinition WithInput(Action<InputDefinition> setup);

/// <summary>
/// A fluent method for adding an input to <see cref="Inputs"/>.
/// </summary>
IWorkflowBuilder WithInput(InputDefinition inputDefinition);

/// <summary>
/// A fluent method for adding a property to <see cref="CustomProperties"/>.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,18 @@ public static IEnumerable<Variable> EnumerateVariablesInScope(this ExpressionExe
}
}

/// <summary>
/// Returns the input value associated with the specified <see cref="InputDefinition"/> in the given <see cref="ExpressionExecutionContext"/>.
/// </summary>
/// <typeparam name="T">The type of the input value.</typeparam>
/// <param name="context">The <see cref="ExpressionExecutionContext"/> containing the input.</param>
/// <param name="inputDefinition">The <see cref="InputDefinition"/> specifying the input to retrieve.</param>
/// <returns>The input value associated with the specified <see cref="InputDefinition"/> in the <see cref="ExpressionExecutionContext"/>.</returns>
public static T? GetInput<T>(this ExpressionExecutionContext context, InputDefinition inputDefinition)
{
return context.GetInput<T>(inputDefinition.Name);
}

/// <summary>
/// Returns the value of the specified input.
/// </summary>
Expand Down

0 comments on commit d511b04

Please sign in to comment.