在开源趋势下,很多开源的组件在国内,乃至全网都少有案例。为了做这个工作流翻了许多帖子和github的帖子
在这里对github ZL.WorflowCoreDemo,和PizzaRestaurantWorkflow-main 表示感谢,同时也感谢给博客园的帖子。
本案例再利用ZL.WorflowCoreDemo 中的项目直接进行新加的。关于如何安装工作流请随便在园子里找一篇文章即可。
在案例中可能有许多做的不合理的地方,请广大园友指正。欢迎大家留言讨论
本案例为控制台应用程序
不多说直接上代码:
1. 定义请假基础类
namespace ZL.WorflowCoreDemo.AskForLeave
{
internal class ApproveMoldel
{
public int ServerId { get; set; }
public string Name { get; set; } = "张三的请假申请";
public bool DepApprove { get; set; }
public bool HrApprove { get; set; }
public int Status { get; set; } = 0;
public Approve Dep { get; set; }
public Approve Hr { get; set; }
}
internal class Approve
{
public bool approveStatus { get; set; }
public string Opinion { get; set; }
}
}
2,定义部门审批中间流程
using System;
using WorkflowCore.Interface;
using WorkflowCore.Models;
namespace ZL.WorflowCoreDemo.AskForLeave
{
internal class DepApprove : StepBody
{
/// <summary>
/// 数据库的id
/// </summary>
public int ServerId { get; set; }
public Approve Dep { get; set; }
public bool OutPut { get; set; }
public override ExecutionResult Run(IStepExecutionContext context)
{
OutPut = Dep.approveStatus;
//将意见存入数据库
Console.WriteLine("部门审批:" + OutPut);
if (OutPut)
{
Console.WriteLine("等待下一个人审批");
}
return ExecutionResult.Next();
}
}
}
3.定义Hr审批中间流程
using System;
using WorkflowCore.Interface;
using WorkflowCore.Models;
namespace ZL.WorflowCoreDemo.AskForLeave
{
internal class HrApprove : StepBody
{
/// <summary>
/// 数据库的id
/// </summary>
public int ServerId { get; set; }
public Approve hr { get; set; }
public bool OutPut { get; set; }
public override ExecutionResult Run(IStepExecutionContext context)
{
OutPut = hr.approveStatus;
//将意见存入数据库
Console.WriteLine("Hr审批:" + OutPut);
return ExecutionResult.Next();
}
}
}
4.定流程主服务
using System;
using WorkflowCore.Interface;
using WorkflowCore.Models;
namespace ZL.WorflowCoreDemo.AskForLeave
{
internal class AskForLeaveFlow : IWorkflow<ApproveMoldel>
{
public string Id => nameof(AskForLeaveFlow);
public int Version => 1;
public void Build(IWorkflowBuilder<ApproveMoldel> builder)
{
//分支1 同意审批也可以不要直接进入下一个审批
var branch1 = builder.CreateBranch()
.StartWith(x => Console.WriteLine("审批通过"));
//分支2 拒绝审批
var branch2 = builder.CreateBranch()
.StartWith(x => Console.WriteLine("审批不通过"))
.Then(x => Console.WriteLine("流程结束")).EndWorkflow();
//注册工作流流程顺序
builder.StartWith(c => ExecutionResult.Next())
.WaitFor("depFor", (data, context) => context.Workflow.Id, data => DateTime.Now)//定义部门审批事件
.Output(d => d.Dep, s => s.EventData)
.Then<DepApprove>()
.Input(i => i.ServerId, s => s.ServerId)
.Input(i => i.Dep, s => s.Dep)
.Output(d => d.DepApprove, s => s.OutPut)
.Decide(d => d.DepApprove)//条件分支
.Branch((d, o) => d.DepApprove, branch1)
.Branch((d, o) => !d.DepApprove, branch2)
.WaitFor("hrFor", (d, c) => c.Workflow.Id, d => DateTime.Now)//定义hr审批事件
.Output(d => d.Hr, s => s.EventData)
.Then<HrApprove>()
.Input(i => i.ServerId, s => s.ServerId)
.Input(i => i.hr, s => s.Hr)
.Output(d => d.HrApprove, s => s.OutPut)
.Decide(d => d.HrApprove)//条件分支
.Branch((d, o) => d.HrApprove, branch1)
.Branch((d, o) => !d.HrApprove, branch2)
.Then(x => Console.WriteLine("流程结束")).EndWorkflow();
}
}
}
5.定义启动程序
using Microsoft.Extensions.DependencyInjection;
using System;
using WorkflowCore.Interface;
namespace ZL.WorflowCoreDemo.AskForLeave
{
public class AskForLeaveFlowRun
{
public static async void Run()
{
IServiceProvider serviceProvider = ConfigureServices();
var host = serviceProvider.GetService<IWorkflowHost>();
//注册工作流
host.RegisterWorkflow<AskForLeaveFlow, ApproveMoldel>();
host.Start();//启动工作流主机
//员工请假申请发出
var model = new ApproveMoldel
{
ServerId = 1,
Dep = new(),
Hr = new(),
};
//定义一个工作流
string workflowId = await host.StartWorkflow("AskForLeaveFlow", 1, model);
//模拟部门经历审批
Console.WriteLine("输入意见");
var value = Console.ReadLine();
var dep = new Approve()
{
approveStatus = true,
Opinion = value
};
//点击发布订阅
await host.PublishEvent("depFor", workflowId, dep);
//模拟人事经理审批
Console.WriteLine("输入意见2");
var value2 = Console.ReadLine();
var hr = new Approve()
{
approveStatus = true,
Opinion = value2
};
//点击发布订阅
await host.PublishEvent("hrFor", workflowId, hr);
Console.ReadLine();
host.Stop();
}
private static IServiceProvider ConfigureServices()
{
//setup dependency injection
IServiceCollection services = new ServiceCollection();
services.AddLogging();
services.AddWorkflow();
var serviceProvider = services.BuildServiceProvider();
return serviceProvider;
}
}
}
6.调用
namespace ZL.WorflowCoreDemo
{
class Program
{
static void Main(string[] args)
{
AskForLeave.AskForLeaveFlowRun.Run();
}
}
}
自此完结!
标签:set,Console,get,开源,工作,WorflowCore,OutPut,using,public From: https://www.cnblogs.com/xiaohhan/p/17434774.html