.NET 中的验证框架可以在字段之间进行编辑
根据我的经验,.NET 中的许多验证框架允许您一次验证一个字段,以执行诸如确保字段是邮政编码或电子邮件地址之类的操作。我通常将这些称为“字段内编辑”。
在我的项目中,我们经常需要进行字段间编辑。例如,如果您有一个这样的类:
public class Range
{
public int Min { get; set; }
public int Max { get; set; }
}
您可能希望确保 Max 大于 Min。您可能还想对外部对象进行一些验证。例如,假设您有一个这样的类:
public class Person
{
public string PostalCode { get; set; }
}
并且出于某种原因您想要确保邮政编码存在于数据库或提供给您的文件中。我有更复杂的示例,例如用户提供数据字典,并且您想要根据该数据字典验证您的对象。
我的问题是:我们可以使用任何现有的 .NET 验证框架(TNValidate、NHibernate Validator)还是我们需要使用规则引擎还是什么?现实世界中的你们如何处理这种情况? :-)
From my experience many validation frameworks in .NET allow you to validate a single field at a time for doing things like ensuring a field is a postal code or email address for instance. I usually call these within-field edits.
In my project we often have to do between-field-edits though. For instance, if you have a class like this:
public class Range
{
public int Min { get; set; }
public int Max { get; set; }
}
you might want to ensure that Max is greater than Min. You might also want to do some validation against an external object. For instance given you have a class like this:
public class Person
{
public string PostalCode { get; set; }
}
and for whatever reason you want to ensure that Postal Code exists in a database or a file provided to you. I have more complex examples like where a user provides a data dictionary and you want to validate your object against that data dictionary.
My question is: can we use any of the existing validation frameworks (TNValidate, NHibernate Validator) for .NET or do we need to use a rules engine or what?? How do you people in the real world deal with this situation? :-)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我熟悉的只有一个验证框架,那就是 企业库验证应用程序块< /a>,或简称 VAB。我将从VAB的背景来回答你的问题。
第一个问题:可以在 VAB 中进行状态(字段间)验证吗?
是的,你可以。有多种方法可以做到这一点。您可以选择自我验证机制,如下所示:
我必须说我个人不喜欢这种类型的验证,特别是在验证我的域实体时,因为我喜欢将我的验证与验证逻辑分开(并保持我的域逻辑不受任何验证框架的引用)。然而,与编写自定义验证器类的替代方案相比,它们需要的代码要少得多。这是一个示例:
编写此类后,您可以将此类挂接到您的验证配置文件中,如下所示:
第二个问题:如何通过可能的数据库交互(与 VAB)进行复杂的验证。
我为第一个问题给出的例子也可用于此。您可以使用相同的技术:自我验证和自定义验证器。您想要检查数据库中的值的场景实际上很简单,因为对象的有效性并不基于其上下文。您可以简单地根据数据库检查对象的状态。当对象所在的上下文变得重要时,情况会变得更加复杂(但对于 VAB 来说这是可能的)。例如,想象一下,您想要编写一个验证,以确保每个客户在给定时刻没有超过两个未发货订单。这不仅意味着您必须检查数据库,而且可能在同一上下文中添加新订单或删除订单。这个问题不是 VAB 特有的,您选择的每个框架都会遇到相同的问题。我写了一篇文章来描述我们的复杂性我们正在面临这些情况(阅读并颤抖)。
第三个问题:现实世界中的你们如何处理这种情况?
我在生产代码中使用 VAB 进行这些类型的验证。它效果很好,但 VAB 不太容易学习。尽管如此,我仍然喜欢我们可以用 VAB 做的事情,并且当 v5.0 发布时它只会变得更好。如果您想学习它,请先阅读 ValidationHOL.pdf 文档,您可以在 动手实验室下载。
我希望这有帮助。
There's only one validation framework that I know well and that is Enterprise Library Validation Application Block, or VAB for short. I will answer your questions from the context of the VAB.
First question: Can you do state (between-field) validation in VAB?
Yes you can. There are multiple ways to do this. You can choose for the self validation mechanism, as follows:
I must say I personally don't like this type of validations, especially when validating my domain entities, because I like to keep my validations separate from the validation logic (and keep my domain logic free from references to any validation framework). However, they need considerably less code than the alternative, which is writing a custom validator class. Here's an example:
After writing this class you can hook this class up in your validation configuration file like this:
Second question: How to do complex validations with possible interaction a database (with VAB).
The examples I give for the first question are also usable for this. You can use the same techniques: self validation and custom validator. Your scenario where you want to check a value in a database is actually a simple one, because the validity of your object is not based on its context. You can simply check the state of the object against the database. It gets more complicated when the context in which an object lives gets important (but it is possible with VAB). Imagine for instance that you want to write a validation that ensures that every customer, at a given moment in time, has no more than two unshipped orders. This not only means that you have to check the database, but perhaps new orders that are added or orders are deleted within that same context. This problem is not VAB specific, you will have the same problems with every framework you choose. I've written an article that describes the complexities we're facing with in these situations (read and shiver).
Third question: How do you people in the real world deal with this situation?
I do these types of validation with the VAB in production code. It works great, but VAB is not very easy to learn. Still, I love what we can do with VAB, and it will only get better when v5.0 comes out. When you want to learn it, start with reading the ValidationHOL.pdf document that you can found in the Hands-On Labs download.
I hope this helps.
当我需要任何未包含在开箱即用的内容时,我会构建自定义验证控件。这里的好处是这些自定义验证器是可重用的,并且它们可以作用于多个字段。下面是我发布到 CodeProject 的 AtLeastOneOf 验证器示例,它允许您要求一组中至少有一个字段具有值:
http://www.codeproject.com/KB/validation/AtLeastOneOfValidator.aspx
下载中包含的代码应该作为一个易于遵循的示例,说明如何进行操作。这里的缺点是 ASP.Net 中包含的验证控件通常不能很好地与 asp.net-ajax 配合使用。
I build custom validation controls when I need anything that's not included out of the box. The nice thing here is that these custom validators are re-usable and they can act on multiple fields. Here's an example I posted to CodeProject of an AtLeastOneOf validator that lets you require that at least one field in a group has a value:
http://www.codeproject.com/KB/validation/AtLeastOneOfValidator.aspx
The code included in the download should work as an easy to follow sample of how you could go about it. The downside here is that Validation controls included with ASP.Net don't often work well with asp.net-ajax.