流畅的验证、域和视图模型
我一直在使用 asp.net mvc、nhibernate 和 ddd 概念开发一个 Web 应用程序。
我已经使用 Fluent Validation 为我的域类开发了验证,并且效果很好。好吧,现在,我需要一个 ViewModel 来编辑视图中的实体,所以,我的问题是,我是否需要创建另一个验证类来验证我的视图模型?或者我应该怎么做才能解决这种情况?
我问这个问题是因为我不想破坏 DRY(不要重复自己)的概念。
谢谢!
I've been developing a web application with asp.net mvc, nhibernate and ddd concepts.
I've developed validations with Fluent Validation for my domain classes and it works fine. Well, now, I need a ViewModel to edit an entity in a View, soo, my question is, Do I need to create another validation class to validate my viewmodel? Or what should I do to get around this situation?
I ask it because I don't want to broke the DRY (don't repeat yourself) concetp.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
恕我直言,域级验证和视图模型验证有很大不同(尽管它们可能有很多重叠)。
例如,数据库中的某个字段可能完全可以为空,但需要在某些 Web 表单上输入它。在这种情况下,您将在模型验证中检查 null。
多个客户端应用程序共享相同的域控制器(例如通过 WCF)但拥有不同的应用程序验证逻辑也是很正常的。
如果您在视图模型中使用 DataAnnotations,则可以免费获得客户端 javascript 验证,因此作为一般规则,我总是从我的 Domain 对象中拥有一个单独的 ViewModel,即使它是 1:1 映射 - 我只是使用 AutoMapper 来他们之间进行翻译。除了获得客户端验证之外,它还减少了域验证中的混乱。
Domain level validation, and View-Model validation are quite different imho (although they can have lots of overlap).
For instance, it may be perfectly allowable to have a certain field as null in your database, but require it's input on certain webforms. In this case you would check for null within the Model validation.
It would also be quite normal for multiple client applications to share the same Domain controllers (via WCF for example), but to possess different application validation logic.
If you use DataAnnotations in your view model you can get client-side javascript validation for free, so as a general rule, I always have a separate ViewModel from my Domain objects, even if it's a 1:1 mapping - I just use AutoMapper to translate between them. In addition to getting the client-side validation, it also reduces the clutter within the Domain validation.