在ASP.NET MVC中同时使用域对象和视图模型时避免验证逻辑重复的良好实践 [英] Good practices for avoiding validation logic duplication when working with both domain objects and view models in ASP.NET MVC

查看:108
本文介绍了在ASP.NET MVC中同时使用域对象和视图模型时避免验证逻辑重复的良好实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

常见情况:

出于演示目的,将层次域模型映射到平面视图模型.

Hierachical domain model is being mapped to flat view model for presentation purposes.

我在我的域中具有完整的验证设置,并且希望避免仅仅为了发现某些属性无效而将视图模型映射到域对象.我也不想在视图模型中重复验证逻辑.

I have a full validation setup in my domain and would like to avoid mapping view model to domain object just to find out that some property is invalid. Nor do I want to duplicate my validation logic in my view models.

这里有一些好的做法?

What are some good practices here?

我反对使用视图模型和域对象的接口,因为视图模型通常是线性且扁平的,而域对象通常是嵌套的并且具有许多其他数据类型的属性.

I'm against interfaces for both view models and domain objects, since view models are usually stringy and flat, whereas domain objects are often nested and have many other data types for properties.

我正在考虑一些可插拔的验证器,该验证器足够聪明,可以验证域对象和视图模型,但对实现有些怀疑.

I'm thinking about some pluggable validator that will be smart enough to validate both domain objects and view models but a bit skeptical about implementation.

但是为了简单起见,我倾向于这种方法:

But for simplicity I'm leaning towards this approach:

服务器端验证仅在域模型中进行;视图模型未通过验证,但是使用JavaScript在客户端上对数据进行了验证.因此,在大多数情况下,我的视图模型将是有效的,并且验证逻辑将停留在一个地方,并且只会在域模型中发生.这种方法的缺点是asp.net mvc 2验证将无法支持它. 你觉得呢?

Server side validation happens only in domain model; view models are not validated, but data is validated on the client with JavaScript. So in most cases my view models will be valid and validation logic will stay in one place and will occur only in domain model. This approach has a drawback that asp.net mvc 2 validation will not be able to support it. What do you think?

谢谢.

推荐答案

Microsoft Enterprise Library提供了这样的可插入验证"库,该库基于泛型(如果提供内存).

The Microsoft Enterprise Library offers such a 'Pluggable Validation' library which is based on generics if memory serves.

我不喜欢它的工作方式,更不用说对其他EL组件的依赖数量了,但是仍然值得一看.

I didn't like the way that it worked, not to mention the number of dependencies on other EL components but it still might be worth a look.

我绝不是该领域的专家,但是我坚信,在提交存储库之前,应该先验证数据,然后再进行处理,并且始终进行验证.因此,验证逻辑的最佳位置是业务逻辑层.客户端验证虽然使用户感到不悦,但却使代码维护成为一场噩梦,并且还导致代码重复,这可能会导致进一步的问题.

I'm by no means an expert in this field but I firmly believe that data should be validated just before you want to do something with it and ALWAYS before commiting to your repository. Therefore, the best place for your Validation Logic is your Business Logic Layer. Client Side validation, although pleasent for the user, makes code maintainance a nightmare and also results in code duplication which can cause problems further down the line.

在我看来,您的演示对象应该是DTO的对象.当用户将数据提交回您的控制器(和BLL)时,您可以使用解析器将演示对象转换为业务对象,然后可以对其进行验证.

Your presentation objects should, in my opinion, be DTO's. When a user submits data back to your controllers (and BLL) you can use parsers to transform the presentation objects into business objects which can then be validated.

我建议您阅读Rudy Lacovara的博客(又名The Angry .NET Developer).他是一个非常谦虚的家伙,他对这类事情有很多好话要说.

I reccommend having a read of Rudy Lacovara's blog (aka The Angry .NET Developer). He's a very modest bloke with a lot of good stuff to say on things like this.

HTH

这篇关于在ASP.NET MVC中同时使用域对象和视图模型时避免验证逻辑重复的良好实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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