简体   繁体   English

DTO上的ASP.Net Web API验证属性?

[英]ASP.Net Web API Validation Attributes on DTO?

I'm using ASP.Net Web API and the Code First Entity Framework and from what I've read you should typically be exposing DTO objects rather than the entity objects directly in your action methods (according to http://www.asp.net/web-api/overview/data/using-web-api-with-entity-framework/part-5 ). 我正在使用ASP.Net Web API和Code First Entity Framework,从我读过的内容中,您通常应该直接在您的操作方法中公开DTO对象而不是实体对象(根据http://www.asp。 net / web-api / overview / data / using-web-api-with-entity-framework / part-5 )。

So in one case I'm working on, to avoid the problem of "over-posting" as described in the link above I've created a DTO object with almost all the same properties as the model object. 所以在我正在研究的一个案例中,为了避免上面链接中描述的“过度发布”的问题,我创建了一个DTO对象,它几乎具有与模型对象相同的所有属性。 What I'm wondering however is do I need to duplicate all the same sets of validation attributes (eg. [Required], [Range(N,M)], etc. for both the DTO and model properties? Initially I was hoping not to (to avoid duplication) but you need the validation attributes on the DTO if you want to leverage the binding validation (ie. ModelState.IsValid) and on the main model if you want your database to be created with the appropriate constraints ([Required] -> not null, etc.) 我想知道的是,我是否需要为DTO和模型属性复制所有相同的验证属性集(例如[必需],[范围(N,M)]等?最初我希望不是to(避免重复)但如果你想利用绑定验证(即ModelState.IsValid),你需要DTO上的验证属性,如果你想用适当的约束创建数据库,你需要在主模型上([必需] ] - > not null等)

Is there no better way? 有没有更好的方法?

Also, are there some attributes that Entity does use but the model binding validation does not use? 此外,是否有一些Entity确实使用的属性,但模型绑定验证不使用? For example, while [Range(n,m)] will clearly affect validation on some client input, does entity care about it at all (it doesn't seem to affect the created DB schema from what I can tell?) 例如,虽然[Range(n,m)]将明显影响某些客户端输入的验证,但实体是否完全关心它(它似乎不会影响我所知道的创建的DB模式?)

The entities should only have the attributes which are actually having an impact on the database. 实体应该只具有实际对数据库产生影响的属性。 The DTOs should not have any attributes for validation except for the DataMemberAttribute to define if a property is required and in which order it should be displayed etc. For OData you also have to set the KeyAttribute. DTO不应具有任何验证属性,除了DataMemberAttribute以定义是否需要属性以及它应以何种顺序显示等。对于OData,您还必须设置KeyAttribute。 The models should have the validation attributes. 模型应具有验证属性。 Because the DTOs and the models are probably almost identical you create for each dto which needs to be validated a model and just swap the dto's value to the model's object. 因为DTO和模型可能几乎相同,所以您需要为每个需要验证模型的dto创建,并将dto的值交换到模型的对象。 Now you can validate it, if you are not using ValidationAttributes for the models you can validate them for example with FluentValidation 现在您可以验证它,如果您没有对模型使用ValidationAttributes,您可以验证它们,例如使用FluentValidation

So long story short: 长话短说:

  • Entities only get the attributes which actually impact the database's schema 实体仅获取实际影响数据库模式的属性

  • DTOs are simple objects with no validation logic, except for the DataMemberAttribute DTO是简单的对象,除了DataMemberAttribute之外没有验证逻辑

  • Models should have the validation attributes (only if needed, not needed when using FluentValidation) 模型应具有验证属性(仅在需要时,在使用FluentValidation时不需要)

The workflow for POST will be: -> DTO comes in -> Swap dto to Model -> Validate the model -> Swap model to entity -> Store entity in database -> Use the updated entity and swap it to a new dto -> Return the dto POST的工作流程将是: - > DTO进入 - >交换到模型 - >验证模型 - >交换模型到实体 - >在数据库中存储实体 - >使用更新的实体并将其交换到新的dto - >返回dto

Greetings, Voltaic 问候,Voltaic

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ASP.NET 核心 Web API - 如何验证 map 的两个实体 - ASP.NET Core Web API - How to map two entities to a single DTO for validation 如何在DTO和ASP.NET Web API和Unity中使用接口? - How to use interfaces in DTO with ASP.NET Web API and Unity? 我需要在 ASP.Net 核心的验证属性中返回自定义验证结果(响应) Web API - I need to return customized validation result (response) in validation attributes in ASP.Net core Web API ASP.NET Web API十进制验证 - ASP.NET Web API decimal validation 我试图使用DTO返回Web api2 asp.net中的相关数据 - Im trying to return related data in web api2 asp.net using DTO 如何在单独的 DTO 程序集中使用带有数据注释的本地化 (Asp.net Core Web Api) - How to use localization with data annotation in a separate DTO Assembly (Asp.net Core Web Api) ASP.NET Web API如何基于put / get / post / delete从DTO对象中排除属性 - ASP.NET web api how to exclude property from DTO object based on put/get/post/delete ASP.NET Web API OData-将DTO查询转换为实体查询 - ASP.NET Web API OData - Translating DTO queries into Entity queries ASP.NET Web API 从具有接口类型的主体中获取 dto - ASP.NET Web API get dto from body with interface type ASP.NET将多个DTO转换为一个模型并在DTO内进行验证 - ASP.NET Multiple DTOs to one Model and validation inside DTO
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM