MVC 验证 - 使用服务层保持干燥 - 最佳实践是什么? [英] MVC Validation - Keep it DRY with a service layer - What is best practice?

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

问题描述

我正在努力遵循最佳的多层设计实践,并且不希望我的 MVC 控制器与我的 DAL(或任何与此相关的 IRepository)进行交互.它必须通过我的业务服务层来强制执行正确的业务规则和验证.验证 - 我不想在我的域模型实体上使用各种验证属性(例如 [Required])在控制器中执行验证,因为这对我的前端有帮助.更何况这个服务也可以通过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.

由于我的验证是在我的服务层中完成的,将值返回给 UI 的最佳做法是什么?我不想要'void addWhatever(int somethingsID)',因为我需要知道它是否失败.它应该是一个布尔值吗?它应该是一个枚举吗?我应该利用异常处理吗?或者我应该返回一些类似于 MVC 在将验证属性装饰到模型对象时使用的 IValidationDictionary 对象?(如果需要,我可以稍后在 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 锁定在业务层验证场景中,您会严重削弱您可以提供的各种解决方案.

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天全站免登陆