Welcome to Dejan's Blog

xworkflows

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

Examples

In this section, we'll explore practical examples of using the XWorkflows library to manage workflows for different entities. Each example will demonstrate how to define custom actions, events, states, and workflows to achieve specific workflow behavior.

1. Order Management Workflow

In this example, we'll create a workflow to manage the lifecycle of an order entity with states such as "Created," "Submitted," "Delivered," and "Canceled." We'll define actions for submitting, canceling, and delivering an order, along with events for logging the actions.

stateDiagram [*] --> Created Created --> Submitted Created --> Canceled Submitted --> Delivered Delivered --> [*]
// Define custom actions, events, and states for Order workflow
// ...

// Create the custom OrderWorkflow
var orderWorkflow = new OrderWorkflow(actions);

// Create an instance of the OrderEntity
var order = new OrderEntity { /* Initialize properties */ };

// Execute actions within the workflow
var submitRequest = new SubmitOrderActionRequest { /* Request data, if needed */ };
var (submitResult, submitResponse) = await orderWorkflow.ExecuteAction(order, submitRequest);

var cancelRequest = new CancelOrderActionRequest { /* Request data, if needed */ };
var (cancelResult, cancelResponse) = await orderWorkflow.ExecuteAction(order, cancelRequest);

var deliverRequest = new DeliverOrderActionRequest { /* Request data, if needed */ };
var (deliverResult, deliverResponse) = await orderWorkflow.ExecuteAction(order, deliverRequest);

2. Task Management Workflow

In this example, we'll create a workflow to manage the lifecycle of a task entity with states such as "Open," "In Progress," "Completed," and "Canceled." We'll define actions for updating the task status, marking it as completed, and canceling it.

stateDiagram [*] --> Open Open --> In_Progress Open --> Canceled In_Progress --> Completed In_Progress --> Canceled Completed --> [*]
// Define custom actions, events, and states for Task workflow
// ...

// Create the custom TaskWorkflow
var taskWorkflow = new TaskWorkflow(actions);

// Create an instance of the TaskEntity
var task = new TaskEntity { /* Initialize properties */ };

// Execute actions within the workflow
var updateStatusRequest = new UpdateTaskStatusActionRequest { /* Request data, if needed */ };
var (updateStatusResult, updateStatusResponse) = await taskWorkflow.ExecuteAction(task, updateStatusRequest);

var completeRequest = new CompleteTaskActionRequest { /* Request data, if needed */ };
var (completeResult, completeResponse) = await taskWorkflow.ExecuteAction(task, completeRequest);

var cancelRequest = new CancelTaskActionRequest { /* Request data, if needed */ };
var (cancelResult, cancelResponse) = await taskWorkflow.ExecuteAction(task, cancelRequest);

3. Approval Workflow

In this example, we'll create a workflow to manage the approval process for a document entity with states such as "Draft," "Pending Approval," "Approved," and "Rejected." We'll define actions for submitting the document for approval, approving it, and rejecting it, along with events for notifying users.

stateDiagram [*] --> Draft Draft --> Pending_Approval Pending_Approval --> Approved Approved --> [*]
// Define custom actions, events, and states for Approval workflow
// ...

// Create the custom ApprovalWorkflow
var approvalWorkflow = new ApprovalWorkflow(actions);

// Create an instance of the DocumentEntity
var document = new DocumentEntity { /* Initialize properties */ };

// Execute actions within the workflow
var submitRequest = new SubmitForApprovalActionRequest { /* Request data, if needed */ };
var (submitResult, submitResponse) = await approvalWorkflow.ExecuteAction(document, submitRequest);

var approveRequest = new ApproveDocumentActionRequest { /* Request data, if needed */ };
var (approveResult, approveResponse) = await approvalWorkflow.ExecuteAction(document, approveRequest);

var rejectRequest = new RejectDocumentActionRequest { /* Request data, if needed */ };
var (rejectResult, rejectResponse) = await approvalWorkflow.ExecuteAction(document, rejectRequest);

GitHub Examples repository: https://github.com/dejandjenic/XWorkflows.Examples

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