(文章目录)
前言
1.什么是工作流
工作流是OA系统比较重要的功能之一,主要在于企业流程协同审批,有效进行流程管理。
流程管理起源于生产组织和办公自动化领域,是针对日常工作中具有固定程序的活动提出的一个概念。目的是通过将工作分解成定义良好的任务、角色,按照一定的规则和过程来执行这些任务并对它们进行监控,达到提高办事效率、降低生产成本、提高企业生产经营管理水平和企业竞争力的目标。
现代计算机技术实现了工作流,是支持企业经营过程高效执行并监控其执行过程的计算机软件系统。工作流软件类似电脑桌面操作系统,操作系统驱动电脑各部件协同运转,工作流软件则驱动业务过程自动有序进行。
Workflow-Core地址:https://github.com/danielgerlag/workflow-core
一、Workflow-Core轻量级工作流引擎
1.安装包
Install-Package WorkflowCore
Microsoft.Extensions.Logging
Microsoft.Extensions.Logging.Debug
2.随机流程
2.1 定义步骤
1、步骤一
using System;
using System.Linq;
using WorkflowCore.Interface;
using WorkflowCore.Models;
namespace WorkflowCore.Sample01.Steps
{
public class HelloWorld : StepBody
{
public override ExecutionResult Run(IStepExecutionContext context)
{
Console.WriteLine("Hello world");
return ExecutionResult.Next();
}
}
}
2、步骤二
public class RandomOutput : StepBody
{
public override ExecutionResult Run(IStepExecutionContext context)
{
Random rnd = new Random();
int value = rnd.Next(2);
Console.WriteLine("Generated random value {0}", value);
return ExecutionResult.Outcome(value);
}
}
3、随机步骤
public class CustomMessage : StepBody
{
public string Message { get; set; }
public override ExecutionResult Run(IStepExecutionContext context)
{
Console.WriteLine(Message);
return ExecutionResult.Next();
}
}
public class GoodbyeWorld : StepBody
{
public override ExecutionResult Run(IStepExecutionContext context)
{
Console.WriteLine("Goodbye world");
return ExecutionResult.Next();
}
}
2.2 定义工作流
public class SimpleDecisionWorkflow : IWorkflow
{
public string Id => "Simple Decision Workflow";
public int Version => 1;
public void Build(IWorkflowBuilder<object> builder)
{
builder
.StartWith<HelloWorld>()
.Then<RandomOutput>(randomOutput =>
{
randomOutput
.When(0)
.Then<CustomMessage>(cm =>
{
cm.Name("Print custom message");
cm.Input(step => step.Message, data => "Looping back....");
})
.Then(randomOutput); //loop back to randomOutput
randomOutput
.When(1)
.Then<GoodbyeWorld>();
});
}
}
2.3 启动工作流
using Microsoft.Extensions.DependencyInjection;
using WorkflowCore.Interface;
using WorkflowCore.Sample02;
IServiceProvider serviceProvider = ConfigureServices();
//start the workflow host
var host = serviceProvider.GetService<IWorkflowHost>();
host.RegisterWorkflow<SimpleDecisionWorkflow>();
host.Start();
host.StartWorkflow("Simple Decision Workflow");
Console.ReadLine();
host.Stop();
static IServiceProvider ConfigureServices()
{
//setup dependency injection
IServiceCollection services = new ServiceCollection();
services.AddLogging();
services.AddWorkflow();
var serviceProvider = services.BuildServiceProvider();
return serviceProvider;
}
2.4 运行
可以看见直接结束,让我们再次运行 这次运行发送三次消息才结束
二、Workflow-Core的流程配置
1.代码配置
public class MyWorkflow : IWorkflow
{
public void Build(IWorkflowBuilder<MyData> builder)
{
builder
.StartWith<Task1>()
.Then<Task2>()
.Then<Task3>();
}
}
2.JSON配置
{
"Id": "HelloWorld",
"Version": 1,
"Steps": [
{
"Id": "Hello",
"StepType": "MyApp.HelloWorld, MyApp",
"NextStepId": "Bye"
},
{
"Id": "Bye",
"StepType": "MyApp.GoodbyeWorld, MyApp"
}
]
}
3.YAML配置
Id: HelloWorld
Version: 1
Steps:
- Id: Hello
StepType: MyApp.HelloWorld, MyApp
NextStepId: Bye
- Id: Bye
StepType: MyApp.GoodbyeWorld, MyApp
标签:CORE,05,Workflow,Id,MyApp,using,ExecutionResult,public,轻量级
From: https://blog.51cto.com/u_15437432/6273820