[Castle AR] 3. Validate

[ 2007-05-08 14:03:32 | 作者: yuhen ]
字号: | |
在业务设计中,对数据往往有确定的格式限制。我们通常的做法是在用户输入界面做这些处理,不过 Castle AR 为我们提供了另外一个备选方案。当我们无法确定类库或服务调用者是否会进行格式检查时,这个功能就非常实用了。要实现这个功能需要 ActiveRecordValidationBase / ActiveRecordValidationBase<T>,以及一些与之配套的验证特性。

uploads/200705/08_140423_ar6.gif


验证特性说明
  • ValidateNotEmpty: 属性不能为空,包括 String.Empty。
  • ValidateConfirmation: 验证两个属性必须相等,如密码验证。
  • ValidateEmail: 验证邮件格式。
  • ValidateLength: 属性字符串长度必须等于或者在某个长度之间。
  • ValidateRegExp: 自定义正则表达式验证。
  • ValidateCreditCard: 验证信用卡号码格式,包括各类常见的信用卡,诸如 Visa、MasterCard 等。
  • ValidateIsUnique: 属性值在数据表中必须是唯一的。
我们还可以从 AbstractValidationAttribute 继承,创建自己的验证特性。

使用方法

将类型基类改为 ActiveRecordValidationBase,然后添加相应的验证特性。在数据库操作之前,调用 IsValid()。

使用演示
[ActiveRecord("Users")]
public class User : ActiveRecordValidationBase<User>
{
    private int id;

    [PrimaryKey(PrimaryKeyType.Identity)]
    public int Id
    {
        get { return id; }
        set { id = value; }
    }

    private string name;

    [Property(Unique = true, NotNull = true)]
    [ValidateNotEmpty("用户名不能为空!")]
    public string Name
    {
        get { return name; }
        set { name = value; }
    }

    private string password;

    [Property]
    public string Password
    {
        get { return password; }
        set { password = value; }
    }

    private string password2;

    [ValidateConfirmation("Password")]
    public string Password2
    {
        get { return password2; }
        set { password2 = value; }
    }

    private string email;

    [Property]
    [ValidateEmail("邮箱地址格式错误!")]
    public string Email
    {
        get { return email; }
        set { email = value; }
    }

    private string address;

    [Property]
    [ValidateLength(5, 50, "长度必须在 5 ~ 50 之间!")]
    public string Address
    {
        get { return address; }
        set { address = value; }
    }

    private string postcode;

    [Property]
    [ValidateLength(6, "长度必须等于6!")]
    [ValidateRegExp("[\\d]{6}")]
    public string Postcode
    {
        get { return postcode; }
        set { postcode = value; }
    }

    private string creditCard;

    [Property]
    [ValidateCreditCard(CreditCardValidator.CardType.VISA)]
    public string CreditCard
    {
        get { return creditCard; }
        set { creditCard = value; }
    }
}

public class ARTester
{
    public static void Test()
    {
        ActiveRecordStarter.Initialize(Assembly.GetExecutingAssembly(), 
            new XmlConfigurationSource("ar.xml"));

        ActiveRecordStarter.DropSchema();
        ActiveRecordStarter.CreateSchema();

        User user = new User();
        user.Name = "zs";
        user.Password = "pwd";
        user.Email = "abc";
        user.Address = "x";
        user.Postcode = "xx";
        user.CreditCard = "12345fdas";

        if (user.IsValid())
        {
            user.Create();
        }
        else
        {
            foreach (string s in user.ValidationErrorMessages)
                Console.WriteLine(s);
        }
    }
}
[最后修改由 yuhen, 于 2007-05-08 15:12:45]
评论Feed 评论Feed: http://www.rainsts.net/feed.asp?q=comment&id=485

这篇日志没有评论。

发表评论
表情图标
[smile] [confused] [cool] [cry]
[eek] [angry] [wink] [sweat]
[lol] [stun] [razz] [redface]
[rolleyes] [sad] [yes] [no]
[heart] [star] [music] [idea]
UBB代码
转换链接
表情图标
悄悄话
用户名:   密码:  
验证码 * 请输入验证码