你会为.NET项目推荐哪个验证框架?

12

要用于基于web的mvc3 .net应用程序,您会推荐哪个验证框架? 应用程序遵循领域模型模式,领域模型POCO在单独的类库中?

需要的验证类型将是...非空、基于正则表达式等。


1
你找到了不同框架的优缺点比较吗? - Michael Freidgeim
2个回答

25

@KnowledgeSeeker 对我来说似乎过于复杂了..但我还没有在真实项目中使用它,无法对其发表有效的意见。我只能说,FluentValidation 对于mvc项目来说似乎是一个很好的解决方案。 - Bassam Mehanni
你有使用过EntLib Validation Application Block的经验吗?如果有,为什么你更喜欢FluentValidation? - Michael Freidgeim
这适用于简单的WinForms或控制台应用程序吗? - Mitulát báti

3
如果您需要一份失败列表(而不是逐个异常),那么我推荐使用企业库验证模块。您可以在以下幻灯片中查看演示:http://msdn.microsoft.com/en-us/library/ff650484.aspx。您可以对大多数基本验证针对您的POCO对象进行连接。并且许多预制规则可以在.config文件中设置。您也可以编写自己的规则。我的规则非常细致。它们一次执行一个验证。例如,我将有两个不同的规则来决定员工是否可雇用(基于出生日期)。一个规则会确保指定了员工的出生日期。第二个规则将确保当前日期减去出生日期大于18岁(或任何其他规则)。现在假设我已经有了一堆规则。因此,在验证例程运行后,我会得到一个无效情况列表。例如,如果我正在验证员工,则会获得无效列表。"未提供LastName","未提供FirstName"。

"未提供社会安全号码"

而不是"逐个检查"。(逐个检查可能需要多次才能最终确定您的支票的有效性)。

以下是一些示例代码。假设有人试图购买ISBN为“ABC123456”的书籍。

下面是一个自定义规则,用于检查该书是否存在(例如,在您的产品数据库中)。我认为你可以跟上。它将与Book(.cs) poco对象连接在一起。(没有显示任何“连接”)。我只是试图给你一个简单规则的快速示例,以展示创建简单规则的难易程度。

当找不到一本书(使用isbn)时...那么您会看到validationResults.AddResult方法。这就是您获得多个无效结果的方式。稍后在检查验证查询时,您将可以访问该集合。

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<string>
    {

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



    }
}

你使用过 Spring.NET 验证框架吗? - InfoLearner
1
没有。我开始使用EnterpriseLibrary,并发现我可以在大约两天内获得所需的内容。请参见:http://stackoverflow.com/questions/3806447/spring-net-vs-enterprise-library和https://dev59.com/MEfRa4cB1Zd3GeqP7Tkx以获取有关这两个框架的更多评论。Enterprise Library一直拥有良好的支持和文档,因此多年来我一直坚持使用它。我已经在使用E.L.Data,因此对于我来说,使用验证块是一个简单的过渡。 - granadaCoder

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接