From d511b04ceef428643f16762a9f5181bfe8aa8aef Mon Sep 17 00:00:00 2001 From: Sipke Schoorstra Date: Mon, 5 Feb 2024 21:38:36 +0100 Subject: [PATCH] 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. --- .../Builders/WorkflowBuilder.cs | 55 ++++++++++++++++++- .../Contracts/IWorkflowBuilder.cs | 25 +++++++++ .../ExpressionExecutionContextExtensions.cs | 12 ++++ 3 files changed, 91 insertions(+), 1 deletion(-) diff --git a/src/modules/Elsa.Workflows.Core/Builders/WorkflowBuilder.cs b/src/modules/Elsa.Workflows.Core/Builders/WorkflowBuilder.cs index 7975f3bf9..1be3bf461 100644 --- a/src/modules/Elsa.Workflows.Core/Builders/WorkflowBuilder.cs +++ b/src/modules/Elsa.Workflows.Core/Builders/WorkflowBuilder.cs @@ -112,7 +112,60 @@ public class WorkflowBuilder : IWorkflowBuilder /// public IWorkflowBuilder WithVariables(params Variable[] variables) { - foreach (var variable in variables) Variables.Add(variable); + foreach (var variable in variables) + Variables.Add(variable); + return this; + } + + /// + public InputDefinition WithInput(string name, string? description = default) + { + return WithInput(inputDefinition => + { + inputDefinition.Name = name; + + if (description != null) + inputDefinition.Description = description; + }); + } + + /// + 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; + }); + } + + /// + public InputDefinition WithInput(string name, Type type, Action? setup = default) + { + return WithInput(inputDefinition => + { + inputDefinition.Name = name; + inputDefinition.Type = type; + setup?.Invoke(inputDefinition); + }); + } + + /// + public InputDefinition WithInput(Action setup) + { + var inputDefinition = new InputDefinition(); + setup(inputDefinition); + Inputs.Add(inputDefinition); + return inputDefinition; + } + + /// + public IWorkflowBuilder WithInput(InputDefinition inputDefinition) + { + Inputs.Add(inputDefinition); return this; } diff --git a/src/modules/Elsa.Workflows.Core/Contracts/IWorkflowBuilder.cs b/src/modules/Elsa.Workflows.Core/Contracts/IWorkflowBuilder.cs index 30034e78f..ac8ac58ec 100644 --- a/src/modules/Elsa.Workflows.Core/Contracts/IWorkflowBuilder.cs +++ b/src/modules/Elsa.Workflows.Core/Contracts/IWorkflowBuilder.cs @@ -109,6 +109,31 @@ public interface IWorkflowBuilder /// A fluent method for adding a variable to . /// IWorkflowBuilder WithVariables(params Variable[] variables); + + /// + /// A fluent method for adding an input to . + /// + InputDefinition WithInput(string name, string? description = default); + + /// + /// A fluent method for adding an input to . + /// + InputDefinition WithInput(string name, Type type, string? description = default); + + /// + /// A fluent method for adding an input to . + /// + InputDefinition WithInput(string name, Type type, Action? setup = default); + + /// + /// A fluent method for adding an input to . + /// + InputDefinition WithInput(Action setup); + + /// + /// A fluent method for adding an input to . + /// + IWorkflowBuilder WithInput(InputDefinition inputDefinition); /// /// A fluent method for adding a property to . diff --git a/src/modules/Elsa.Workflows.Core/Extensions/ExpressionExecutionContextExtensions.cs b/src/modules/Elsa.Workflows.Core/Extensions/ExpressionExecutionContextExtensions.cs index 385d5477a..ca56cc449 100644 --- a/src/modules/Elsa.Workflows.Core/Extensions/ExpressionExecutionContextExtensions.cs +++ b/src/modules/Elsa.Workflows.Core/Extensions/ExpressionExecutionContextExtensions.cs @@ -338,6 +338,18 @@ public static class ExpressionExecutionContextExtensions } } + /// + /// Returns the input value associated with the specified in the given . + /// + /// The type of the input value. + /// The containing the input. + /// The specifying the input to retrieve. + /// The input value associated with the specified in the . + public static T? GetInput(this ExpressionExecutionContext context, InputDefinition inputDefinition) + { + return context.GetInput(inputDefinition.Name); + } + /// /// Returns the value of the specified input. ///