惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Engineering at Meta
Engineering at Meta
Microsoft Azure Blog
Microsoft Azure Blog
I
InfoQ
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
人人都是产品经理
人人都是产品经理
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
T
Tailwind CSS Blog
MongoDB | Blog
MongoDB | Blog
Google DeepMind News
Google DeepMind News
WordPress大学
WordPress大学
量子位
美团技术团队
大猫的无限游戏
大猫的无限游戏
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Last Week in AI
Last Week in AI
博客园 - 司徒正美
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
小众软件
小众软件
C
Check Point Blog
博客园 - 三生石上(FineUI控件)
N
Netflix TechBlog - Medium
Recent Announcements
Recent Announcements
有赞技术团队
有赞技术团队
月光博客
月光博客

博客园 - JerryZhao

[转载]jQuery对象VS DOM对象 [Reference] Navigation on Matrix Subtotal [Refereces]Creating an .ini File for the Application Manager [Refereces] Developing and Deploying Pocket PC Setup Applications [References]More on device app installs - Installing a Windows Mobile device application from a desktop MSI [References]Deploying .NET Compact Framework 2.0 Applications with .cab and .msi Files [References]Installing Multiple CAB Files [Reference]Wix Restart IIS code snippet [Reference] Windows Installer XML (WiX) 3.0 Snippets [Reference] Casle Demo App: Midway Summary [ActiveRecord] 之五:ActiveRecordMediator [Reference][ActiveRecord] 之四:Cascade [Reference][ActiveRecord] 之六:继承 [Referece][ActiveRecord] 之七:多数据库配置 [Reference] [Castle AR] 5. Base Relations [Reference][Castle AR] 2. ActiveRecord [Reference][Castle AR] 1. Starter [reference][Castle AR] 4. CRUD [Reference]NHibernateDataSource: A DataSourceControl for ASP.NET 2.0
[Reference][Castle AR] 3. Validate
JerryZhao · 2008-01-23 · via 博客园 - JerryZhao

http://www.rainsts.net/article.asp?id=485
在业务设计中,对数据往往有确定的格式限制。我们通常的做法是在用户输入界面做这些处理,不过 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);
    }
  }
}