您会为.net项目推荐哪种validation框架?

要用于基于Web的mvc3 .net应用程序,您会推荐哪种validation框架? 应用程序遵循域模型模式和域模型POCO在单独的类库中?

所需的validation类型将是……非空,基于正则表达式等

我会选择FluentValidation,这是一个很棒的开源项目

https://github.com/JeremySkinner/FluentValidation

它同样适用于基本和更复杂的validation

如果您需要一个失败列表(而不是一次一个exception),那么我喜欢Enterprise Library Validation块。

请访问以下url查看powerpoint演示文稿: http : //msdn.microsoft.com/en-us/library/ff650484.aspx

您可以针对POCO对象连接大多数基本validation。 并且可以在.config文件中设置很多预制规则。

你可以写自己的规则。

我的规则非常精细。 他们一次执行1次validation。

举个简单的例子:我会有2个不同的规则来决定雇员是否可以雇佣(基于出生日期)。 一条规则将确保指定员工的生日。
第二条规则将确保当前日期减去出生日期大于18年。 (或任何规则)。

(现在让我们假设我有一堆规则)。 因此,在validation例程运行之后,我会返回列表中所有(无效)情况的列表。 例如,如果我正在validation员工,我会得到一份残疾人名单。

“没有提供LastName”

“没有提供FirstName”

“没有提供SSN”

而不是“一次一个”。 (“一次一个”这样做可能需要多次传递才能最终确定支票的有效性)。

下面是一些示例代码。 让我们说有人试图买一本ISBN“ABC123456”的书。

以下是一个自定义规则,用于检查该书是否存在(例如,在您的产品数据库中)。 我想你可以跟着走。 它将与Book(.cs)poco对象相连。 (没有显示“接线”)。 我只是想给你一个快速的例子,说明创建一个简单规则有多难(或不难)。

当找不到书籍时(使用isbn)….然后你会看到validationResults.AddResult方法。 这就是你得到多个残疾人的方法。 当您检查validation查询时,您将有权访问该集合。

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Text.RegularExpressions; using Microsoft.Practices.EnterpriseLibrary.Validation; using Microsoft.Practices.EnterpriseLibrary.Validation.Validators; namespace MyCompany.Applications.MyApplication.BusinessLogic.Validation.MyType1Validations { [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field)] public class BookExistsValidatorAttribute : ValidatorAttribute { protected override Validator DoCreateValidator(Type targetType) { return new BookExistsValidator("BookExistsValidatorTag"); } } public class BookExistsValidator : Validator { public BookExistsValidator(string tag) : base("BookExistsValidatorMessageTemplate", tag) { } protected override string DefaultMessageTemplate { get { throw new NotImplementedException(); } } protected override void DoValidate(string objectToValidate, object currentTarget, string key, ValidationResults validationResults) { bool bookExists = BookMatchExists(objectToValidate); if (!bookExists) { string msg = string.Format("The Book does not exist. Your ISBN='{0}'", objectToValidate); validationResults.AddResult(new ValidationResult(msg, currentTarget, key, 10001, this)); /* 10001 is just some number I made up */ } } private bool BookMatchExists(string isbn) { bool returnValue = false; IBookCollection coll = MyCompany.Applications.MyApplication.BusinessLogic.CachedControllers.BookController.FindAll(); /* Code not shown, but this would hit the db and return poco objects of books*/ IBook foundBook = (from item in coll where item.ISBN.Equals(book, StringComparison.OrdinalIgnoreCase) select item).SingleOrDefault(); if (null != foundBook) { returnValue = true; } return returnValue; } } }