要用于基于Web的mvc3 .net应用程序,您会推荐哪种验证框架?应用程序遵循领域模型模式和领域模型POCO在单独的类库中?您对.NET项目推荐哪种验证框架?
排序验证,将被要求将... NOT NULL,基于正则表达式等
要用于基于Web的mvc3 .net应用程序,您会推荐哪种验证框架?应用程序遵循领域模型模式和领域模型POCO在单独的类库中?您对.NET项目推荐哪种验证框架?
排序验证,将被要求将... NOT NULL,基于正则表达式等
spring.net怎么样? – InfoLearner 2012-01-05 22:54:54
@KnowledgeSeeker对我来说似乎不必要的复杂..但我没有用过真正的项目来给出一个有效的意见。我只能说FluentValidation看起来像是一个很好的解决方案,用于mvc项目 – 2012-01-05 23:02:58
您是否有使用EntLib验证应用程序块的经验?如果是,为什么你更喜欢FluentValidation? – 2012-06-28 21:32:35
如果您需要故障列表(而不是一次一个例外),那么我喜欢Enterprise Library Validation块。
查看PowerPoint演示文稿时: http://msdn.microsoft.com/en-us/library/ff650484.aspx
您可以连接最多最基本的验证对你的POCO对象。 许多预生产规则可以在.config文件中设置。
你可以编写自己的规则。
我的规则非常精细。他们一次执行1次验证。
作为一个简单的例子:我会有两个不同的规则来决定雇员是否可以雇佣(根据出生日期)。 一个规则可以确保员工的生日是特定的。
第二条规则将确保当前日期减去出生日期大于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验证框架? – InfoLearner 2012-01-05 22:42:20
不,我没有。我从EnterpriseLibrary开始,找出我可以在两天内得到所需的东西。请参阅:http://stackoverflow.com/questions/3806447/spring-net-vs-enterprise-library和http://stackoverflow.com/questions/751700/which-validation-framework-to-choose-spring-validation-或者验证申请,以获得关于这两者的更多评论。企业图书馆一直有很好的支持和文档,所以多年来我一直坚持它。我已经在使用E.L.Data,所以使用验证块对我来说是一个简单的过渡。 – granadaCoder 2012-01-06 16:45:14
您是否发现了不同框架的优缺点比较? – 2012-06-06 12:42:37