[Castle ActiveRecord] 3. Validate

 在業務設計中,對數據往往有確定的格式限制。我們通常的做法是在用戶輸入界面做這些處理,不過 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);
        }
    }
}


 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章