WF4活动验证:RequiredArgument,OverloadGroup及Constraints

简介:

1.RequiredArgument 和OverloadGroup 属性

OverloadGroup可以把活动中的参数和属性分成不同的组,每组作为一个整体。每组的属性必须同时指定。方式如下:
[RequiredArgument]
[OverloadGroup("G1")]
public InArgument<int> Num{ get; set; }
也可以在XAML中使用,如下:
<x:Property Name="Operand1" Type="InArgument(x:Int32)">
    <x:Property.Attributes>
        <RequiredArgumentAttribute />
    </x:Property.Attributes>
</x:Property>

例假如有一个请假单活动,我们要求申请者,部分必须同时填写,请假的开始时间应小于结束时间,自定义活动如下:
public sealed class LeaveForm:CodeActivity
    {
        [RequiredArgument]
        [OverloadGroup("G1")]
        public InArgument<string> ApplyName { get; set; }

        [RequiredArgument]
        [OverloadGroup("G1")]
        public InArgument<string> ApplyDept { get; set; }

        public DateTime StartTime { get; set; }
        public DateTime EndTime { get; set; }

        protected override void CacheMetadata(CodeActivityMetadata metadata)
        {
            base.CacheMetadata(metadata);
            if(StartTime>EndTime)
            {
                metadata.AddValidationError("开始时间应小于结束时间");
            }
        }
        protected override void Execute(CodeActivityContext context)
        {            
        }
    }
如果没有验证通过设计器会有相应的提示信息,如下图:
image 

2.Constraints

WF4还提供了Constraints,Constraint也是一个活动,他包含了验证的逻辑。我们可以将Constraint加入到活动的Constraints属性中进行验证。如下:

public sealed class CaryActivity : CodeActivity
{
    public CaryActivity()
    {
        base.Constraints.Add(CheckPickBranch());
    }
}

也可以使用ValidationSettings实例的AdditionalConstraints属性。下面举例:

先来定义两个约束:

class CaryConstraints
    {        
        public static Constraint CheckThenOrElse() 
        {
            DelegateInArgument<If> element = new DelegateInArgument<If>();

            return new Constraint<If>
            {
                Body = new ActivityAction<If, ValidationContext>
                {
                    Argument1 = element,
                    Handler = new AssertValidation
                    {                        
                        Assertion = new InArgument<bool>(env => (element.Get(env).Then != null)
|| (element.Get(env).Else != null)), Message = new InArgument<string>("'If' 活动应该有Then或Else"), } } }; } public static Constraint CheckPickBranch() { DelegateInArgument<Pick> element = new DelegateInArgument<Pick>(); return new Constraint<Pick> { Body = new ActivityAction<Pick, ValidationContext> { Argument1 = element, Handler = new AssertValidation { IsWarning = true, Assertion = new InArgument<bool>(env => (element.Get(env).Branches.Count == 0)
|| (element.Get(env).Branches.Count > 1)), Message = new InArgument<string>("这个Pick活动只有一个分支"), } } }; } }
工作流如下:
image 
调用如下:
static void Main()
        {            
            ValidationSettings validationSettings = new ValidationSettings
            {                
                AdditionalConstraints =
                    {                             
                        {typeof(If), new List<Constraint> {CaryConstraints.CheckThenOrElse()}}, 
                        {typeof(Pick), new List<Constraint> {CaryConstraints.CheckPickBranch()}}
                    }
            };
            ValidationResults results = ActivityValidationServices.Validate(new CaryAL.Activity1(), validationSettings);
            if (results.Errors.Count == 0 && results.Warnings.Count == 0)
            {
                Console.WriteLine("No warnings or errors");
            }
            else
            {                
                foreach (ValidationError error in results.Errors)
                {
                    Console.WriteLine("Error: " + error.Message);
                }
                foreach (ValidationError warning in results.Warnings)
                {
                    Console.WriteLine("Warning: " + warning.Message);
                }
            }          
        }

结果如下:

Error: The private implementation of activity '1: Activity1' has the following validation error:  
'If' 活动应该有Then或Else
Warning: The private implementation of activity '1: Activity1' has the following validation error:  
这个Pick活动只有一个分支

请按任意键继续. . .

本文转自生鱼片博客园博客,原文链接:http://www.cnblogs.com/carysun/archive/2009/11/29/WF4-ActivityValidate.html,如需转载请自行联系原作者
相关文章
|
7月前
flowable流程部署只生成了act_re_deployment,未生成act_re_procdef
flowable流程部署只生成了act_re_deployment,未生成act_re_procdef
SAP WM中阶LT06对物料做上架报错 -Definition of totals level is missing for storage type 004 -
SAP WM中阶LT06对物料做上架报错 -Definition of totals level is missing for storage type 004 -
SAP WM中阶LT06对物料做上架报错 -Definition of totals level is missing for storage type 004 -
SAP QM执行事务代码QE23为检验批录入结果,报错-No selected set exists for the inspection point 200 or plant NMDC-
SAP QM执行事务代码QE23为检验批录入结果,报错-No selected set exists for the inspection point 200 or plant NMDC-
SAP QM执行事务代码QE23为检验批录入结果,报错-No selected set exists for the inspection point 200 or plant NMDC-
SAP WM 执行VL06P为交货单创建组,报错-TO for multiple deliveries only with delayed delivery update-
SAP WM 执行VL06P为交货单创建组,报错-TO for multiple deliveries only with delayed delivery update-
SAP WM 执行VL06P为交货单创建组,报错-TO for multiple deliveries only with delayed delivery update-
SAP MM ML81N为采购订单创建服务接收单,报错- No matching PO items selected -
SAP MM ML81N为采购订单创建服务接收单,报错- No matching PO items selected -
SAP MM ML81N为采购订单创建服务接收单,报错- No matching PO items selected -
SAP QM 启用04检验类型的物料,工单上STOCK TYPE不是QI?
SAP QM 启用04检验类型的物料,工单上STOCK TYPE不是QI?
SAP QM 启用04检验类型的物料,工单上STOCK TYPE不是QI?
SAP PP COR1事务里下达工单,保存时报错:No data was found for the input values
SAP PP COR1事务里下达工单,保存时报错:No data was found for the input values
SAP PP COR1事务里下达工单,保存时报错:No data was found for the input values
|
Java
营销活动送红包之更改现金活动状态(alipay.marketing.campaign.cash.status.modify)-java版
说明: 本帖是测试营销活动送红包的更改现金活动状态接口,本帖是使用沙箱环境测试的,仅供参考!! 是否需要签约:需要,【如何签约】  是否支持沙箱环境:支持 接口文档:查看  sdk下载:下载  营销活动送红包沙箱Java版demo:download:营销活动送红包Java版.
542 12
|
Java
营销活动之活动创建接口koubei.marketing.campaign.activity.create(创建口令送活动)-Java版
说明:  本帖是测试营销活动创建口令送活动,首先要创建口碑门店,本帖是沙箱环境测试,仅供参考!!   是否支持沙箱环境:支持   接口文档:查看   sdk下载:下载   沙箱Java版营销活动demo:download:营销活动Java版.
735 11