The Chaining pattern allows for the execution of activities in sequence, and in-between, states are saved. In this example we have an Order example, where different steps are executed in sequence: Initialize Order, Validate Order, Process Order and Save Order.
The Order is created as the activities are executed.
[FunctionName($"{nameof(Chaining)}_HttpStart")]publicasyncTask<HttpResponseMessage>HttpStart([HttpTrigger(AuthorizationLevel.Anonymous,"get","post")]HttpRequestMessagereq,[DurableClient]IDurableOrchestrationClientstarter,ILoggerlog){varorder=awaitreq.Content.ReadAsAsync<Order>();varinstanceId=awaitstarter.StartNewAsync(nameof(Orchestrator),order);log.LogInformation($"Started orchestration with ID = '{instanceId}'.");returnstarter.CreateCheckStatusResponse(req,instanceId);}
This is an HTTPTrigger function that receives the Order input, which initially receives a ProductSku and Quantity as available in this CURL request:
The Orchestrator contains the calls to all Order activities. It receives an Order as input and returns the outputs (different Order properties). Check the PlayGoKids repository for details of IOrderService.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[FunctionName(nameof(Orchestrator))]publicasyncTask<List<string>>RunOrchestrator([OrchestrationTrigger]IDurableOrchestrationContextcontext){varinput=context.GetInput<Order>();// sku and quantity
varoutputs=newList<string>{awaitcontext.CallActivityAsync<string>(nameof(IOrderService.InitializeOrder),input),awaitcontext.CallActivityAsync<string>(nameof(IOrderService.ValidateOrder),input),awaitcontext.CallActivityAsync<string>(nameof(IOrderService.ProcessOrder),input),awaitcontext.CallActivityAsync<string>(nameof(IOrderService.SaveOrder),input),};returnoutputs;}
Activity Functions
The activities were created in order Top-down, displayed below:
publicclassOrderService:IOrderService{publicOrderInitializeOrder(Orderorder){order.OrderNumber=$"ON-RANDOM";// generate number
order.OrderStatus=nameof(InitializeOrder);returnorder;}publicOrderValidateOrder(Orderorder){if(order.Quantity==0)// validate quantity
{order.Quantity=1;// at least one item
}order.OrderStatus=nameof(ValidateOrder);returnorder;}publicOrderProcessOrder(Orderorder){order.OrderDate=DateTime.Now;// generate date
order.OrderStatus=nameof(ProcessOrder);returnorder;}publicOrderSaveOrder(Orderorder){order.OrderId=1000;// generate id
order.OrderStatus=nameof(SaveOrder);returnorder;}}
This is an example of how to use the Chaining pattern, applied to the creation of Orders.
In the next article of the series let’s check out how to use the Fan out/fan in pattern.
Join the conversation! Share your thoughts and connect with other readers.