詳解WF 4.0中如何實(shí)現(xiàn)子流程
在WF 4.0中,存在不同種類(lèi)的流程。本文將介紹的子流程如何在主流程中實(shí)現(xiàn)的原理,希望對(duì)大家有所幫助。
工作流服務(wù)中,經(jīng)常會(huì)在主流程啟用一些子流程。我在審批流程中經(jīng)常會(huì)使用bookmark來(lái)暫停流程,這篇文章,將結(jié)合bookmark來(lái)實(shí)現(xiàn)主流程啟動(dòng)子流程。
使用以前的一篇WF4.0自定義持久化中的自定義的持久化。不過(guò)數(shù)據(jù)表中加入了一個(gè)字段parentid,用于標(biāo)識(shí)父流程:
下面用一個(gè)流程實(shí)例為例說(shuō)明主流程是如何啟用子流程,子流程又是如何返回主流程的,主流程如下:
***個(gè)節(jié)點(diǎn)“***站審核”和第三個(gè)節(jié)點(diǎn)“第二站審核”都是BookMark書(shū)簽,附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);
- }
第二個(gè)節(jié)點(diǎn)“啟用子流程”,它是一個(gè)自定義的節(jié)點(diǎn),代碼如下:
代碼
- 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類(lèi),實(shí)際上是在ChildCodeActivity中啟動(dòng)子流程的,ChildCodeActivity后面是一個(gè)書(shū)簽,用于暫停主流程。當(dāng)子流程完成后,在子流程中恢復(fù)這個(gè)書(shū)簽,子流程結(jié)束,主流程繼續(xù)往下跑。這個(gè)活動(dòng)中有一個(gè)FlowName屬性,用于表示是啟用哪個(gè)子流程。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)根據(jù)FlowName啟動(dòng)相應(yīng)的子流程,并得到實(shí)例的Guid。并將子流程的parentid修改改成主流程的guid。
子流程的示例如下:
#T#
子流程的***個(gè)節(jié)點(diǎn)“子流程***站審核”和第二個(gè)節(jié)點(diǎn)“子流程第二站審核”也都是BookMark書(shū)簽。
***一個(gè)節(jié)點(diǎn)是“結(jié)束”。這個(gè)節(jié)點(diǎn)也至關(guān)重要,因?yàn)槲沂鞘褂眠@個(gè)節(jié)點(diǎn),從子流程中返回到主流程的。因此,每個(gè)子流程都會(huì)有End節(jié)點(diǎn),它的代碼如下:
代碼
- 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中一個(gè)啟用子流程的方案,如果你有什么更好的方案和建議,請(qǐng)給我留言,謝謝。
原文標(biāo)題:WF4.0中實(shí)現(xiàn)子流程
鏈接:http://www.cnblogs.com/zhuqil/archive/2010/01/31/SubProcessDemo.html