WF4活动验证:RequiredArgument,OverloadGroup及Constraints
Posted on 2009-11-29 14:57 生鱼片 阅读(1869) 评论(4) 编辑 收藏 举报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: 请按任意键继续. . . |
作者:生鱼片
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。