Skip to content

Integrate workflow client module

A client module is provided to integrate workflow features within your bounded context.

The rest of this document is devoted to explain how to integrate the workflow client module within your bounded context.

Add reference to the workflows client module

The first thing to do is to add a reference to the workflows client module project, as shown below:

XML
<ProjectReference Include="$(ServicesPath)Workflows/ITsynch.Suite.Workflows.ClientModule/ITsynch.Suite.Workflows.ClientModule.csproj" />

Configure workflows client module

In your application module you have to depend on client module, and provide all the information required by mean of WorkflowsClientModuleOptions, including:

  • which DbContext to use, through SetDbContext method
  • all available actions, specified through AddWorkflowAction<YOUR_ACTION_DESCRIPTOR_TYPE> method
  • finally, define the workflow for a given entity, through AddWorkflow<TEntity>
C#
public override void SetupModule(IModuleBuilder builder)
{
    base.SetupModule(builder);

    // Configure workflows client module
    builder.DependsOn<WorkflowsClientModule, WorkflowsClientModuleOptions>(opts =>
    {
        // Configure DbContext
        opts.SetDbContext<ServiceOrdersDbContext>();

        // Add workflow actions
        opts.AddWorkflowAction<MyWorkflowAction1>();
        opts.AddWorkflowAction<MyWorkflowAction2>();

        // Add workflow templates
        var workflowId = new Guid("5261D191-52EA-45AD-B29F-7433FFBC2222");
        opts.AddWorkflow<MyEntity>(WorkflowInfo.DefaultWorkflowTemplateId, builder =>
        {
            // Configure your workflow here
            // builder.SetInitialState(...);
            // builder.AddState(...);
        });
    });
}

We encourage you to use an extension method on WorkflowsClientModuleOptions to move workflow configuration away from the application composition method, for the sake of readability.

Please consider moving the workflowId to a public constant, this way it can be referenced from deployment modules.

Configure the workflow

The workflow must be defined through the workflow builder, by invoking AddWorkflow<TEntity> method, where TEntity is the entity for which we are defining the workflow.

To know more about configuring the workflow, see here.

Seed definition using deployment tasks

Every entity supporting workflow must be given at least one template. It's important to include the template creation when defining deployment tasks.

Starting the workflow

The workflow must be started, you need to invoke StartWorkflow method to have it started and bound to the lifecycle defined by the workflow template for the entity type.

If you are using consumers, you must inject IWorkflowEngine<TEntity> and call its StartWorkflow method, typically when creating the entity. In the other hand, if you are using sagas, you must invoke StartWorkflow saga's extension method, typically at InitialState.

Once the workflow's been started, the rest of the workflow= lifecycle will be driven by WorkflowTransitionConsumer, until reaching a final state.