MVC验证 - 保持干燥与服务层 - 什么是最好的做法? [英] MVC Validation - Keep it DRY with a service layer - What is best practice?

查看:100
本文介绍了MVC验证 - 保持干燥与服务层 - 什么是最好的做法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图坚持以最好的多层设计实践,不希望我的MVC控制器与我的DAL交互(或任何IRepository为此事)。它必须要经过我的业务服务层执行适当的业务规则和验证。验证 - 我不希望在我的域模型实体使用各种验证特性(如[必填]),因为这揭示了我的前端射在控制器中进行验证。且不说这种服务也可以通过一个WPF前端实现。

I am trying to adhere to best multi-layer design practices, and don't want my MVC controller to interact with my DAL (or any IRepository for that matter). It must go through my business service layer to enforce proper business rules and validation. Validation - I don't want to perform validation in the controller using the various validation attributes ( such as [Required]) on my domain model entities because this sheds light on my front end. Not to mention this service can also be implemented through a WPF front end.

由于我的验证正在我的服务层完成,什么是返回值返回到用户界面的最佳实践?我不想要一个无效addWhatever(INT somethingsID)',因为我需要知道,如果它失败了。它应该是一个布尔值?它应该是一个枚举?我应该利用的异常处理?或者我应该返回一些IValidationDictionary对象类似于使用MVC装饰时验证属性模型对象? (我可以在UI使用适配器模式以后如果需要的话)

Since my validation is being done in my service layer, what are best practices for returning values back to the UI? I don't want a 'void addWhatever(int somethingsID)', because I need to know if it failed. Should it be a boolean? Should it be a Enum? Should I take advantage of exception handling? Or should I return some IValidationDictionary object similar to that used by MVC when adorning validation attributes to Model objects? (which I could use an adapter pattern in the UI later if needs be)

我想从控制器到服务层通过我的实体,并了解验证/数据持久性与否失败。我也不想失去的事实,我需要返回一个视图指示每个可能已经失败的验证领域适当的错误消息视距(我想保持这一尽可能无痛)。

I would like to pass my entity from the controller to the service layer, and understand whether or not validation/data-persistence failed. I also don't want to lose sight on the fact that I need to return a view indicating the proper error messages for each field that may have failed validation (I'd like to keep this as painless as possible).

我有几个想法,觉得这样不对所有这一切。我觉得答案包括查看特定模型的实体,但是这将导致整个映射的问题,必须加以处理,更何况这违反了DRY(不要重复自己)原则。什么是最好的做法?

I have had several ideas, all of which don't feel right. I feel the answer includes View-specific-model entities, but this leads to a whole mapping issue that must be dealt with, not to mention this violates the DRY (Don't repeat yourself) principle. What is best practice?

推荐答案

我知道,好像做MVC验证违背了DRY原则,但在现实中..它不..至少不为大多数(非平凡)应用程序。

I know, it seems like doing MVC validation violates DRY, but in reality.. it doesn't.. at least not for most (non-trivial) applications.

为什么呢?因为你的观点的验证需求通常都是从你的业务不同对象验证要求。您的观点验证关注的是验证一个特定的观点是有效的,而不是你的商业模式是有效的。

Why? Because your view's validation requirements are quite often different from your business objects validation requirements. Your views validation concerns itself with validating that a specific view is valid, not that your business model is valid.

有时,这两个是相同的,但如果你建立你的应用程序,这样的观点需要的商业模式是有效的,那么你把自己锁进了这种情况。如果你需要创建对象分成两个页面,会发生什么?如果你决定使用服务层为Web服务,会发生什么?通过你的UI锁定在业务层的验证情况下,您severly削弱种解决方案,您可以提供的。

Sometimes those two are the same, but if you build your app so that the view requires the business model to be valid, then you are locking yourself into this scenario. What happens if you need to split object creation into two pages? What happens if you decide to use the service layer for a web service? By locking your UI into a business layer validation scenario you severly cripple the kinds of solutions you can provide.

视图是输入的验证,而不是模型的验证。

The view is validation of the input, not validation of the model.

这篇关于MVC验证 - 保持干燥与服务层 - 什么是最好的做法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