Introduction
Installation
Basic concepts
Getting Started
Entities
Actions
Events
Workflow
Error handling
Advanced usage
Examples
Workflows in XWorkflows provide a structured way to manage the behavior of entities throughout their lifecycles. A workflow defines a logical grouping of related actions and states for a specific entity. To work with workflows in XWorkflows, you'll need to create custom workflow classes and register the relevant actions and states for each entity.
To create a custom workflow, define a class that inherits from WorkflowBase<TWorkflow, TEntity, TState>. This class will serve as the blueprint for managing the workflow behavior of your entity.
public class OrderWorkflow : WorkflowBase<OrderWorkflow, OrderEntity, State>
{
public OrderWorkflow(IEnumerable<IWorkflowOwner<OrderWorkflow>> actions) : base(actions)
{
}
}
XWorkflows use dependency injection to register workflow related classes. In your Program.cs your should invoke method RegisterWorkflows.
In this example, we have defined the OrderWorkflow class, which inherits from WorkflowBase. We have registered the relevant actions (SubmitOrderAction, CancelOrderAction, DeliverOrderAction) and states (CreatedState, SubmittedState, DeliveredState, CanceledState) for the OrderEntity workflow.
XWorkflows use dependency injection to register workflow related classes. In your Program.cs your should invoke method RegisterWorkflows after this all workflow related classes will be registered with DI.
To execute an action within a workflow, create an instance of your custom workflow class, and then call the ExecuteAction() method:
var orderWorkflow = new OrderWorkflow(actions);
var order = new OrderEntity { /* Initialize properties */ };
var submitRequest = new SubmitOrderActionRequest { /* Request data, if needed */ };
var (result, response) = await orderWorkflow.ExecuteAction(order, submitRequest);
By executing an action within a workflow, XWorkflows ensures that the entity transitions through the defined states based on the action's logic and AllowedTransitions defined in the states.
XWorkflows provides robust error handling to ensure that the workflow execution is smooth and reliable. If an action is executed without permission to transition the entity to a desired state or if an action fails during execution, XWorkflows will throw an exception with a meaningful error message to help diagnose and handle the issue gracefully.
In XWorkflows, you can manage multiple workflows for different entities in your application. Simply define 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.