Skip to content

Commit

Permalink
Refactor task payload extraction and update workflow builder
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
sfmskywalker committed Dec 25, 2023
1 parent 2c0c880 commit 46ba0d9
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -21,8 +22,8 @@ public async Task HandleAsync(RunTaskRequest notification, CancellationToken can
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -29,7 +29,7 @@ protected override void Build(IWorkflowBuilder builder)
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)}"),
Expand Down

0 comments on commit 46ba0d9

Please sign in to comment.