[WF4.0 实战] WF4.0申请审批实例

定义

 

百度百科:

 

工作流(Workflow),就是“业务过程的部分或整体在计算机应用环境下的自动化”,它主要解决的是“使在多个参与者之间按照某种预定义的规则传递文档、信息或任务的过程自动进行,从而实现某个预期的业务目标,或者促使此目标的实现”。

 

一句话:工作流就是一系列相互衔接、自动进行的业务活动。

再通俗点就是对于一个业务需求不需要我们像以前一样手动去调用,而是使用工作流自动执行

 

优势


1,不必手动去调各个方法,而由工作流控制程序的执行

2,状态的维护由工作流处理,减轻程序员工作量

3,更加面像对象(对于工作流的结点的抽象可以实现更好的复用)

 

 

下面为简单的实现示例

 

1,开发结点

2,根据需求组织工作流

3,使用工作流

 

开发工作流结点:


[WF4.0 实战] WF4.0申请审批实例_第1张图片


结点生成的相应活动:


[WF4.0 实战] WF4.0申请审批实例_第2张图片   


组织相应的工作流:


[WF4.0 实战] WF4.0申请审批实例_第3张图片


使用工作流:



代码示例:


/// <summary>
    /// 启动工作流
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Start_Click(object sender, EventArgs e)
    {
        RequestInfo.RequestInfo info = new RequestInfo.RequestInfo();
       
        info.InstanceID = Guid.Empty;
        info.RealName = txtRealName.Text;
        info.YourCompany = "hjy";
        info.YourInterest = "hjy";
        info.YourPost = "[email protected]";      
        info.Email = "hejingyuan";
        WorkFlowProcess.CreateAndRun(info);
        lbMsg.Text = "申请成功,等待管理员审核...";
    }

 // creates a workflow application, binds parameters, links extensions and run it
        public static Guid CreateAndRun(RequestInfo.RequestInfo Request)
        {
            SqlWorkflowInstanceStore instanceStore = new SqlWorkflowInstanceStore("server=.;database=aspnetdb;uid=sa;pwd=123456");

            InstanceView view = instanceStore.Execute(instanceStore.CreateInstanceHandle(), new CreateWorkflowOwnerCommand(), TimeSpan.FromSeconds(30));

            instanceStore.DefaultInstanceOwner = view.InstanceOwner;

            IDictionary<string, object> input = new Dictionary<string, object> 
            {
                { "Request" , Request }
            };

            WorkflowApplication application = new WorkflowApplication(new ApplyBlogFlow(), input);

            application.InstanceStore = instanceStore;

            //获取或设置当前工作流实例处于空闲状态并可被保留时调用的 ActivityFunc
            application.PersistableIdle = (e) =>
            {
                instanceUnloaded.Set();
                return PersistableIdleAction.Unload;
            };
            //获取或设置卸载当前工作流
            application.Unloaded = (e) =>
            {
                instanceUnloaded.Set();
            };
            //获取或设置当前工作流实例遇到未处理的异常时
            application.OnUnhandledException = (ex) =>
            {
                Console.Write("Exception");
                return UnhandledExceptionAction.Terminate;
            };

            Guid id = application.Id;

            application.Run();
            //阻止当前线程,直到当前waithand收到信号
            instanceUnloaded.WaitOne();
            return id;
        }

/// <summary>
    /// 激活工作流(输入工作流id)
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Activite_Click(object sender, EventArgs e)
    {
        RequestInfo.RequestInfo info = new RequestInfo.RequestInfo();
        info.InstanceID = Guid.Parse(lblQC.Text);        
        info.RealName = txtRealName.Text;
        info.Email = "hejingyuan";
        info.Agree = "true";
    
        WorkFlowProcess.RunInstance(info);
        lbMsg.Text = "提交成功!";
    }

public static void RunInstance(RequestInfo.RequestInfo Request)
        {
            instanceUnloaded = new AutoResetEvent(false);
            SqlWorkflowInstanceStore instanceStore = new SqlWorkflowInstanceStore("server=.;database=aspnetdb;uid=sa;pwd=123456");

            IDictionary<string, object> input = new Dictionary<string, object> 
            {
                { "Request" , Request }
            };

            WorkflowApplication application1 = new WorkflowApplication(new ApplyBlogFlow());

            application1.InstanceStore = instanceStore;
            //工作流当前实例完成
            application1.Completed = (workflowApplicationCompletedEventArgs) =>
            {

                Console.WriteLine("\nWorkflowApplication has Completed in the {0} state.", workflowApplicationCompletedEventArgs.CompletionState);

            };
            //卸载当前工作流
            application1.Unloaded = (workflowApplicationEventArgs) =>
            {
                Console.WriteLine("WorkflowApplication has Unloaded\n");
                //发信号,说明值已经被写进去了。
                instanceUnloaded.Set();

            };

            application1.Load(Request.InstanceID);

            application1.ResumeBookmark("Apply", Request.Agree);
             //此段代码起作用了,之前是因为默认为true,并没有自动设置为false,故在前面手动设置了一下!2014年9月30日
            instanceUnloaded.WaitOne();

            Console.ReadLine();

            
        }

总结

 

        有时候我们看似深奥的东西其实并非如此,任何知识只要潜心研究,其实都不难,重要的不是知识本身,而是要解决不怕不知道就怕不知道的问题!




你可能感兴趣的:([WF4.0 实战] WF4.0申请审批实例)