Skip to content

Workflow builder

The workflow builder provides features to completely define the workflow in terms of state and behavior for a given entity.

Using the following example we're going to explain how to provide a complete workflow template by using the workflow builder.

C#
workflowsModuleOptions.AddWorkflow<ServiceOrder>(WorkflowInfo.DefaultWorkflowTemplateId, workflowBuilder =>
{
    workflowBuilder.SetInitialState("New", stateBuilder =>
    {
        stateBuilder
            .WithTransition("Assign", "Assigned", transitionBuilder =>
            {
                transitionBuilder.AddAction<ServiceOrderAssignActionDescriptor>(actionBuilder =>
                {
                    actionBuilder.WithArguments<Guid>(ServiceOrderAssignActionDescriptor.AssignedPositionId, binder => binder.FromMessage());
                });
            })
    });

    workflowBuilder.AddState("Assigned", stateBuilder =>
    {
        stateBuilder
            .WithTransition("Complete", "Completed", transitionBuilder =>
            {
                transitionBuilder.AddAction<ServiceOrderCompleteActionDescriptor>(actionBuilder =>
                {
                    actionBuilder.WithArguments<Guid>(ServiceOrderCompleteActionDescriptor.CompletedByUserId, binder => binder.FromMessage());
                });
            });
    });
});

Registering actions

Actions are shared across workflow definitions, hence they must be declared off the workflow template definition. To do so, you must use the workflow module options instance, as follow:

C#
1
2
3
    opts.AddWorkflowAction<YOUR_ACTION_DESCRIPTOR>();
    opts.AddWorkflowAction<YOUR_ACTION_DESCRIPTOR>();
    opts.AddWorkflowAction<YOUR_ACTION_DESCRIPTOR>();

Once registered, these actions can be used in any workflow template.

See here for more information on workflow entities.

Start configuring the workflow

First off, you need to invoke the AddWorkflow method of the WorkflowModuleOptions instance to get access to the workflow builder instance. You need to specify for which entity the workflow is being defined for.

C#
1
2
3
4
5
6
    workflowsModuleOptions.AddWorkflow<ServiceOrder>(
        WorkflowInfo.DefaultWorkflowTemplateId,
        workflowBuilder =>
        {
            // use workflowBuilderInstance to further configure the workflow
        }

See here for more information on workflow entities.

Configuring initial state

Using the workflow builder you must define an initial state, and other non-initial ones, based on your needs.

C#
1
2
3
4
    workflowBuilder.SetInitialState("STATE_NAME", stateBuilder =>
    {
        // configure state using stateBuilder instance
    }

for initial state, and

C#
1
2
3
4
    workflowBuilder.AddState("STATE_NAME", stateBuilder =>
    {
        // configure state using stateBuilder instance
    });

for non-initial states.

See here for more information on workflow states.

Configuring transitions

Through the state builder you can configure transitions to move from the current sate to the target one.

C#
1
2
3
4
    stateBuilder.WithTransition("TRANSITION_NAME", "TARGET_STATE", transitionBuilder =>
    {
        // configure transition using transitionBuilder
    });

See here for more information on workflow transitions.