活动验证逻辑概述
活动的验证程序组件中包括所有设计时和运行时验证逻辑。 这将确保活动的元数据中定义的活动属性可得到正确配置。
活动框架提供了广泛的默认验证功能,因此在许多情况下,不需要为活动编写自定义验证程序。 如果要编写自定义验证程序类,则该自定义类必须从 Validator 继承。
备注
默认情况下,即使将 ValidationOptionAttribute 设置为 ValidationOption.Required,也不会验证活动实例属性。 如果想要验证自定义活动中的实例属性,必须创建自己的自定义验证程序来执行此操作。
系统将在编译工作流期间自动进行验证,另外,在使用相应的活动元数据对象作为参数来调用活动的验证程序组件的 Validate 方法时,也将自动进行验证。 这使得工作流创作工具将使用 "correct-by-construction" 范例(如果工作流创作工具选择了此范例)。
此外,如果执行了工作流更改,则在运行时也将对工作流实例进行验证。 此验证可能与在编译时执行的验证不同。 此功能将确保运行时操作的安全,例如在正在运行的工作流实例的活动树中添加或替换活动。
下面的示例演示了如何通过从 ActivityValidator 类派生并重写 Validate 方法以添加自定义验证代码,来创建自定义活动验证程序。
Public Class CustomActivityValidator
Inherits ActivityValidator
Public Overrides Function Validate(ByVal manager As ValidationManager, ByVal obj As Object) As ValidationErrorCollection
Dim activity As Activity1 = CType(obj, Activity1)
If activity.Parent IsNot Nothing Or activity.Activities.Count <> 0 Then
Dim errors As ValidationErrorCollection = New ValidationErrorCollection()
errors.AddRange(MyBase.Validate(manager, obj))
Return errors
End If
Return New ValidationErrorCollection()
End Function
End Class
public class CustomActivityValidator : ActivityValidator
{
public override ValidationErrorCollection Validate(ValidationManager manager, object obj)
{
Activity1 activity = obj as Activity1;
if (activity.Parent != null || activity.Activities.Count != 0)
{
ValidationErrorCollection errors = new ValidationErrorCollection();
errors.AddRange(base.Validate(manager, obj));
return errors;
}
return new ValidationErrorCollection();
}
}
若要在活动中使用自定义验证程序,必须使用下面示例中所示的 ActivityValidatorAttribute 来修饰活动。
<ActivityValidator(GetType(CustomActivityValidator))> _
Public Class Activity1
Inherits SequenceActivity
Public Shared MyPropertyProperty As DependencyProperty = System.Workflow.ComponentModel.DependencyProperty.Register("MyProperty", GetType(String), GetType(Activity1))
<DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)> _
Public Property MyProperty() As String
Get
Return (CType((MyBase.GetValue(Activity1.MyPropertyProperty)), String))
End Get
Set(ByVal Value As String)
MyBase.SetValue(Activity1.MyPropertyProperty, value)
End Set
End Property
End Class
[ActivityValidator(typeof(CustomActivityValidator))]
public partial class Activity1: SequenceActivity
{
public Activity1()
{
InitializeComponent();
}
public static DependencyProperty MyPropertyProperty = System.Workflow.ComponentModel.DependencyProperty.Register("MyProperty", typeof(string), typeof(Activity1));
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public string MyProperty
{
get
{
return ((string)(base.GetValue(Activity1.MyPropertyProperty)));
}
set
{
base.SetValue(Activity1.MyPropertyProperty, value);
}
}
}
请参见
参考
概念
其他资源
版权所有 (C) 2007 Microsoft Corporation。保留所有权利。