詳解WF 4.0中如何實現子流程
在WF 4.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。
子流程的示例如下:
#T#
子流程的***個節點“子流程***站審核”和第二個節點“子流程第二站審核”也都是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中一個啟用子流程的方案,如果你有什么更好的方案和建議,請給我留言,謝謝。
原文標題:WF4.0中實現子流程
鏈接:http://www.cnblogs.com/zhuqil/archive/2010/01/31/SubProcessDemo.html