关闭

跟我一起学Windows Workflow Foundation(7)-----给定制活动添加有效性验证

465人阅读 评论(0) 收藏 举报
 

 

给定制活动属性添加有效性验证

这个实验也没什么可详细说的了,无非就是给我们之前发送E-Mail的活动的属性里加个验证,这里我们验证了E-mail的To和From属性是否符合邮件的格式。如果不是正确的邮件格式,我们则在编译的时候不允许进行编译。

下面大概过一下:

1. 打开我们上次的那个定制活动项目,在 SendMailActivityLibrary这个项目上添加一个新的代码文件,名称叫做ParametersValidator.cs

2. 在代码文件中输入如下代码,代码继承自ActivityValidator,如果是合成的活动(就是上上次我们拼出来的那个自定义活动),那么代码应该继承自CompositeActivityValidatorActivityValidator在设计和运行时状态会进行验证,这些验证依赖于活动,在我们进行编译和运行时活动时,会自动运行这些代码查看我们的属性是否符合规则。

using System;
using System.Workflow.Activities;
using System.Workflow.ComponentModel;
using System.Workflow.ComponentModel.Compiler;
using System.Text.RegularExpressions;
using System.Net.Mail;
namespace SendMailActivityLibrary
{
public class ParametersValidator : ActivityValidator
{
}
}

3. 重写ValidateProperties方法来完成我们的验证。活动的ValidateProperties方法是在进行编译时执行的验证方法。这些验证语句将验证我们输入的这些属性是否是正确的E-mail格式

public override ValidationErrorCollection ValidateProperties(ValidationManager manager, object obj)
{
ValidationErrorCollection validationErrors = new ValidationErrorCollection(base.ValidateProperties (manager, obj));
SendMailActivity sendMailActivityToBeValidated = obj as SendMailActivity;
if (sendMailActivityToBeValidated == null)
{
throw new InvalidOperationException("Parameter obj is not of type SendMailActivity");
}
if (!IsValidEmailAddress(sendMailActivityToBeValidated.To))
{
ValidationError CustomActivityValidationError =
new ValidationError(String.Format("/'{0}/' is an Invalid destination e-mail address", sendMailActivityToBeValidated.To), 1);
validationErrors.Add(CustomActivityValidationError);
}
if (!IsValidEmailAddress(sendMailActivityToBeValidated.From))
{
ValidationError CustomActivityValidationError =
new ValidationError(String.Format("/'{0}/' is an Invalid source e-mail address", sendMailActivityToBeValidated.From), 1);
validationErrors.Add(CustomActivityValidationError);
}
return validationErrors;
}
public Boolean IsValidEmailAddress(String address)
{
// must only proceed with validation if we have data    
// to validate
if (address == null || address.Length == 0)
return true;
Regex rx = new Regex(@"[^A-Za-z0-9@/-_.]", RegexOptions.Compiled);
MatchCollection matches = rx.Matches(address);
if (matches.Count > 0)
return false;
// Must have an '@' character
int i = address.IndexOf('@');
// Must be at least three chars after the @
if (i <= 0 || i >= address.Length - 3)
return false;
// Must only be one '@' character
if (address.IndexOf('@', i + 1) >= 0)
return false;
// Find the last . in the address
int j = address.LastIndexOf('.');
// The dot can't be before or immediately after the @ char
if (j >= 0 && j <= i + 1)
return false;
return true;
}

4. 修改 SendMailActivity.cs 的代码

5. 添加 ActivityValidator 属性到 SendMailActivity, 如下面所示:

namespace SendMailActivityLibrary

{

[ActivityValidator(typeof(ParametersValidator))]

public partial class SendMailActivity: System.Workflow.ComponentModel.Activity

{

6. 重新进行编译.

7. 下面我们进行测试,修改SendMailWorkflow.cs 中的SendMailActivity活动,修改他的From属性,让他不是一个正确的E-mail格式,如:shinji3292163.com。

8. 重新进行编译。

9. 可以看到如下界面:

10. 修改成正常的,就可以编译成功了。

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:214193次
    • 积分:3801
    • 等级:
    • 排名:第8707名
    • 原创:160篇
    • 转载:1篇
    • 译文:0篇
    • 评论:27条