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)
        {            
        }
    }
如果没有验证通过设计器会有相应的提示信息,如下图:
 

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活动只有一个分支"), } } }; } }
工作流如下:
 
调用如下:
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,如需转载请自行联系原作者
相关文章
|
12月前
|
监控 安全
关于在执行 SAP ERP MM 模块 Post Goods Issue 时修改 Material Cost 的讨论
关于在执行 SAP ERP MM 模块 Post Goods Issue 时修改 Material Cost 的讨论
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 QM 执行事务代码QE01为检验批录入结果直接进入Multiple Specification标签页?
SAP QM 执行事务代码QE01为检验批录入结果直接进入Multiple Specification标签页?
SAP QM 执行事务代码QE01为检验批录入结果直接进入Multiple Specification标签页?
SAP QM初阶之启用了Multiple Specification功能后检验批的不同之处?
SAP QM初阶之启用了Multiple Specification功能后检验批的不同之处?
SAP QM初阶之启用了Multiple Specification功能后检验批的不同之处?
SAP QM创建一个包含Multiple Specification的检验计划
SAP QM创建一个包含Multiple Specification的检验计划
SAP QM创建一个包含Multiple Specification的检验计划
SAP WM高阶之上架策略P(Storage Unit Type)
SAP WM高阶之上架策略P(Storage Unit Type)
SAP WM高阶之上架策略P(Storage Unit Type)
|
数据挖掘
SAP QM 使用QP01事务代码真的不能创建含有Multiple Specification的检验计划
SAP QM 使用QP01事务代码真的不能创建含有Multiple Specification的检验计划
SAP QM 使用QP01事务代码真的不能创建含有Multiple Specification的检验计划
SAP WM 2-Step Picking流程里创建的Group的分析
SAP WM 2-Step Picking流程里创建的Group的分析
SAP WM 2-Step Picking流程里创建的Group的分析
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 QE02 修改检验结果,报错 -No characteristics were found–
SAP QM QE02 修改检验结果,报错 -No characteristics were found–
SAP QM QE02 修改检验结果,报错 -No characteristics were found–