【问题标题】:Which validation framework would you recommend for .net projects? [closed]您会为 .net 项目推荐哪种验证框架? [关闭]
【发布时间】:2012-01-05 21:38:58
【问题描述】:

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

所需的验证类型将是...非 Null、基于正则表达式等

【问题讨论】:

  • 你有没有发现不同框架的优缺点比较?

标签: c# asp.net asp.net-mvc-3


【解决方案1】:

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

https://github.com/JeremySkinner/FluentValidation

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

【讨论】:

  • @KnowledgeSeeker 对我来说似乎不必要地复杂......但我没有在实际项目中使用它来给出有效的意见。我只能说 FluentValidation 似乎是 mvc 项目的绝佳解决方案
  • 您有使用 EntLib 验证应用程序块的经验吗?如果是,为什么您更喜欢 FluentValidation?
  • 这适用于简单的 Winform 或控制台应用程序吗?
【解决方案2】:

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

在以下位置查看幻灯片演示: http://msdn.microsoft.com/en-us/library/ff650484.aspx

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

您可以编写自己的规则。

我的规则非常细化。他们一次执行 1 次验证。

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

(现在让我们假设我有一堆规则)。 因此,在验证例程运行后,我会返回列表中所有(无效)情况的列表。例如,如果我正在验证员工,我会得到一个无效列表。

“未提供姓氏”

“未提供名字”

“未提供 SSN”

而不是“一次一个”。 (“一次一个”进行可能需要多次通过才能最终确定支票的有效性)。

下面是一些示例代码。假设有人想买一本 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验证框架吗?
  • 没有。我没有。我从 EnterpriseLibrary 开始,发现我可以在大约 2 天内得到我需要的东西。有关两者的更多信息,请参阅:stackoverflow.com/questions/3806447/…stackoverflow.com/questions/751700/…。 Enterprise Library 一直有很好的支持和文档,所以多年来我一直坚持使用它。我已经在使用 E.L.Data,所以使用验证块对我来说是一个简单的过渡。
猜你喜欢
  • 1970-01-01
  • 2011-05-10
  • 1970-01-01
  • 2010-09-16
  • 2010-11-04
  • 2010-12-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多