MVC 3和干定制验证 [英] MVC 3 and DRY custom validation

查看:146
本文介绍了MVC 3和干定制验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

除非我失去了一些东西(这是很可能的),但在我看来,自定义验证一直违反干燥。在我见过的所有例子,即使推出了W / MVC 3全新的不显眼的客户端验证,我们必须创建.NET code为我们的服务器端验证,和jQuery(或JavaScript code)对于客户端验证。

Unless I'm missing something (which is very possible), it seems to me that custom validation has always violated DRY. In all the examples I've seen, even with the brand new Unobtrusive Client Validation introduced w/ MVC 3, we have to create .NET code for our server-side validation, and jQuery (or JavaScript code) for client-side validation.

据我所知,有没有这样的东西作为一个.NET到jQuery的翻译,便利DRY服务器/客户端验证,我想这将是有工作的服务器和客户端实干验证的唯一途径。

I understand that there's no such thing as a .NET-to-jQuery translator that would facilitate DRY server/client validation, and I guess that would be the only way to have true DRY validation that works both server and client side.

不过,我会与具有总是在服务器上执行自定义验证心满意足。传递到自定义验证(在我的情况)所需要的数据通常仅限于一个或两个字段,而服务器端逻辑通常是pretty快,即使它打数据库。

But I would be perfectly content with having custom validation always performed on the server. The data needed to pass into custom validation (in my case) is usually limited to one or two fields, and the server-side logic is usually pretty quick, even if it has to hit the database.

有没有使用属性连接最多的自定义验证,然后让你的客户端验证使用Ajax来执行验证服务器端和客户端没有响应机制,开箱即用的?或者说,有一个人想出了这样的解决方案?

Is there no OOTB mechanism for wiring up custom validation using attributes, then having your client side validation use Ajax to execute the validation server-side and respond to the client? Or, has someone come up with such a solution?

或者是它的,到最后,重复自定义验证的权衡比推出W /总是执行自定义的验证服务器端?这些问题更好的事情

Or is it a matter of, in the end, the tradeoffs of repeating the custom validation is better than the issues introduced w/ always executing custom validation server side?

先谢谢了。

推荐答案

开箱即用的:的http://msdn.microsoft.com/en-us/library/system.web.mvc.remoteattribute(v=vs.98).aspx

您是不是真的验证DRY在这里。 DRY的概念是一个有点超过code的简单重复更细致。有可接受的折衷,特别是当耦合关切考虑

You aren't really validating DRY here. The concept of DRY is a bit more nuanced than simple duplication of code. There are acceptable tradeoffs, especially when coupling concerns are taken into account.

当有人问这个问题,这是相当频繁,如果你搜索周围,我通常会转介他们到的DDD概念有界的概念的。使用 [遥控] ,并迫使DRY当谈到验证往往在一个地方和合并若干层的职责成一团吨的担忧。业务逻辑与持久性和数据完整性逻辑(非空值)。

When people ask this question, which is quite often if you search around, I usually refer them to the DDD concept of bounded concepts. Using [Remote] and forcing DRY when it comes to validation tends to bunch up tons of concerns in one place and merging the responsibilities of several layers. Business Logic vs. Persistence and Data Integrity Logic ( non nulls ).

@Darin德米特罗夫表示,pretty好了很多他的答案,他对这一问题的确切做。验证一个必填字段填入为确保莎莉有足够的信贷来进行购买,应该多少区别对待多大区别。

@Darin Dmitrov says it pretty well in a lot of his answers he's made to this exact question. Validating that a required field is filled in is much different from making sure Sally has enough credit to make a purchase and should be treated much differently.

客户端验证最适合用于基本的关切,而不是更重的操作超载。从客户端验证可用性的影响是最小化的。没有什么不可用关于提交表单,等待刷新。这是更流畅,但没有什么,这将使或打破您的应用程序的成功。

Client validation is best used for basic concerns and not be overloaded with more heavy operations. The impact on usability from client validation is really minimal. There is nothing "unusable" about posting a form and waiting for a refresh. It is more fluent but not anything that will make or break the success of your application.

这篇关于MVC 3和干定制验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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