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:
parent
2c0c880a9a
commit
46ba0d9829
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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)}"),
|
||||
|
|
|
|||
Loading…
Reference in a new issue