其中,将数据管理规则在ASP.NET MVC复杂的数据验证? [英] Where to put data management rules for complex data validation in ASP.NET MVC?

查看:110
本文介绍了其中,将数据管理规则在ASP.NET MVC复杂的数据验证?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前工作的一个ASP.NET MVC2项目。这是我第一次对一个真正的MVC Web应用程序的工作。该 ASP.NET MVC网站真的帮我上手非常快,但我仍然有关于一些模糊认识数据模型的验证。

I am currently working on an ASP.NET MVC2 project. This is the first time I am working on a real MVC web application. The ASP.NET MVC website really helped me to get started really fast, but I still have some obscure knowledge concerning datamodel validation.

我的问题是,我真的不知道,当涉及到复杂的验证规则的地方来管理我充满数据模型。例如,验证字符串字段用正则表达式是很容易的,我知道,我只是来装饰我的领域具有特定属性的,因此数据管理规则在模型中实现的。但是,如果我有需要正确设置多个领域,我需要验证其彼此,例如多个日期时间之后的具体时间的规则,我在哪里需要验证呢?我知道我可以创建自己的验证属性,但有时验证问这是复杂的使用属性验证一个特定的验证路径。

My problem is that I do not really know where to manage my filled datamodel when it comes to complex validation rules. For example, validating a string field with a Regex is quite easy and I know that I just have to decorate my field with a specific attribute, so data management rules are implemented in the model. But if I have multiple fields that I need to validate which each other, for example multiple datetime that need to be correctly set following a specific time rule, where do I need to validate them? I know that I could create my own validation attributes, but sometimes validation ask a specific validation path which is to complex to be validated using attributes.

这第一个问题也导致我一个相关的问题是,是不是验证控制器模型?因为这是我找到了复杂的验证的唯一途径的时刻。但我觉得这有点脏,我觉得它并没有真正适合的控制器角色,更难测试(多code路径)。

This first question also leads me to a related question which is, is it right to validate a model in the controller? Because for the moment that is the only way I found for complex validation. But I find this a bit dirty and I feel it does not really fit a the controller role and much harder to test (multiple code path).

感谢。

注:我得到了一些pretty良好的解决方案在这里,但我在等待其他的想法和一些最佳实践解决方案

NB: I got some pretty good solutions here but I am waiting for other ideas and some "best practice" solution".

推荐答案

兆丰杜佩。兆丰主观的。在哪里以及如何与MVC验证的说法已经被殴打致死,没有拿出一个直接的答案。这是如此主观的,哲学的,以每个开发人员/店,其几乎是不可能的大家就任何问题达成共识。

Mega Dupe. Mega Subjective. The "where and how to validate with MVC" argument has been beaten to death without coming up with a straight answer. This is so subjective and philosophical to each developer/shop that its almost impossible for everybody to agree on anything.

的另一个问题是,即使在验证工具来在一堆的形状和大小的,并且可以在不同的范围和层起作用。其近乎癫狂的各种验证工具。如何做,如果(someString!=)得到这么难? ;)

The other issue is even the validation tooling comes in a bunch of shapes and sizes and can function in different scopes and layers. Its almost insane the variety in validation tooling. How did if( someString != "" ) get so hard? ;)

如果你读了这些其他的答案,你很快就会发现有​​没有最好的做法在所有。一旦你进入领域驱动设计的校长和无效状态的讨论和对象,你会发现的讨论变得更加复杂。

If you read these other answers you'll quickly find there is no best practice at all. Once you get into Domain Driven Design principals and the discussion on invalid state and objects you'll find the discussion gets even more complicated.

<一个href=\"http://stackoverflow.com/questions/134388/where-do-you-do-your-validation-model-controller-or-view\">http://stackoverflow.com/questions/134388/where-do-you-do-your-validation-model-controller-or-view

<一个href=\"http://stackoverflow.com/questions/2571569/asp-net-mvc-2-validation-using-dtos-instead-of-domain-entities\">http://stackoverflow.com/questions/2571569/asp-net-mvc-2-validation-using-dtos-instead-of-domain-entities

<一个href=\"http://stackoverflow.com/questions/2181940/does-asp-net-mvc-2-validation-need-some-more-thought-in-terms-of-patterns-and-use\">http://stackoverflow.com/questions/2181940/does-asp-net-mvc-2-validation-need-some-more-thought-in-terms-of-patterns-and-use

<一个href=\"http://stackoverflow.com/questions/2075288/\">http://stackoverflow.com/questions/2075288/

<一个href=\"http://stackoverflow.com/questions/1553838/which-validation-library-for-asp-net-mvc\">http://stackoverflow.com/questions/1553838/which-validation-library-for-asp-net-mvc

<一个href=\"http://stackoverflow.com/questions/294890/asp-net-mvc-user-input-and-service-repository-where-to-do-validation\">http://stackoverflow.com/questions/294890/asp-net-mvc-user-input-and-service-repository-where-to-do-validation

<一个href=\"http://stackoverflow.com/questions/1558457/asp-net-mvc-is-data-annotation-validation-enough\">http://stackoverflow.com/questions/1558457/asp-net-mvc-is-data-annotation-validation-enough

<一个href=\"http://stackoverflow.com/questions/25675/mvc-where-to-implement-form-validation-server-side\">http://stackoverflow.com/questions/25675/mvc-where-to-implement-form-validation-server-side

<一个href=\"http://stackoverflow.com/questions/1531388/asp-net-mvc-validation\">http://stackoverflow.com/questions/1531388/asp-net-mvc-validation

DDD:

<一个href=\"http://stackoverflow.com/questions/516615/validation-in-a-domain-driven-design\">http://stackoverflow.com/questions/516615/validation-in-a-domain-driven-design

这篇关于其中,将数据管理规则在ASP.NET MVC复杂的数据验证?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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