[Castle ActiveRecord] 3. Validate

本文介绍CastleAR提供的数据验证功能,通过ActiveRecordValidationBase及验证特性确保数据格式正确。示例展示了如何创建验证规则并应用于User类。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

验证特性说明

  • 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);
        }
    }
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值