WF4.0中实现子流程

简介:

  工作流服务中,经常会在主流程启用一些子流程。我在审批流程中经常会使用bookmark来暂停流程,这篇文章,将结合bookmark来实现主流程启动子流程。

    使用以前的一篇WF4.0自定义持久化中的自定义的持久化。不过数据表中加入了一个字段parentid,用于标识父流程:

   下面用一个流程实例为例说明主流程是如何启用子流程,子流程又是如何返回主流程的,主流程如下:

    

第一个节点“第一站审核”和第三个节点“第二站审核”都是BookMark书签,附BookMark的代码如下:

复制代码
代码
    public   sealed   class  Read < TResult >  : NativeActivity < TResult >
    {
        
public  Read()
            : 
base ()
        {
        }

        
public   string  BookmarkName {  get set ; }

        
//  Define an activity input argument of type string
         public  InArgument < string >  Text {  get set ; }



        
//  Must return true for a NativeActivity that creates a bookmark
         protected   override   bool  CanInduceIdle
        {
            
get  {  return   true ; }
        }

        
protected   override   void  Execute(NativeActivityContext context)
        {
            context.CreateBookmark(
this .BookmarkName,  new  BookmarkCallback( this .Continue));
        }

        
void  Continue(NativeActivityContext context, Bookmark bookmark,  object  obj)
        {
            
this .Result.Set(context, (TResult)obj);
        }
复制代码

第二个节点“启用子流程”,它是一个自定义的节点,代码如下:

复制代码
代码
  public   sealed   class  CallChild : Activity
    {

        
public   string  FlowName {  get set ; }


        
public  CallChild()
        {
            
base .Implementation  =   new  Func < Activity > (CreateBody);
        }

        
public  Activity CreateBody()
        {

            
return   new  Sequence
            {
                DisplayName 
=   " 子流程 " ,
                Activities 
=
                    {   
                        
new  ChildCodeActivity
                        {
                            FlowName
= this .FlowName      
                        }
                        ,
                        
new  Read < string >
                        {
                            BookmarkName
= " CallChild " ,
                         
                        }
                   
                    }
            };
        }
    }
复制代码

     注意上面的ChildCodeActivity类,实际上是在ChildCodeActivity中启动子流程的,ChildCodeActivity后面是一个书签,用于暂停主流程。当子流程完成后,在子流程中恢复这个书签,子流程结束,主流程继续往下跑。这个活动中有一个FlowName属性,用于表示是启用哪个子流程。ChildCodeActivity代码如下:

复制代码
代码
sealed   class  ChildCodeActivity : CodeActivity
    {
        
//  Define an activity input argument of type string
         public   string  FlowName {  get set ; }


        
//  If your activity returns a value, derive from CodeActivity<TResult>
        
//  and return the value from the Execute method.
         protected   override   void  Execute(CodeActivityContext context)
        {
            Guid ChildGuid;
            ChildGuid 
=  WorkFlowRun.CreateAndRun(FlowName);
            InstancesTable obj 
=  InstancesTableBiz.GetInstancesTable(ChildGuid); // 取得子流程的id
            obj.parentid  =  context.WorkflowInstanceId;
            InstancesTableBiz.UpdateInstancesTable(obj);
// 跟新父流程id
        }
    }
复制代码

WorkFlowRun.CreateAndRun(FlowName)根据FlowName启动相应的子流程,并得到实例的Guid。并将子流程的parentid修改改成主流程的guid。

子流程的示例如下:

子流程的第一个节点“子流程第一站审核”和第二个节点“子流程第二站审核”也都是BookMark书签。

最后一个节点是“结束”。这个节点也至关重要,因为我是使用这个节点,从子流程中返回到主流程的。因此,每个子流程都会有End节点,它的代码如下:

复制代码
代码
    public   sealed   class  End : CodeActivity
    {
        
//  Define an activity input argument of type string
         public  InArgument < string >  Text {  get set ; }

        
//  If your activity returns a value, derive from CodeActivity<TResult>
        
//  and return the value from the Execute method.
         protected   override   void  Execute(CodeActivityContext context)
        {
            
//  Obtain the runtime value of the Text input argument
             string  text  =  context.GetValue( this .Text);
            Guid id 
=  context.WorkflowInstanceId;
            InstancesTable Obj 
=  InstancesTableBiz.GetInstancesTable(id);
            
if  (Guid.Empty  !=  Obj.parentid) // 如果是子流程,返回父流程
            {
                WorkFlowRun.Submit(Obj.parentid, 
" ParentProcess " " returnMain " );
            }
        }
    }
复制代码

这是我思考出的在WF4.0中一个启用子流程的方案,如果你有什么更好的方案和建议,请给我留言,谢谢。





本文转自麒麟博客园博客,原文链接:http://www.cnblogs.com/zhuqil/archive/2010/01/31/SubProcessDemo.html,如需转载请自行联系原作者

相关文章
SAP WM 2-Step Picking流程里创建的Group的分析
SAP WM 2-Step Picking流程里创建的Group的分析
SAP WM 2-Step Picking流程里创建的Group的分析