简体   繁体   English

WCF实体验证的最佳做法

[英]Best Practice for WCF Entity Validation

I'm currently creating a WCF Service and this is just a read-only service, meaning there's no write access allowed on the service. 我当前正在创建WCF服务,这只是一个只读服务,这意味着该服务上不允许有写访问权限。 The WCF Service will use the Employee entity (below) as data object as i'm using entity framework. WCF服务将使用Employee实体(如下)作为数据对象,就像我使用实体框架一样。 My question is, what would be the best practice for handling the validation for Employee entity? 我的问题是,处理E​​mployee实体验证的最佳实践是什么? Should I use dataannotation? 我应该使用数据注释吗? or should i let the entity framework just throw the error? 还是我应该让实体框架抛出错误?

Does WCF Service entities perform validation,when the request is proxied into the Entity Framework? 将请求代理到实体框架后,WCF服务实体是否执行验证?

public class Employee{

   public int EmployeeId {get; set}
   public string EmployeeName{get; set}
} 

Yes you can use data annotation to verify that your input respects your domain, like for example verify that mandatory fields are valorized and that data type and format is correct, etc.. You can add also validation method to the data contract model to check for example if some fields must respect a particular range of data. 是的,您可以使用数据注释来验证您的输入是否尊重您的域,例如,验证必填字段是否为有效值以及数据类型和格式正确等。您还可以向数据合同模型添加验证方法以检查例如某些字段必须遵守特定范围的数据。

This kind of validation can be usefull because it can reduce the number of database access, but if you need to check the existence of the Employee in your database you need to let Entity Framework try to get the entity, so when you need to perform validation that can be executed only involving data in database there is no alternatives to execute some queries. 这种验证很有用,因为它可以减少数据库访问的次数,但是如果您需要检查数据库中Employee的存在,则需要让Entity Framework尝试获取该实体,因此当您需要执行验证时只能执行涉及数据库中数据的操作,没有其他选择可以执行某些查询。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM