search
HomeDatabaseMysql TutorialWF 4.0 之持久化操作一:SqlServer方式的存储

没有 持久 化的WF 能称为一个完整的WF吗,答案是否定的;如果WF不能 持久 化,那么流程就需要一次就执行完毕,所有的 操作 就要一次走下去,可现实中的工作流是这样吗,答案同样是否定的。一个投票流程需要多个评委投票之后确定一个票数最高的组长才可以进入

     没有持久化的WF 能称为一个完整的WF吗,答案是否定的;如果WF不能持久化,那么流程就需要一次就执行完毕,所有的操作就要一次走下去,可现实中的工作流是这样吗,答案同样是否定的。一个投票流程需要多个评委投票之后确定一个票数最高的组长才可以进入下一个流程,如果不能持久化,那么在此流程中每个评委使用的数据(在流程中需要处理的公共数据,可能为简单类型,也可能为复杂类型)则完全不同,甚至说每个人都是不一样的,我们要在每一个步骤记录下数据,在流程结束时候进行汇总分析,很明显流程没有根据原始的思路一步一步走下去,所以我们需要在所有投票完成之前都保持一个挂起状态,只有等所有的投票完成,流程才结束,此时我们想到了持久化。

持久化分析之BookMark:

持久化之前,我提到了需要挂起状态,为什么要挂起,挂起是一个怎样的操作。在WF中一个挂起很简单,就是一个BookMark(书签),顾名思义,先存放一个书签,暂时结束,如果下次需要根据书签找到上次停止的地方,继续进行操作

持久化分析之InstanceStore:

这个类为WF提供的,用于进行持久化的操作,WF中已经包含了一个实现它的SqlWorkflowInstanceStore,用于保存到数据库中,当然本文还会介绍另外一种是使用XML的方式。此类保存到的数据库有些要求,使用的sql脚本微软已经提供,下文将会一一说明。

 持久化分析之PersistenceParticipant:

PersistenceParticipant class or the PersistenceIOParticipant class (derived class of the PersistenceParticipant class), implements abstract methods, and then add an instance of the class as a workflow instance extension.">持久性参与者派生自 PersistenceParticipant 类或 PersistenceIOParticipant 类(PersistenceParticipant 类的派生类),实现抽象方法,然后添加类的实例作为工作流实例扩展。WorkflowApplication and WorkflowServiceHost look for such extensions when persisting an instance and invoke appropriate methods at appropriate times."> WorkflowApplication 和 WorkflowServiceHost 在保留实例时查找此类扩展,并在适当的时间调用适当的方法。 根据MSDN的解释可以看出来此类是我们自定义持久化需要继承的类,那么在进行XML方式持久化的时候我们就会用到,Sql方式的时候无需自定义具体信息下一篇介绍。

WorkflowApplication and WorkflowServiceHost look for such extensions when persisting an instance and invoke appropriate methods at appropriate times."> 

到此,我们的关键技术分析已经完成,一个BookMark类和一个InstanceStore类以及自定义需要的PersistenceParticipant三个类来共同完成我们的持久化。

持久化实现之业务分析:

第一步,我们先来分析一个流程的,假设有若干个评委来投票,需要所有的评委投票完成之后流程才能进入到下一步,这里的投票可以认为是评委需要登录到系统进行投票,并且投票的先后顺序以及时间都不确定,
根据分析设计工作流如下:

一个包含了DoWhile和Foreach的流程,DoWhile控制是否进行多次审批,ForEach用于遍历所有的评委,同时创建多个书签,注意此处使用的Parallel循环,让创建书签的过程为同步,如果使用ForEach则只会创建一个书签;

在If中创建书签使流程同步,此时就是需要持久化的过程,同时每激活一个或多个也要再次持久化,直到流程结束。

 

持久化实现之BookMark类:

