.Net 轻量级工作流引擎 WorkflowCore的使用(十五)—— If判断

If判断比较简单,根据流程关联的数据对象中的值进行判断,如果条件满足执行相应的分支。需要注意的是没有else相关语句,如果需要实现相关逻辑,需要再次进行一次条件相反的判断。下面是简单的例子,仍然使用前面定义的数据类和步骤,输入采用Activity:

using System;
using System.Collections.Generic;
using System.Text;
using WorkflowCore.Interface;
using WorkflowCore.Models;
using ZL.WorflowCoreDemo.InputDataToStep;
using ZL.WorflowCoreDemo.InputDataToStep.Steps;

namespace ZL.WorflowCoreDemo.ControlStructures
{
    public class IfWorkflow : IWorkflow
    {
        public string Id => "IfWorkflow";
        public int Version => 1;

        public void Build(IWorkflowBuilder builder)
        {
            builder
                .StartWith(context=> ExecutionResult.Next())
                .Activity("activity-1", (data) => data.MyName)
                        .Output(data => data.MyName, step => step.Result)    
                .If(data => data.MyName.Length < 3)
                    .Do(then=>then
                        .StartWith(context => { Console.WriteLine("输入小于3个字符"); ExecutionResult.Next(); }))
                .If(data => data.MyName.Length >= 3)
                    .Do(then => then
                        .StartWith(context => { Console.WriteLine("输入大于等于3个字符"); ExecutionResult.Next(); }))
                .Then()
                   .Input(step => step.Name, data => data.MyName);
        }
    }
}

流程的运行代码如下:

            IServiceProvider serviceProvider = ConfigureServices();
            var host = serviceProvider.GetService();
            host.RegisterWorkflow();

            host.Start();

            var myClass = new MyNameClass { MyName = "张三" };

            host.StartWorkflow("IfWorkflow", 1, myClass);

            var activity = host.GetPendingActivity("activity-1", "worker1", TimeSpan.FromMinutes(1)).Result;


            if (activity != null)
            {
                Console.WriteLine("输入名字");
                string value = Console.ReadLine();
                host.SubmitActivitySuccess(activity.Token, value);
                
            }

            Console.ReadLine();
            host.Stop();

流程运行结果如下:


图片.png

你可能感兴趣的:(.Net 轻量级工作流引擎 WorkflowCore的使用(十五)—— If判断)