From 28dbd7d96124fe9164e69a63a86fd79c3fac2bed Mon Sep 17 00:00:00 2001 From: Sipke Schoorstra Date: Mon, 25 Sep 2023 10:25:48 +0200 Subject: [PATCH] Improve WorkflowBuilder with common default behavior --- .../Builders/WorkflowBuilder.cs | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/modules/Elsa.Workflows.Core/Builders/WorkflowBuilder.cs b/src/modules/Elsa.Workflows.Core/Builders/WorkflowBuilder.cs index 742897e0b..645ae8aa8 100644 --- a/src/modules/Elsa.Workflows.Core/Builders/WorkflowBuilder.cs +++ b/src/modules/Elsa.Workflows.Core/Builders/WorkflowBuilder.cs @@ -71,6 +71,7 @@ public class WorkflowBuilder : IWorkflowBuilder { var variable = new Variable(); Variables.Add(variable); + variable.WithWorkflowStorage(); return variable; } @@ -81,7 +82,7 @@ public class WorkflowBuilder : IWorkflowBuilder { Name = name, Value = value - }; + }.WithWorkflowStorage(); Variables.Add(variable); return variable; @@ -91,6 +92,7 @@ public class WorkflowBuilder : IWorkflowBuilder public Variable WithVariable(T value) { var variable = value != null ? new Variable(value) : new Variable(); + variable.WithWorkflowStorage(); Variables.Add(variable); return variable; } @@ -159,6 +161,18 @@ public class WorkflowBuilder : IWorkflowBuilder // Assign identities to all activities. _identityGraphService.AssignIdentities(nodes); + // Give unnamed variables in each variable container a predictable name. + var variableContainers = nodes.Where(x => x.Activity is IVariableContainer).Select(x => (IVariableContainer)x.Activity).ToList(); + + foreach (var container in variableContainers) + { + var index = 0; + var unnamedVariables = container.Variables.Where(x => string.IsNullOrWhiteSpace(x.Name)).ToList(); + + foreach (var unnamedVariable in unnamedVariables) + unnamedVariable.Name = $"Variable_{index++}"; + } + return workflow; }