WF4 ViewStateService和补偿CompensableActivity

本文介绍WF4中ViewStateService的使用方法,通过自定义活动实现视图状态的存储和加载。同时探讨补偿机制,包括CompensableActivity、Compensate及Confirm活动的运用。

标题的两者并没有什么关系,只是内容都比较短就放到一起了。

1.在工作流中存储ModelItems的视图状态。

例子,   我们创建一个自定义活动的活动设计器ActivityDesigner1.XAML,如下:

<sap:ActivityDesigner x:Class="CaryActivityLibrary1.ActivityDesigner1"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    xmlns:sap="clr-namespace:System.Activities.Presentation;assembly=System.Activities.Presentation"

    xmlns:sapv="clr-namespace:System.Activities.Presentation.View;assembly=System.Activities.Presentation">

    <Grid>

        <StackPanel Name="stackPanel1" VerticalAlignment="Top" >

            <TextBox  Name="commentBlock"   />

            <Button Content="LoadViewState" Name="loadViewState" Click="loadViewState_Click" />

            <Button Content="CommitViewState"  Name="button1" Click="button1_Click" />

        </StackPanel>

    </Grid>

</sap:ActivityDesigner>

 

对应的代码如下:

private void loadViewState_Click(object sender, RoutedEventArgs e)

        {

            ViewStateService vss = this.Context.Services.GetService<ViewStateService>();

            commentBlock.Text = vss.RetrieveViewState(this.ModelItem, "comment") as string;

        }

 

        private void button1_Click(object sender, RoutedEventArgs e)

        {

            ViewStateService vss = this.Context.Services.GetService<ViewStateService>();

            vss.StoreViewStateWithUndo(this.ModelItem, "comment", commentBlock.Text);

        }

我们在工作流中使用该活动,输入一些信息后提交ViewState,你会看到工作流中增加了如下部分:

<sap:WorkflowViewStateService.ViewState>

      <scg3:Dictionary x:TypeArguments="x:String, x:Object">

        <x:String x:Key="comment">123456</x:String>

      </scg3:Dictionary>

    </sap:WorkflowViewStateService.ViewState>

2.补偿

补偿是提供给前面已经成功的操作一个补偿的机会,WF4提供了下面活动支持补偿:

image
1. CompensableActivity活动,该活动调用后会返回一个CompensationToken类型的Result属性,
2. Compensate活动和Confirm活动用于显示的调用CompensableActivity活动的confirmation handler 和 compensation handler部分。这两个活动都有一个Target属性用于指定CompensableActivity活动的Result属性。
3. CompensableActivity活动的CompenationHandler当工作流有异常时调用,ConfirmationHandler为工作流正常执行完成后调用不是CompensableActivity活动执行完成就立即调用,CancellationHandler为活动取消时调用。

参考资源:

1.http://blogs.msdn.com/mwinkle/archive/2009/12/06/wf4-viewstateservice.aspx
2.http://msdn.microsoft.com/en-us/library/system.activities.presentation.view.viewstateservice_members(VS.100).aspx
3.http://msdn.microsoft.com/en-us/library/dd489415(VS.100).aspx

 

 

 

本文转自生鱼片博客园博客,原文链接:http://www.cnblogs.com/carysun/archive/2009/12/07/WF4-ViewStateService-Compensate.html,如需转载请自行联系原作者

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值