Lesson 5: Sub-workflows
An order is fulfilled by two other workflows: PackOrder runs as a child and the parent waits for its result; SendInvoice starts as an independent instance and the parent completes at once.
Waiting for a child
// Inline: the parent waits until PackOrder completes; its result is under "result.packing".
.SubWorkflow(
"Pack order",
new WorkflowReference("PackOrder"),
WorkflowExpr.Obj(
WorkflowExpr.Prop("orderId", WorkflowExpr.Path("state.orderId")),
WorkflowExpr.Prop("warehouse", WorkflowExpr.Path("state.warehouse"))),
resultKey: "packing")
.Set("parcels", WorkflowExpr.Path("result.packing.parcels"))
Starting a follow-up
// Fire and forget: SendInvoice starts as a new instance and the parent completes.
.ContinueWith(
"Send invoice",
new WorkflowReference("SendInvoice"),
WorkflowExpr.Obj(WorkflowExpr.Prop("orderId", WorkflowExpr.Path("state.orderId"))))
Concepts
SubWorkflow: the parent waits; the child’s result is underresult.<resultKey>.ContinueWith: the new instance has its own lifetime; nothing returns to the parent.new WorkflowReference("PackOrder")names a registered workflow; register the children in the same host.- Each workflow is its own definition and its own Designated Workflow.
The complete code
The lesson’s complete code. It uses the namespace of the tutorials; put the classes in a namespace of yours.
OrderFulfilmentWorkflows.cs:
using StellaOps.Workflow.Abstractions;
using StellaOps.Workflow.Contracts;
namespace Examples.Workflow.Tutorials.SubWorkflows;
public sealed class OrderFulfilmentRequest
{
[WorkflowBusinessId]
public string OrderId { get; set; } = string.Empty;
public string Warehouse { get; set; } = string.Empty;
}
/// <summary>
/// The parent: packs an order through the PackOrder child workflow and waits for it, then starts the
/// SendInvoice workflow as an independent instance and completes.
/// </summary>
public sealed class OrderFulfilmentWorkflow : IDeclarativeWorkflow<OrderFulfilmentRequest>
{
public string WorkflowName => "OrderFulfilment";
public string WorkflowVersion => "1.0.0";
public string DisplayName => "Order fulfilment";
public IReadOnlyCollection<string> WorkflowRoles { get; } = ["warehouse"];
public WorkflowSpec<OrderFulfilmentRequest> Spec { get; } = WorkflowSpec.For<OrderFulfilmentRequest>()
.InitializeState(
WorkflowExpr.Obj(
WorkflowExpr.Prop("orderId", WorkflowExpr.Path("start.orderId")),
WorkflowExpr.Prop("warehouse", WorkflowExpr.Path("start.warehouse"))))
.StartWith(flow => flow
// Inline: the parent waits until PackOrder completes; its result is under "result.packing".
.SubWorkflow(
"Pack order",
new WorkflowReference("PackOrder"),
WorkflowExpr.Obj(
WorkflowExpr.Prop("orderId", WorkflowExpr.Path("state.orderId")),
WorkflowExpr.Prop("warehouse", WorkflowExpr.Path("state.warehouse"))),
resultKey: "packing")
.Set("parcels", WorkflowExpr.Path("result.packing.parcels"))
// Fire and forget: SendInvoice starts as a new instance and the parent completes.
.ContinueWith(
"Send invoice",
new WorkflowReference("SendInvoice"),
WorkflowExpr.Obj(WorkflowExpr.Prop("orderId", WorkflowExpr.Path("state.orderId"))))
.Complete())
.Build();
public IReadOnlyCollection<WorkflowTaskDescriptor> Tasks => Spec.TaskDescriptors;
}
public sealed class PackOrderRequest
{
public string OrderId { get; set; } = string.Empty;
public string Warehouse { get; set; } = string.Empty;
}
/// <summary>The child: a packer packs the order and reports the number of parcels.</summary>
public sealed class PackOrderWorkflow : IDeclarativeWorkflow<PackOrderRequest>
{
public string WorkflowName => "PackOrder";
public string WorkflowVersion => "1.0.0";
public string DisplayName => "Pack an order";
public IReadOnlyCollection<string> WorkflowRoles { get; } = ["warehouse"];
public WorkflowSpec<PackOrderRequest> Spec { get; } = WorkflowSpec.For<PackOrderRequest>()
.InitializeState(
WorkflowExpr.Obj(
WorkflowExpr.Prop("orderId", WorkflowExpr.Path("start.orderId")),
WorkflowExpr.Prop("warehouse", WorkflowExpr.Path("start.warehouse"))))
.AddTask(
WorkflowHumanTask.For<PackOrderRequest>("Pack parcels", "PackParcels", "warehouse/pack")
.WithPayload(WorkflowExpr.Obj(WorkflowExpr.Prop("orderId", WorkflowExpr.Path("state.orderId"))))
.OnComplete(flow => flow
.Set("parcels", WorkflowExpr.Path("payload.parcels"))
.Complete()))
.StartWith(flow => flow.ActivateTask("Pack parcels"))
.Build();
public IReadOnlyCollection<WorkflowTaskDescriptor> Tasks => Spec.TaskDescriptors;
}
public sealed class SendInvoiceRequest
{
public string OrderId { get; set; } = string.Empty;
}
/// <summary>The independent follow-up: one call to the billing service.</summary>
public sealed class SendInvoiceWorkflow : IDeclarativeWorkflow<SendInvoiceRequest>
{
public string WorkflowName => "SendInvoice";
public string WorkflowVersion => "1.0.0";
public string DisplayName => "Send an invoice";
public IReadOnlyCollection<string> WorkflowRoles { get; } = ["billing"];
public WorkflowSpec<SendInvoiceRequest> Spec { get; } = WorkflowSpec.For<SendInvoiceRequest>()
.InitializeState(WorkflowExpr.Obj(WorkflowExpr.Prop("orderId", WorkflowExpr.Path("start.orderId"))))
.StartWith(flow => flow
.Call(
"Create invoice",
new EndpointAddress("billing", "/invoices"),
WorkflowExpr.Obj(WorkflowExpr.Prop("orderId", WorkflowExpr.Path("state.orderId"))))
.Complete())
.Build();
public IReadOnlyCollection<WorkflowTaskDescriptor> Tasks => Spec.TaskDescriptors;
}
The canonical form
The C# compiles to a canonical JSON definition, the document the engine validates, stores and renders. Write it to a file with the export helper of lesson 9:
CanonicalExport.WriteCanonicalJson(new OrderFulfilmentWorkflow(), "order-fulfilment.definition.json");CanonicalExport.WriteCanonicalJson(new PackOrderWorkflow(), "pack-order.definition.json");CanonicalExport.WriteCanonicalJson(new SendInvoiceWorkflow(), "send-invoice.definition.json");
Next: Lesson 6: Advanced patterns.
