流畅的验证与数据注释
当用于 ASP.NET MVC 验证时,这两个验证包之间有哪些操作差异?他们似乎拥有相似的对象,甚至连对象名称都相似。一个与另一个相关吗?他们有什么区别?这些差异以什么方式表示不同的用例?
What are the operative differences between these two validation packages when used for ASP.NET MVC validatation? They seem to have similar objects, all the way to their object names. Is one related to another? What are their differences? In what way do these differences denote different use cases?
- Fluent Validation (3rd party solution)
- Data annotations (Microsoft "baked-in")
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我更喜欢Fluent Validation:
I prefer Fluent Validation:
注意:在评论中,原作者表示他现在更喜欢流畅的验证,
我显然更喜欢数据注释,因为......
NB : In the comments the original author states he now prefers fluent validation
I clearly prefer Data Annotations because ...
根据我的观点,FluentValidation 比数据注释更好,
首先,与数据注释相比,单元测试和配置复杂的验证规则更容易。
还有一点是这个
虽然这是一个有争议的问题,但我认为数据注释不符合坚实的原则
According to my point of view, FluentValidation is better than Data Annotation
Firstly, Unit testing and configuring complex validation rules are easier compared to Data Annotations.
Another point is this
Although this is a controversial issue, I think dataannotation does not comply with the solid principles
关于验证类与属性的一个考虑因素是,可以有一个类根据上下文使用不同的验证。例如,地址类可以在两种不同的情况下使用:
您可以使用相同的类,但验证会不同。例如,对于人口统计数据,您可能只需要国家/地区、州/省或城市,而不需要实际的街道地址。使用属性进行这种类型的验证会很困难。
我知道这是一个令人费解的例子,但它确实出现在我的项目中(可能是 1% 或 2% 的时间),值得考虑。
One consideration regarding a validation class vs. attributes, it is possible to have a class that uses different validations, depending on context. For example, an address class can be used in two different instances:
You could use the same class, but the validation would be different. For demographics for example, you might only require the country or state or city, but not the actual street address. Doing this type of validation would be difficult using attributes.
I know this is a convoluted example, but it does come up in my projects (perhaps 1 or 2 percent of the time) to warrant consideration.