Refactor task payload extraction and update workflow builder

Modified the way task payload is extracted in the OrderFoodTaskHandler.cs for more robustness. The input parameter of the workflow builder in the HungryWorkflow.cs file has also been refactored for efficiency. Making these tweaks streamlines management of values within the tasks.
This commit is contained in:
Sipke Schoorstra 2023-12-25 22:23:23 +01:00
parent 2c0c880a9a
commit 46ba0d9829
2 changed files with 5 additions and 4 deletions

View file

@ -1,6 +1,7 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using Elsa.Extensions;
using Elsa.Mediator.Contracts;
using Elsa.Workflows.Runtime.Contracts;
using Elsa.Workflows.Runtime.Notifications;
@ -21,8 +22,8 @@ public class OrderFoodTaskHandler : INotificationHandler<RunTaskRequest>
if (notification.TaskName != "OrderFood")
return;
var args = (dynamic)notification.TaskPayload!;
var foodName = args.Food;
var args = notification.TaskPayload!;
var foodName = args.GetValue<string>("Food");
Console.WriteLine("Preparing {0}...", foodName);
await Task.Delay(1000, cancellationToken);

View file

@ -14,7 +14,7 @@ public class HungryWorkflow : WorkflowBase
{
protected override void Build(IWorkflowBuilder builder)
{
var deliveredFood = new Variable<string>();
var deliveredFood = builder.WithVariable<string>();
builder.Root = new Sequence
{
@ -29,7 +29,7 @@ public class HungryWorkflow : WorkflowBase
new WriteLine("Hunger detected!"),
new RunTask("OrderFood")
{
Payload = new(new Dictionary<string, object>() { ["Food"] = "Pizza" }),
Payload = new(new Dictionary<string, object> { ["Food"] = "Pizza" }),
Result = new Output<object>(deliveredFood)
},
new WriteLine(context => $"Eating the {deliveredFood.Get(context)}"),