Welcome to Dejan's Blog

xworkflows

Introduction
Installation
Basic concepts
Getting Started
Entities
Actions
Events
Workflow
Error handling
Advanced usage
Examples

Advanced Usage

In addition to the basic workflow management features, XWorkflows offers several advanced functionalities to cater to complex workflow scenarios and customization requirements.

1. Customizing Action and Event Execution

XWorkflows allows you to customize the execution of actions and events. By overriding methods in the action and event classes, you can implement specific behavior before and after the execution of actions and events. This enables you to integrate with external systems, perform additional validation, or modify the action/event data as needed.

public class CustomSubmitOrderAction : SubmitOrderAction
{
    protected CustomSubmitOrderAction(SubmittedState state, SubmitOrderEvent e) : base(state, e)
    {
    }

    // Override the Execute method to add custom logic before and after action execution
    public override async Task<(WorkflowActionResult<bool>, SubmitOrderActionRequest)> Execute(OrderEntity data, SubmitOrderActionRequest p, CancellationToken cancellationToken)
    {
        // Custom pre-execution logic
        Log("Executing CustomSubmitOrderAction...");

        // Execute the base action's logic
        var result = await base.Execute(data, p, cancellationToken);

        // Custom post-execution logic
        Log("CustomSubmitOrderAction executed successfully!");

        return result;
    }
}

2. Handling Action Parameters and Responses

XWorkflows allows you to pass custom parameters to actions and retrieve responses from actions. This allows you to provide additional data to actions and receive feedback after execution.

// Define custom action parameters and response
public class CustomActionParameters
{
    // Define custom properties here
}

public class CustomActionResponse
{
    // Define custom properties here
}

public class CustomAction : MyWorkflowAction<MyWorkflowEvent, MyWorkflowState, CustomActionParameters, CustomActionParameters, CustomActionResponse>
{
    protected CustomAction(MyWorkflowState state, MyWorkflowEvent e) : base(state, e)
    {
    }

    public override Task<(WorkflowActionResult<CustomActionResponse>, CustomActionParameters)> Execute(MyWorkflowEntity data, CustomActionParameters p, CancellationToken cancellationToken)
    {
        // Implement the action logic here

        // Access custom action parameters
        var customData = p;

        // Return custom action response
        var response = new CustomActionResponse { /* Populate properties */ };
        return Task.FromResult((WorkflowActionResult<CustomActionResponse>.Succeeded(response), p));
    }
}

3. Managing Multiple Workflows

XWorkflows allows you to manage multiple workflows for different entities in your application. Simply create separate custom workflow classes for each entity and register their relevant actions and states. This enables you to keep the workflows separate and organized, ensuring a clear and modular workflow management structure.

4. Integrating with Dependency Injection

XWorkflows use dependency injection to register workflow related classes. In your Program.cs your should invoke method RegisterWorkflows.

5. Implementing Conditional Transitions

XWorkflows provides flexibility in defining conditional transitions between states. You can override the AllowedTransitions method in a state class to define complex logic for allowing or denying state transitions based on specific conditions.

public class ConditionalTransitionState : MyWorkflowState
{
    public override IEnumerable<Type> AllowedTransitions()
    {
        // Implement custom logic for conditional transitions
        if (/* Some condition here */)
        {
            yield return typeof(AllowedAction);
        }
        else
        {
            yield return typeof(DeniedAction);
        }
    }
}
About me
Me
I'm Dejan Đenić, a seasoned software developer with over 20 years of experience, specializing in .NET backend development, NoSQL databases, microservices, CI/CD, and containerization. I have management experience, emphasizing secure API development. Through my blog and LinkedIn, I share insights, fostering a community of developers, and promoting effective team leadership in software development.
Social Plugin