此类代码较简单直接贴出来

 <span>public</span> <span>sealed</span> <span>class</span> WaitForVote<t>: NativeActivity<t><span>
    {
        </span><span>public</span> InArgumentstring> UserId { <span>get</span>; <span>set</span><span>; }

        </span><span>public</span> InOutArgument<requestforexpert> InOutRequestForExpert { <span>get</span>; <span>set</span><span>; }

        </span><span>protected</span> <span>override</span> <span>void</span><span> Execute(NativeActivityContext context)
        {
            </span><span>string</span> name =  <span>this</span><span>.UserId.Get(context).ToString();
            context.CreateBookmark(name, </span><span>new</span><span> BookmarkCallback(OnReadComplete));
        }

        </span><span>void</span> OnReadComplete(NativeActivityContext context, Bookmark bookmark, <span>object</span><span> state)
        {  
            </span><span>string</span>[] input = (<span>string</span><span>[])state;
            context.SetValue(</span><span>this</span><span>.Result, input);
            RequestForExpert requestForExpert </span>=<span> context.GetValue(InOutRequestForExpert);
            requestForExpert.ExpertList.Find((ExpertInfo expert) </span>=> { <span>return</span> expert.UserId == input[<span>0</span>]; }).IsConfirmed=<span>true</span><span>;
            context.SetValue(InOutRequestForExpert ,requestForExpert);
        }

        </span><span>protected</span> <span>override</span> <span>bool</span><span> CanInduceIdle
        {
            </span><span>get</span><span>
            {
                </span><span>return</span> <span>true</span><span>;
            }
        }</span></requestforexpert></t></t>

此类继承了NativeActivity,有关该类的具体说明可以查看MSDN。
该类主要为以下几个部分:
1.一个InArguement的参数,用于接收书签的名称
2.重写Execute方法,用于创建书签,通过传递的参数,作为书签的名称,同时指定激活回调方法
3.给BookMark指定激活回调方法,可以在激活书签的时候传递值,在此方法中可以处理值,并传递到页面去(InOutRequestForExpert就是页面传递到书签的值,在此方法中进行修改再返回到页面去)
4.设置属性CanInduceIdle属性为true,这样才可以进行持久化。

 

持久化实现之宿主类:

 <span>interface</span><span> IExpertHost
    {
        </span><span>///</span> <span><summary></summary></span>
        <span>///</span><span> 创建并运行WorkFlow实例
        </span><span>///</span> <span></span>
        <span>///</span> <span><param name="rfe"></span>
        <span>///</span> <span><returns></returns></span>
<span>        WorkflowApplication CreateAndRun(RequestForExpert rfe);
        </span><span>///</span> <span><summary></summary></span>
        <span>///</span><span> 加载WorkFlow实例
        </span><span>///</span> <span></span>
        <span>///</span> <span><param name="instanceId"></span>
        <span>///</span> <span><returns></returns></span>
<span>        WorkflowApplication LoadInstance(Guid instanceId);

        </span><span>///</span> <span><summary></summary></span>
        <span>///</span><span> 当前项目是否可以投票
        </span><span>///</span> <span></span>
        <span>///</span> <span><param name="instanceId"></span><span>实例编号</span><span></span>
        <span>///</span> <span><param name="userId"></span><span>评委编号</span><span></span>
        <span>///</span> <span><returns></returns></span>
        <span>bool</span> CanVoteToInstance(Guid instanceId, <span>string</span><span> userId);
     </span><span>///</span> <span><summary></summary></span>
        <span>///</span><span> 投票
        </span><span>///</span> <span></span>
        <span>///</span> <span><param name="instanceId"></span><span>WorkFlow实例编号</span><span></span>
        <span>///</span> <span><param name="projectId"></span><span>项目编号</span><span></span>
        <span>///</span> <span><param name="userId"></span><span>用户编号</span><span></span>
        <span>///</span> <span><param name="selUserId"></span><span>推荐评委编号</span><span></span>
        <span>void</span> Vote(Guid instanceId, <span>string</span> userId, <span>string</span><span> selUserId);
    }</span>

首先来看CreateAndRun的实现写法:
这里定义了一个枚举类型,来表明持久化使用Sql方式还是XML的方式

 <span>public</span> <span>enum</span><span> StoreType
    {
        Sql, Xml
    }</span>

  <span>public</span><span> WorkflowApplication CreateAndRun(RequestForExpert rfe)
        {
           
            IDictionary</span>string, <span>object</span>> inputs = <span>new</span> Dictionarystring, <span>object</span>><span>();
            inputs.Add(</span><span>"</span><span>InRequestForExpert</span><span>"</span><span>, rfe);
            </span><span>//</span><span> 实例化工作流对象</span>
            Activity wf = <span>new</span><span> ExpertWorkFlow();
            WorkflowApplication instance </span>= <span>new</span><span> WorkflowApplication(wf, inputs);
            instance.PersistableIdle </span>+=<span> OnIdleAndPersistable;
            instance.Completed </span>+=<span> OnWorkflowCompleted;
            instance.Idle </span>+=<span> OnIdle;
            </span><span>//</span><span><strong>持久</strong>化设置</span>
<span>            GetSqlInstanceStore(instance, StoreType.Sql);
            instance.Run();
            </span><span>return</span><span> instance;
        }

     </span><span>///</span> <span><summary></summary></span>
        <span>///</span><span> 设置<strong>持久</strong>化<strong>方式</strong>
        </span><span>///</span> <span></span>
        <span>///</span> <span><param name="instance"></span>
        <span>private</span> <span>void</span><span> GetSqlInstanceStore(WorkflowApplication instance, StoreType storeType)
        {
            </span><span>switch</span><span> (storeType)
            {
                </span><span>case</span><span> StoreType.Sql:
                    SqlWorkflowInstanceStore sqlInstanceStore </span>= <span>new</span><span> SqlWorkflowInstanceStore(connectionString);
                    InstanceView view </span>= sqlInstanceStore.Execute(sqlInstanceStore.CreateInstanceHandle(), <span>new</span> CreateWorkflowOwnerCommand(), TimeSpan.FromSeconds(<span>30</span><span>));
                    </span><span>//</span><span>设置默认实例的所有者</span>
                    sqlInstanceStore.DefaultInstanceOwner =<span> view.InstanceOwner;
                    instance.InstanceStore </span>=<span> sqlInstanceStore;
                    </span><span>break</span><span>;
                </span><span>case</span><span> StoreType.Xml:
                    XmlWorkflowInstanceStore store </span>= <span>new</span><span> XmlWorkflowInstanceStore(instance.Id);
                    instance.InstanceStore </span>=<span> store;
                    </span><span>break</span><span>;
                </span><span>default</span><span>:
                    </span><span>break</span><span>;
            }
        }</span>

    <span>//</span><span> executed when instance goes idle</span>
        <span>public</span> <span>void</span><span> OnIdle(WorkflowApplicationIdleEventArgs e)
        {
        }

        </span><span>public</span><span> PersistableIdleAction OnIdleAndPersistable(WorkflowApplicationIdleEventArgs e)
        {
            </span><span>return</span><span> PersistableIdleAction.Unload;
        }

        </span><span>//</span><span> executed when instance is persisted</span>
        <span>public</span> <span>void</span><span> OnWorkflowCompleted(WorkflowApplicationCompletedEventArgs e)
        {

        }</span>

在上述方法中创建了一个WorkFlowApplication工作流应用对象,同时绑定PersistableIdle ,此函数说明使用怎样的持久方式,此处使用的为Unload即卸载并持久化,这样在Load的时候不会出现实例被锁定的错误。

设置持久方式,此处使用的是Sql方式存储,在存储的方法中主要是指定了要保存的数据库,此数据库可不是一般的数据库,需要特性的Sql脚本,别急此脚本已在你的电脑中,打开目录 "C:\Windows\Microsoft.NET\Framework\v4.0.30319\SQL\en"

创建一个数据库名字为WorkflowInstanceStore(可自定义),先执行第二个文件,再执行第一个文件。

下面再来看看其他的方法:

<span>     ///</span> <span><summary></summary></span>
        <span>///</span><span> 加载工作流
        </span><span>///</span> <span></span>
        <span>///</span> <span><param name="instanceId"></span>
        <span>///</span> <span><param name="userId"></span>
        <span>///</span> <span><returns></returns></span>
        <span>public</span><span> WorkflowApplication LoadInstance(Guid instanceId)
        {
            WorkflowApplication instance </span>= <span>new</span> WorkflowApplication(<span>new</span><span> ExpertWorkFlow());
            instance.Completed </span>+=<span> OnWorkflowCompleted;
            instance.PersistableIdle </span>+=<span> OnIdleAndPersistable;
            instance.Idle </span>+=<span> OnIdle;
            </span><span>//</span><span><strong>持久</strong>化设置</span>
<span>            GetSqlInstanceStore(instance, StoreType.Sql);
            instance.Load(instanceId);
            </span><span>return</span><span> instance;
        }

     </span><span>///</span> <span><summary></summary></span>
        <span>///</span><span> 当前评委是否可以投票,判断其有没有投票
        </span><span>///</span> <span></span>
        <span>///</span> <span><param name="instanceId"></span>
        <span>///</span> <span><param name="userId"></span>
        <span>///</span> <span><returns></returns></span>
        <span>public</span> <span>bool</span> CanVoteToInstance(Guid instanceId, <span>string</span><span> userId)
        {
            WorkflowApplication instance </span>=<span> LoadInstance(instanceId);
            </span><span>//</span><span>除非存在当前的书签,则说明没有投票</span>
            <span>foreach</span> (BookmarkInfo item <span>in</span><span> instance.GetBookmarks())
            {
                </span><span>if</span><span> (item.BookmarkName.Equals(userId))
                {
                    </span><span>return</span> <span>true</span><span>;
                }
            }
            </span><span>return</span> <span>false</span><span>;
        }

      </span><span>///</span> <span><summary></summary></span>
        <span>///</span><span> 投票
        </span><span>///</span> <span></span>
        <span>///</span> <span><param name="instanceId"></span><span>工作流实例Id</span><span></span>
        <span>///</span> <span><param name="userId"></span><span>用户Id</span><span></span>
        <span>///</span> <span><param name="selUserId"></span><span>推荐的用户Id</span><span></span>
        <span>public</span> <span>void</span> Vote(Guid instanceId, <span>string</span> userId, <span>string</span><span> selUserId)
        {

            WorkflowApplication instance </span>=<span> LoadInstance(instanceId);
            </span><span>//</span><span>除非存在当前的书签,则说明没有投票</span>
            <span>foreach</span> (BookmarkInfo item <span>in</span><span> instance.GetBookmarks())
            {
                </span><span>if</span><span> (item.BookmarkName.Equals(userId))
                {
                    instance.ResumeBookmark(userId, </span><span>new</span> <span>string</span><span>[] { userId, selUserId });
                }
            }
            instance.Unload();
        }</span>

一个用于加载工作流的方法,一个判断是否可以投票的方法(如果对应当前评委的书签还存在,则可以投票,注意这里书签的名称都为评委的Id,不过这个不重要,大家可以在自己的程序中自己决定使用什么,只是在激活

书签的时候需要使用一样的值,这样才可以找到书签),一个就是投票的方法(主要用于激活书签)。 

 

持久化实现之WCF以及Web客户端:
到这里WorkFlow的工作已经完成了看看怎么调用,在这里我是通过WCF把WorkFlow和Web端结合起来,所以又添加了一个WCF的项目WFWCF(源码中都包含),添加WCF IExpertWCF

<span>  [ServiceContract]
    </span><span>public</span> <span>interface</span><span> IExpertWCF
    {
        </span><span>///</span> <span><summary></summary></span>
        <span>///</span><span> 创建工作流
        </span><span>///</span> <span></span>
        <span>///</span> <span><param name="rfe"></span>
<span>        [OperationContract]
        </span><span>void</span><span> CreateWorkFlow(RequestForExpert rfe);

        </span><span>///</span> <span><summary></summary></span>
        <span>///</span><span>投票
        </span><span>///</span> <span></span>
        <span>///</span> <span><param name="instanceId"></span>
        <span>///</span> <span><param name="userId"></span>
        <span>///</span> <span><param name="selUserId"></span>
<span>        
        [OperationContract]
        </span><span>void</span> Vote(Guid instanceId,<span>string</span> userId,<span>string</span><span> selUserId);
    }

  </span><span>public</span> <span>class</span><span> ExpertWCF : IExpertWCF
    {

        </span><span>private</span> <span>string</span> ConnectionString = ConfigurationManager.ConnectionStrings[<span>"</span><span>ApplicationServices</span><span>"</span><span>].ConnectionString;

        </span><span>///</span> <span><summary></summary></span>
        <span>///</span><span> 创建workFlow
        </span><span>///</span> <span></span>
        <span>///</span> <span><param name="rfe"></span>
        <span>public</span> <span>void</span><span> CreateWorkFlow(RequestForExpert rfe)
        {
            ExpertProcessHost host </span>= <span>new</span><span> ExpertProcessHost();</span>
<span>            host.CreateAndRun(rfe);
        }


        </span><span>///</span> <span><summary></summary></span>
        <span>///</span><span> 投票
        </span><span>///</span> <span></span>
        <span>///</span> <span><param name="instanceId"></span>
        <span>///</span> <span><param name="userId"></span>
        <span>///</span> <span><param name="selUserId"></span>
        <span>public</span> <span>void</span> Vote(Guid instanceId, <span>string</span> userId, <span>string</span><span> selUserId)
        {</span>
            ExpertProcessHost host = <span>new</span><span> ExpertProcessHost();
            host.Vote(instanceId, projectId, userId, selUserId);
        }
}</span>

方法都很简单,一个创建工作流的方法,一个投票的方法,实例化ExpertProcessHost 对象直接调用即可。
最后来看页面设计

首页,用于创建工作流

点击按钮会在数据库声称如下的一条数据(数据值不同),而Id列就是在投票页面要使用的InstanceId。

投票页面,输入InstanceId,点击投票即可投票,激活对应用户的书签(此处的用户为写固定,可以自行修改,同时InstanceId可以调试获得,或者从数据库的InstancesTable表中获得),当激活所有的书签之后流程将结束,同时数据库则会自动删除此条数据。至此,Sql方式持久化就结束了,下一篇开始将介绍XML方式持久操作

 

源码在下一篇中。

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Vue中如何实现数据的缓存和持久化Vue中如何实现数据的缓存和持久化Oct 15, 2023 pm 01:06 PM

Vue中如何实现数据的缓存和持久化在Vue中,数据的缓存和持久化是一个常见的需求。缓存数据可以提高应用的性能,而持久化数据可以让用户在刷新页面或重新打开应用后依然能够看到之前保存的数据。下面将介绍如何通过一些常用的方法实现数据的缓存和持久化。使用Vuex实现数据缓存Vuex是Vue的官方状态管理库,它可以用来集中管理应用的所有组件的状态。我们可以利用Vuex

PHP编程中有哪些常见的Behat操作?PHP编程中有哪些常见的Behat操作?Jun 12, 2023 am 08:19 AM

PHP编程中有哪些常见的Behat操作?Behat是一个行为驱动开发(BDD)工具,允许测试人员和开发人员在自然语言中撰写测试用例,并将这些用例转化为可执行的代码。它支持PHP语言,并提供了丰富的库和功能,可以实现多种常见的测试操作。下面列举了PHP编程中常见的Behat操作。前置条件(Background)在编写测试用例时,有时候会有一些公共的前置条件需要

Java 缓存技术中的缓存持久化机制Java 缓存技术中的缓存持久化机制Jun 20, 2023 am 09:33 AM

随着互联网应用的不断发展,数据缓存技术在大规模应用中显得尤为重要。Java作为目前广泛应用的一种编程语言,也有着许多应对缓存技术的工具和技术。其中缓存持久化机制在Java中具有重要的应用价值,本文将详细阐述该技术。一、缓存技术的应用在Java应用中,数据缓存的作用非常重要。随着应用规模的不断扩大,对数据的处理能力和数据的存储能力都提出了更高的要求。缓存技术就

PHP编程中有哪些常见的OAuth操作?PHP编程中有哪些常见的OAuth操作?Jun 12, 2023 am 08:48 AM

OAuth(开放授权)是一种用于授权访问控制的标准化协议。在Web开发中,使用OAuth可以帮助应用程序安全地从第三方平台中获取用户数据或资源。而在PHP编程中,OAuth操作也被广泛应用。本文将介绍PHP编程中的常见OAuth操作。OAuth1.0a授权OAuth1.0a授权是OAuth中最早出现的授权方式,也是最复杂的一种授权方式。在PHP编程中,O

Redis的持久化备份与恢复Redis的持久化备份与恢复May 11, 2023 am 08:19 AM

Redis是一款高性能的内存数据库,其优势在于快速读取数据和高并发能力。但是,由于数据全部存储在内存中,一旦服务器奔溃或宕机,数据就会丢失。为了避免这种情况的发生,Redis提供了持久化存储功能,将内存中的数据写入磁盘中,以便在需要时进行数据恢复。Redis具有两种持久化方式:RDB快照和AOF日志。下面将分别讲解这两种方式的实现、优缺点和适用场景。RDB快

踏上持久层开发的巅峰:掌握 Hibernate 框架的知识点踏上持久层开发的巅峰:掌握 Hibernate 框架的知识点Feb 19, 2024 pm 04:36 PM

实体映射Hibernate的核心思想之一就是实体映射,它将Java对象映射到数据库表中,从而实现了面向对象的持久化。其提供的多种映射方式,包括注解映射、XML映射等,可以满足不同开发者的需求。例如,使用注解映射,开发者只需在Java类上添加@Entity注解,即可将其映射为数据库表,而字段映射则通过@Column注解实现。@EntitypublicclassUser{@Id@GeneratedValue(strategy=GenerationType.IDENTITY)privateLongid

java框架中持久化中间件的技术比较java框架中持久化中间件的技术比较Jun 03, 2024 pm 01:24 PM

Java持久化中间件技术比较:JDBC:可扩展且高效,但冗长且易出错。Hibernate:易于使用,提供高级功能,但可能较慢。JPA:标准化,与JavaEE兼容,但配置限制较多。选择适合的技术取决于应用程序需求:JDBC注重控制和可扩展性,Hibernate/JPA注重开发效率和功能。

怎样使用ThinkPHP6进行多语言翻译操作?怎样使用ThinkPHP6进行多语言翻译操作?Jun 12, 2023 am 08:49 AM

随着全球化的发展,越来越多的网站和应用程序需要提供多语言支持。而对于使用ThinkPHP6框架的开发者来说,如何实现多语言翻译操作是一个重要的需求。本文将介绍怎样使用ThinkPHP6进行多语言翻译操作。配置语言包在ThinkPHP6中,语言包是一个包含键值对的数组。可以将其存储在app/lang/目录下的各种子目录中。例如:/app/lang/zh-cn/

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),