使用代码契约在 ASP.NET MVC 中进行验证
我想知道在 ASP.NET MVC 2 中使用“代码契约”属性作为验证规则的可用选项。
I want to know the available options for using the "Code Contracts" attributes as the validations rules in ASP.NET MVC 2.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以通过自定义运行时检查器来完成此操作。
尽管可以调整代码契约以用作验证框架,但我建议不要这样做。这是调整运行时检查器的更高级功能之一,更重要的是,它不是代码契约的用途。
代码契约基本上是一种在项目中集成契约设计的方法。尽管其目的之一是根据类设计验证您的代码,但它不是一个验证框架。
在我看来,您应该将它与另一个真正的验证框架一起使用。
在开始使用代码合同之前,我建议您阅读此处<的用户手册/a>.它包含所有信息,包括如何自定义运行时检查器。
You could do this by customizing the Runtime Checker.
Although it is possible to tweak Code Contracts into being used as a validation framework, I would advise against it. It is one of the more advanced features to tweak the Runtime Checker and more importantly, not what Code Contracts is made for.
Code Contracts is basically a means of integrating Design by Contract in your project. Although one of its purposes is to validate your code against your class design, it is not a validation framework.
In my opinion, you should use it next to another real validation framework.
Before you start using Code Contracts, I suggest you read the user manual here. It has all the information, including how to customize the Runtime Checker.
是的,“代码契约”的主要目的是根据我的类设计验证我的代码,但它也描述了我的对象的有效状态。
我能想到的验证场景只有以下三种类型:
验证,例如: NHibernate
Validator)
我认为验证是一个逻辑问题,无论目标对象是否处于有效状态。
DbC 具有三个部分:
第一部分(规则)在所有三种验证场景中都是通用的。当我使用“代码契约”将属性标记为“Not Null”以验证其设计时,使用 MVC(UI 验证)的“System.ComponentModel.DataAnnotations”将其标记为“Not Empty”是不是很难看?
简而言之,有效状态的定义是核心,验证框架和“代码契约”的共享部分提供了它,还有一个额外的功能,即用于领域设计的“静态检查器”(不是 I /O)。
Yes that is true that the main purpose of "Code Contracts" is to validate my code against my class design but it also describes the valid states of my object.
All I can think of validation scenarios, is of these three types:
Validation, Ex: NHibernate
Validator )
I consider validation as a logical question whether the target object is in a valid state or not.
The DbC has three parts,
The first part ( the rules) is common in all three scenarios of validation. When I tag a property with "Not Null" using "Code Contracts" to validate its design, isn't it ugly to have it tagged "Not Empty" using "System.ComponentModel.DataAnnotations" for MVC (UI Validation) ?
In abstract, the definition of valid states is core and the shared part of validation frameworks and "Code Contracts" provides it, with one extra which is the "Static Cheker" for the domain design (not the I/O).