ASP.NET MVC如果把自定义的验证属性 [英] ASP.NET MVC Where to put custom validation attributes

查看:65
本文介绍了ASP.NET MVC如果把自定义的验证属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在摆弄一些ASP.NET MVC3解决方案结构和我已经是由以下项目的设计解决:

I've been fiddling with some ASP.NET MVC3 solution structures and I have settled on a design that is made up of the following projects:

MyApp.Web -  MVC3 Web Layer
MyApp.Data - Repositories and infrastructure for managing data
MyApp.Domain - POCO Entities
MyApp.Service - Service layer through with I manipulate the model
MyApp.Test - doh

你会在哪里把你的自定义验证属​​性类?就我而言,他们应该在域项目中去,因为它们是针对生活在那里的实体类,但随后我就需要引用一些dll文件,如System.Web.MVC的,我本来希望已在该项目保持引用到最低限度。思考?

Where would you put your custom validation attribute classes? As far as I'm concerned they should go in the Domain project because they are specific to the entity classes that live there, but then I would need to reference a number of dlls such as System.Web.MVC and I would have liked to have kept references in that project to a bare minimum. Thoughts?

更新:

我删除从我的域名项目的所有属性和创建的视图模型来包装他们的Web项目。
把我所有自定义验证类的文件夹中的Web项目。谢谢你们

I removed all attributes from my Domain project and created view models to wrap them in the Web project. Placed all my custom validation classes in a folder in the web project. Thanks guys

推荐答案

您并不需要引用网络的DLL。只有 System.ComponentModel.DataAnnotations

You do not need to reference the web dlls. Only System.ComponentModel.DataAnnotations

把验证属性,它们都use.d

Put the validation attributes where they are use.d

为什么不呢?几乎每一个我见过的例子在视图使用波苏斯。有什么替代方案?创建我的波苏斯某种包装类的?

Why not? Almost every example I've seen uses POCOs in views. What's the alternative? Creating some kind of wrapper classes for my POCOs?

由于的:

安全

假设你有一个用户POCO与 IsAdmin 字段。通过使用POCO,如果它被黑客因此谁获得管理员访问张贴在MVC中的模型绑定器将设置该属性。

Let's say that you got a User POCO with an IsAdmin field. By using the POCO, the ModelBinder in MVC will set that property if it's POSTed by a "hacker" who therefore gain admin access.

隔离

在您的视图使用POCO让您查看间接依赖你的数据库。当然,波苏斯都应该删除的依赖。但在现实中,他们很少这样做。

Using the POCO in your view makes your View indirectly dependent of your database. Sure, POCOs are supposed to remove that dependency. But in reality they seldom do.

自定义

您也很少显示在视图中的所有值,因为它们是存储在数据库中。

You do seldom show all values in a view as they are stored in the db.

在code相适应的模式不应该在视图,但在视图模型。使用尽可能少的逻辑尽量在你的看法。

The code to adapt a model should not be in the view, but in the viewmodel. Use as little logic as possible in your views.

这篇关于ASP.NET MVC如果把自定义的验证属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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