diff --git a/src/apps/Elsa.Server.Web/Elsa.Server.Web.csproj b/src/apps/Elsa.Server.Web/Elsa.Server.Web.csproj
index e48fa21f3..3da8ad6f1 100644
--- a/src/apps/Elsa.Server.Web/Elsa.Server.Web.csproj
+++ b/src/apps/Elsa.Server.Web/Elsa.Server.Web.csproj
@@ -77,6 +77,7 @@
+
diff --git a/src/modules/Elsa.Workflows.Core/Abstractions/WorkflowBase.cs b/src/modules/Elsa.Workflows.Core/Abstractions/WorkflowBase.cs
index 303cbcfc6..3e581b44e 100644
--- a/src/modules/Elsa.Workflows.Core/Abstractions/WorkflowBase.cs
+++ b/src/modules/Elsa.Workflows.Core/Abstractions/WorkflowBase.cs
@@ -52,7 +52,7 @@ public abstract class WorkflowBase : WorkflowBase
///
protected WorkflowBase()
{
- Result = new Variable();
+ Result = new("Result", default!);
}
///
diff --git a/src/modules/Elsa.Workflows.Core/Activities/ParallelForEach.cs b/src/modules/Elsa.Workflows.Core/Activities/ParallelForEach.cs
index ea8765948..c8fed303a 100644
--- a/src/modules/Elsa.Workflows.Core/Activities/ParallelForEach.cs
+++ b/src/modules/Elsa.Workflows.Core/Activities/ParallelForEach.cs
@@ -10,7 +10,7 @@ namespace Elsa.Workflows.Activities;
public class ParallelForEach : ParallelForEach
[Port]
- public IActivity Body { get; set; } = default!;
+ public IActivity Body { get; set; } = null!;
///
protected override async ValueTask ExecuteAsync(ActivityExecutionContext context)
diff --git a/src/modules/Elsa.Workflows.Core/Builders/WorkflowBuilder.cs b/src/modules/Elsa.Workflows.Core/Builders/WorkflowBuilder.cs
index c813ada31..fb48a93b8 100644
--- a/src/modules/Elsa.Workflows.Core/Builders/WorkflowBuilder.cs
+++ b/src/modules/Elsa.Workflows.Core/Builders/WorkflowBuilder.cs
@@ -72,9 +72,10 @@ public class WorkflowBuilder(IActivityVisitor activityVisitor, IIdentityGraphSer
}
///
+ [Obsolete("Use the overload that takes a name instead. This overload will be removed in a future version.")]
public Variable WithVariable()
{
- var variable = new Variable();
+ var variable = new Variable(null!, default!);
Variables.Add(variable);
variable.WithWorkflowStorage();
variable.Id = null!; // This ensures that a deterministic ID is assigned by the builder.
@@ -84,13 +85,13 @@ public class WorkflowBuilder(IActivityVisitor activityVisitor, IIdentityGraphSer
///
public Variable WithVariable(string name, T value)
{
- var variable = WithVariable();
- variable.Name = name;
- variable.Value = value;
+ var variable = new Variable(name, value);
+ Variables.Add(variable);
return variable;
}
///
+ [Obsolete("Use the overload that takes a name instead. This overload will be removed in a future version.")]
public Variable WithVariable(T value)
{
var variable = WithVariable();
diff --git a/src/modules/Elsa.Workflows.Core/Memory/Variable.cs b/src/modules/Elsa.Workflows.Core/Memory/Variable.cs
index d3ed28f2f..47a33dd29 100644
--- a/src/modules/Elsa.Workflows.Core/Memory/Variable.cs
+++ b/src/modules/Elsa.Workflows.Core/Memory/Variable.cs
@@ -1,5 +1,7 @@
+using System.Text.Json.Serialization;
using Elsa.Expressions.Helpers;
using Elsa.Expressions.Models;
+using Humanizer;
namespace Elsa.Workflows.Memory;
@@ -11,19 +13,23 @@ public class Variable : MemoryBlockReference
///
public Variable()
{
- Id = Guid.NewGuid().ToString("N");
}
///
- public Variable(string name) : this()
+ public Variable(string name)
{
+ Id = GetIdFromName(name);
Name = name;
}
///
- public Variable(string name, object? value = null) : this()
+ public Variable(string name, object? value = null) : this(name)
+ {
+ Value = value;
+ }
+
+ public Variable(string name, object? value = null, string? id = null) : this(name, value)
{
- Name = name;
Value = value;
}
@@ -45,6 +51,8 @@ public class Variable : MemoryBlockReference
///
public override MemoryBlock Declare() => new(Value, new VariableBlockMetadata(this, StorageDriverType, false));
+
+ private string GetIdFromName(string? name) => $"{name?.Camelize() ?? "Unnamed"}{nameof(Variable)}";
}
///
@@ -59,16 +67,19 @@ public class Variable : Variable
}
///
+ [Obsolete("Use the constructor that takes a name parameter instead.", true)]
public Variable(T value)
{
Value = value;
}
///
- public Variable(string name, T value)
+ public Variable(string name, T value) : base(name, value)
+ {
+ }
+
+ public Variable(string name, T value, string? id = null) : base(name, value, id)
{
- Name = name;
- Value = value;
}
///
@@ -95,6 +106,18 @@ public class Variable : Variable
Id = id;
return this;
}
+
+ public Variable WithName(string name)
+ {
+ Name = name;
+ return this;
+ }
+
+ public Variable WithValue(T value)
+ {
+ Value = value;
+ return this;
+ }
}
///
diff --git a/test/component/Elsa.Workflows.ComponentTests/Scenarios/JavaScriptVariables/JavaScriptVariablesWorkflow1.cs b/test/component/Elsa.Workflows.ComponentTests/Scenarios/JavaScriptVariables/JavaScriptVariablesWorkflow1.cs
index ea7ebdc89..530fa8736 100644
--- a/test/component/Elsa.Workflows.ComponentTests/Scenarios/JavaScriptVariables/JavaScriptVariablesWorkflow1.cs
+++ b/test/component/Elsa.Workflows.ComponentTests/Scenarios/JavaScriptVariables/JavaScriptVariablesWorkflow1.cs
@@ -11,6 +11,6 @@ public class JavaScriptVariablesWorkflow1 : WorkflowBase
{
builder.WithDefinitionId(DefinitionId);
builder.WithVariable("MagicNumber", 3).WithWorkflowStorage();
- builder.Root = new RunJavaScript("setMagicNumber(42)", default, default);
+ builder.Root = new RunJavaScript("setMagicNumber(42)", null, null);
}
}
\ No newline at end of file
diff --git a/test/component/Elsa.Workflows.ComponentTests/Scenarios/JavaScriptVariables/JavaScriptVariablesWorkflow2.cs b/test/component/Elsa.Workflows.ComponentTests/Scenarios/JavaScriptVariables/JavaScriptVariablesWorkflow2.cs
index d6684b401..23a2e00ce 100644
--- a/test/component/Elsa.Workflows.ComponentTests/Scenarios/JavaScriptVariables/JavaScriptVariablesWorkflow2.cs
+++ b/test/component/Elsa.Workflows.ComponentTests/Scenarios/JavaScriptVariables/JavaScriptVariablesWorkflow2.cs
@@ -11,6 +11,6 @@ public class JavaScriptVariablesWorkflow2 : WorkflowBase
{
builder.WithDefinitionId(DefinitionId);
builder.WithVariable("MagicNumber", 3).WithWorkflowStorage();
- builder.Root = new RunJavaScript("variables.MagicNumber = 42", default, default);
+ builder.Root = new RunJavaScript("variables.MagicNumber = 42", null, null);
}
}
\ No newline at end of file
diff --git a/test/component/Elsa.Workflows.ComponentTests/Scenarios/JavaScriptVariables/JavaScriptVariablesWorkflow3.cs b/test/component/Elsa.Workflows.ComponentTests/Scenarios/JavaScriptVariables/JavaScriptVariablesWorkflow3.cs
index 34486085e..b62061c67 100644
--- a/test/component/Elsa.Workflows.ComponentTests/Scenarios/JavaScriptVariables/JavaScriptVariablesWorkflow3.cs
+++ b/test/component/Elsa.Workflows.ComponentTests/Scenarios/JavaScriptVariables/JavaScriptVariablesWorkflow3.cs
@@ -11,6 +11,6 @@ public class JavaScriptVariablesWorkflow3 : WorkflowBase
{
builder.WithDefinitionId(DefinitionId);
builder.WithVariable("MagicNumber", 3).WithWorkflowStorage();
- builder.Root = new RunJavaScript("setVariable('MagicNumber', 42)", default, default);
+ builder.Root = new RunJavaScript("setVariable('MagicNumber', 42)", null, null);
}
}
\ No newline at end of file
diff --git a/test/component/Elsa.Workflows.ComponentTests/Scenarios/JavaScriptVariables/JavaScriptVariablesWorkflowTests.cs b/test/component/Elsa.Workflows.ComponentTests/Scenarios/JavaScriptVariables/JavaScriptVariablesWorkflowTests.cs
index c2876f599..a119f743d 100644
--- a/test/component/Elsa.Workflows.ComponentTests/Scenarios/JavaScriptVariables/JavaScriptVariablesWorkflowTests.cs
+++ b/test/component/Elsa.Workflows.ComponentTests/Scenarios/JavaScriptVariables/JavaScriptVariablesWorkflowTests.cs
@@ -30,7 +30,7 @@ public class JavaScriptVariablesWorkflowTests(App app) : AppComponentTest(app)
var workflowState = workflowInstance!.WorkflowState;
var rootWorkflowActivityExecutionContext = workflowState.ActivityExecutionContexts.Single(x => x.ParentContextId == null);
var variables = GetVariablesDictionary(rootWorkflowActivityExecutionContext);
- var magicNumber = variables["Workflow1:variable-1"].ConvertTo();
+ var magicNumber = variables["magicNumberVariable"].ConvertTo();
Assert.Equal(42, magicNumber);
}
diff --git a/test/component/Elsa.Workflows.ComponentTests/Scenarios/Variables/CountdownWorkflowTests.cs b/test/component/Elsa.Workflows.ComponentTests/Scenarios/Variables/CountdownWorkflowTests.cs
index a09a46863..031242e4f 100644
--- a/test/component/Elsa.Workflows.ComponentTests/Scenarios/Variables/CountdownWorkflowTests.cs
+++ b/test/component/Elsa.Workflows.ComponentTests/Scenarios/Variables/CountdownWorkflowTests.cs
@@ -1,3 +1,4 @@
+using Elsa.Common.Models;
using Elsa.Expressions.Helpers;
using Elsa.Extensions;
using Elsa.Workflows.ComponentTests.Abstractions;
@@ -25,7 +26,7 @@ public class CountdownWorkflowTests(App app) : AppComponentTest(app)
var bookmarkStore = Scope.ServiceProvider.GetRequiredService();
var runAndCreateRequest = new CreateAndRunWorkflowInstanceRequest
{
- WorkflowDefinitionHandle = WorkflowDefinitionHandle.ByDefinitionId(CountdownWorkflow.DefinitionId),
+ WorkflowDefinitionHandle = WorkflowDefinitionHandle.ByDefinitionId(CountdownWorkflow.DefinitionId, VersionOptions.Latest),
};
var runResponse = await workflowClient.CreateAndRunInstanceAsync(runAndCreateRequest);
var workflowInstanceId = runResponse.WorkflowInstanceId;
@@ -42,7 +43,7 @@ public class CountdownWorkflowTests(App app) : AppComponentTest(app)
var workflowState = workflowInstance!.WorkflowState;
var rootWorkflowActivityExecutionContext = workflowState.ActivityExecutionContexts.Single(x => x.ParentContextId == null);
var variables = GetVariablesDictionary(rootWorkflowActivityExecutionContext);
- var actualCounter = variables["Workflow1:variable-1"].ConvertTo();
+ var actualCounter = variables["counterVariable"].ConvertTo();
Assert.Equal(--expectedCounter, actualCounter);
var bookmark = bookmarks.Pop();
diff --git a/test/component/Elsa.Workflows.ComponentTests/Scenarios/Variables/Workflows/CountdownWorkflow.cs b/test/component/Elsa.Workflows.ComponentTests/Scenarios/Variables/Workflows/CountdownWorkflow.cs
index 696ccd5f0..b71a70291 100644
--- a/test/component/Elsa.Workflows.ComponentTests/Scenarios/Variables/Workflows/CountdownWorkflow.cs
+++ b/test/component/Elsa.Workflows.ComponentTests/Scenarios/Variables/Workflows/CountdownWorkflow.cs
@@ -6,29 +6,29 @@ namespace Elsa.Workflows.ComponentTests.Scenarios.Variables.Workflows;
public class CountdownWorkflow : WorkflowBase
{
- public static readonly string DefinitionId = Guid.NewGuid().ToString();
+ public static readonly string DefinitionId = "Guid.NewGuid().ToString()";
protected override void Build(IWorkflowBuilder builder)
{
builder.WithDefinitionId(DefinitionId);
- var counter = builder.WithVariable("Counter", 3).WithWorkflowStorage();
-
- builder.Root = new Sequence
- {
- Activities =
- {
- new While(context => counter.Get(context) > 0)
- {
- Body = new Sequence
- {
- Activities =
- {
- new WriteLine(context => $"Counter: {counter.Get(context)}"),
- new CountdownStep()
- }
- }
- }
- }
- };
+ // var counter = builder.WithVariable("Counter", 3).WithWorkflowStorage();
+ //
+ // builder.Root = new Sequence
+ // {
+ // Activities =
+ // {
+ // new While(context => counter.Get(context) > 0)
+ // {
+ // Body = new Sequence
+ // {
+ // Activities =
+ // {
+ // new WriteLine(context => $"Counter: {counter.Get(context)}"),
+ // new CountdownStep()
+ // }
+ // }
+ // }
+ // }
+ // };
}
}
\ No newline at end of file
diff --git a/test/integration/Elsa.JavaScript.IntegrationTests/JsonElementConverterTests.cs b/test/integration/Elsa.JavaScript.IntegrationTests/JsonElementConverterTests.cs
index b7203e554..c2c57695f 100644
--- a/test/integration/Elsa.JavaScript.IntegrationTests/JsonElementConverterTests.cs
+++ b/test/integration/Elsa.JavaScript.IntegrationTests/JsonElementConverterTests.cs
@@ -17,12 +17,8 @@ public class JsonElementConverterTests(ITestOutputHelper testOutputHelper)
public async Task TestJsonObjectPassedAsJsonElement()
{
var javaScriptEvaluator = _serviceProvider.GetRequiredService();
- var expressionExecutionContext = new ExpressionExecutionContext(_serviceProvider, new MemoryRegister());
- var jsonVariable = new Variable