使用数据注释进行 MVC 验证 - 模型类还是视图模型类?
将数据验证注释放入模型或视图模型中是最佳实践吗?一种方法相对于另一种方法的优点/缺点是什么?
我很想知道每个人都在哪里进行验证,我目前正在模型项目中进行验证。然而我看到一些人说这不是最佳实践。
Is it best practice to put data validation annotations in the Model or View Model? What are the advantages/disadvantages of one approach over the other?
Curious to see where everyone is putting their validation, I am currently doing it in the model project. However I have seen a few people say this is not best practice.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
就最佳实践而言,我想说:两者都不是。验证应该是分开的。 FluentValidation 等框架允许您将验证逻辑与模型完全分离。但为了回答你的问题,我会将验证放入视图模型中,因为这些是你将控制器操作绑定到的类。您还可以拥有多个绑定到同一模型但具有不同验证规则的视图模型。
As far as best practices is concerned I would say: in neither of them. Validation should be separate. Frameworks such as FluentValidation allow you to completely separate your validation logic from your models. But to answer your question I would put validation into View Models as those are the classes you are binding your controller actions to. You could also have multiple View Models that are tied to the same model but with different validation rules.
将注释放入视图模型中。
每个 DataModel 可以有多个 ViewModel,例如 DisplayModel、EditModel、ListModel .. 所有这些都可能需要不同的注释。
通常认为最好的做法是不要将 DataModel 直接暴露给视图,特别是在“POST”/编辑场景中。
我建议阅读 Brad Wilson 的精彩概述: http://bradwilson.typepad.com/blog/2009/10/aspnet-mvc-2-templates-part-1-introduction.html
这些文章主要介绍 MVC2 中 Dislpay 和 Edit 模板的使用,但清楚地说明了使用 ViewModel 模式的优点
Put your Annotations in your Viewmodel.
It is possible to have multiple ViewModels for each DataModel, eg DisplayModel, EditModel, ListModel .. all which may require different annotations.
It is generally considered best practice not to expose your DataModel directly to a view, espcicially in "POST"/Edit scenarios.
I suggest reading Brad Wilson's excellent overview at: http://bradwilson.typepad.com/blog/2009/10/aspnet-mvc-2-templates-part-1-introduction.html
These articles primarily cover the use of Dislpay and Edit templates within MVC2, but clearly illustrate the advantages of using the ViewModel pattern
如果您遵循单一责任,那么它可能应该进入它自己的组件中。话虽如此,如果您想走捷径,也可以在 ViewModel 中进行。但它绝对不应该出现在模型中。您的模型应该是“纯”数据。没有业务规则,验证就是业务规则。
If you follow single responsibility then it should probably go into it's own component. That being said, if you want to make a short cut it's okay to but it in the ViewModel. It definitely shouldn't go in the model though. Your model should be "pure" data. No business rules and validation is business rules.
我的意见是:这要看情况。
我通常在控制器和模型中控制输入,因此输入在控制器和模型中都是有效的。这是为了防止我想将模型绑定到另一种应用程序。说吧,WPF。
然而,很多人也采用“防御性编程”。这意味着函数(参数)的每个输入都会被检查。在这种情况下,一个输入可能会被检查几次,但您可以确保即使您更改了某些内容,验证逻辑仍然有效。
因此,对我来说,出现了几个问题:
1)是否有可能出现验证逻辑被绕过的情况。就像将模型绑定到 wpf 应用程序一样。
2) 我是否想通过检查每个函数的输入来牺牲性能而不是确保安全性?
对我来说这篇关于横切问题的文章也有帮助。
这些是我对此事的想法。希望有帮助
Well my opinion is: it depends.
I usually control my input in the controllers and the models, so the input is validate both in the controller and in the model. This is in case I want to tie the model to another sort of app. Say, WPF.
However, a lot of people also employ "defensive programming". This means that every input to a function (paramter) is checked. In this case one input may be checked a couple of times but you ensure that even if you change something, the validation logic holds.
So for me, a couple of questions arise:
1) Is there any chance that there can be a scenario where the validation logic is bypassed. Like tying the model to a wpf app.
2) Do I want to compromise performance over ensuring security by checking the input in every function?
For me this article on cross cutting concerns also helped.
Those are my thoughts on the matter. Hope it helps