为什么使用而不是ASP.NET MVC的验证fluentvalidation [英] Why use fluentvalidation instead of ASP.NET MVC validation

查看:115
本文介绍了为什么使用而不是ASP.NET MVC的验证fluentvalidation的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在何种情况下,你会选择过的 FluentValidation(FV) ://devtrends.co.uk/blog/the-complete-guide-to-validation-in-asp.net-mvc-3-part-1相对=nofollow> ASP.NET MVC 3路?

In which circumstances would you choose FluentValidation (FV) over the ASP.NET MVC 3 way?

什么是FV过MVC的优势是什么?我认识到,与后者,我们必须写更多的code和可乱丢code。与数据注释。此外,它似乎更容易编写使用FV比MVC自定义验证。然而,随着MVC它有可能使使用数据注解,并在塞jQuery验证

What are the advantages of FV over MVC? I realise that with the latter we have to write much more code and can litter the code with Data Annotations. Moreover, it would seem to be easier to write custom validation using FV than MVC. However, with MVC it is possible to make use the data annotation and plug jQuery validation in.

那么,在你看来会让你选择一个比其他?是否有情况,你甚至会同时使用?

So what in your view would make you choose one over the other? Are there circumstances where you would even use both?

推荐答案

流利的验证是建立专门的验证对象,当你想要把验证逻辑从业务逻辑分离,你会使用的一种方式。面向方面编程(AOP)范例实现的横切关注点分离系统内和验证就是这样的一个问题。分离验证可以帮助清理你的域名code,使其更具凝聚力,以及给你一个单一的地方去寻找验证逻辑。

Fluent validation is one way of setting up dedicated validator objects, which you would use when you want to treat validation logic as separate from business logic. The aspect-oriented programming (AOP) paradigm enables separation of cross-cutting concerns within a system, and validation is one such concern. Separating validation helps clean up your domain code and make it more cohesive, as well as giving you a single place to look for validation logic.

MVC注解驱动的验证是一个很便宜的方式来获得一些基本的验证到一个应用程序,而不去创建专用的验证对象的麻烦,创造它组织他们一个验证系统,并插上在一起。这很容易建立,但可以让你的域对象不干净。

MVC annotation-driven validation is a very 'cheap' way to get some basic validation into an application, without going to the hassle of creating dedicated validator objects, creating a validation system which organizes them and plugging it all together. It's very easy to set up, but can make your domain objects less clean.

有关,所有的验证逻辑可以使用注解来处理小系统,我只想使用注释,因为他们是那么容易设置建议。对于更大,更复杂的系统,我会建议使用验证器对象分开的验证问题。

For small systems where all the validation logic can be handled using annotations, I would recommend just using annotations, because they're so easy to set up. For larger, more complex systems, I would recommend separating the validation concern using validator objects.

我个人喜欢用这两种方法:添加验证属性视图模型类(这意味着注释不要弄乱我的域对象),以及具有我的域名层内专用的验证对象。这是重复一个小数目,但使用​​注释是如此快捷方便,我觉得很值得额外的维护费用。

I personally like to use both approaches: add validation attributes to ViewModel classes (which means the annotations don't clutter up my domain objects), as well as having dedicated validator objects within my domain layer. This is a small amount of duplication, but using annotations is so quick and easy, I find it worth the extra maintenance cost.

这篇关于为什么使用而不是ASP.NET MVC的验证fluentvalidation的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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