简体   繁体   English

在 .NET CORE ZC6E190B284633C48E190B284633C48E39E55049DA3CCE8Z ZDB974278714CA8ADE4FZA8 中对域 model 实体进行服务器端验证的最佳位置/方式是什么?

[英]What is the best place/way for server-side validation of domain model entities in .NET CORE Web API?

I have Domain model for a variety of objects.我有各种对象的域 model。 When I insert/update/delete entities through WebApi Call, I want a pre-validation of entities before that.当我通过 WebApi 调用插入/更新/删除实体时,我想要在此之前对实体进行预验证。 Like For insert, if the email property of employee object already exists then throw error, similarly, for Employee object,I have other multiple Database validation to perform before I can actually perform Insert.与插入一样,如果员工 object 的 email 属性已经存在,则抛出错误,同样,对于员工 object,我还有其他多个数据库验证要执行,然后才能实际执行插入。 Same goes for Update and Delete operation which has some common and some different validation to perform.Eg: For update of email email address,I will do a database call to check if employee really exists.If not then throw error.So how do I know if my operation if Insert/Update/Delete operation and then call appropriate validation methods?更新和删除操作也是如此,它有一些共同的和一些不同的验证要执行。例如:对于 email email 地址的更新,我将进行数据库调用以检查员工是否真的存在。如果不存在则抛出错误。那我该怎么做是否知道我的操作是否插入/更新/删除操作然后调用适当的验证方法?

I cannot make Db Call from Domain model since I don't have reference to Data Access Layer from this project.I understand that for normal field validation like range/invalid type,I can use IValidatable in my domain model.But how do I do multiple database calls.I have a service layer which currently make Db Calls for few validations for general purpose for all domain models.But how do I separate out Db Calls for each domain models?我无法从域 model 进行 Db 调用,因为我没有从这个项目中引用数据访问层。我知道对于像范围/无效类型这样的正常字段验证,我可以在我的域 model 中使用 IValidatable。但是我该怎么做多个数据库调用。我有一个服务层,目前为所有域模型的通用目的进行 Db 调用进行少量验证。但是如何分离每个域模型的 Db 调用?

If only a single service modifies your domain model (like a MyDomainModelService ), you can perform all validation checks there.如果只有一个服务修改了您的域 model(如MyDomainModelService ),您可以在那里执行所有验证检查。

If this is not the case, you can create a service which checks the validity of your domain model, and use it in the places where you need it:如果不是这种情况,您可以创建一个服务来检查您的域 model 的有效性,并在您需要的地方使用它:

IValidationService<MyDomainModel> _validationService;
// ...
if (!_validationService.IsValid(model)) { throw new Exception("invalid model"); }

While it is possible to perform validation on a database level in IValidatable, it is not recommended as you need to request access to your database context by "hidden" means.虽然可以在 IValidatable 中对数据库级别执行验证,但不建议这样做,因为您需要通过“隐藏”方式请求访问数据库上下文。 Apart from that, the validation method is not async.除此之外,验证方法不是异步的。

Another possibility is using validation attributes, in particular the [Remote] attribute.另一种可能性是使用验证属性,特别是[Remote]属性。 However, this approach violates some principles if you are following a clean architecture sort of style.但是,如果您遵循一种简洁的架构风格,那么这种方法会违反一些原则。 I still wanted to mention it because in a simple application it is certainly worth considering.我仍然想提及它,因为在一个简单的应用程序中它当然值得考虑。

Apart from that, you should reflect on whether your conditions should be part of the validation.除此之外,您应该考虑您的条件是否应该成为验证的一部分。 You can make an email unique by database means or - depending on the ORM you are using - it fails (automatically) if an update is carried out on an entity / a record which does not exist.您可以通过数据库方式使 email 唯一,或者 - 根据您使用的 ORM - 如果对不存在的实体/记录执行更新,它会(自动)失败。

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

相关问题 .NET Core Web API模型验证 - .NET Core Web API model validation 用于客户端和服务器端验证的 ASP.NET Core 重用代码 - ASP.NET Core Reuse Code for Client-Side and Server-Side Validation 如何在 ASP.NET Core 3.1 MVC 中进行RequiredIf 客户端和服务器端验证? - How to make RequiredIf Client-side and server-side validation in ASP.NET Core 3.1 MVC? 在.NET中设计用于Web API的相关业务实体的最佳方法是什么? - What is the best way to design the related business entities in .NET to be used for Web API? 动态绕过服务器端模型验证ASP.NET MVC - Bypass server-side model validation dynamically ASP.NET MVC 保留用户控件值而不必回发或编写服务器端代码的最佳方法是什么? - What is the best way to persist the usercontrol values without having to postback or to write server-side code? 在 Blazor 服务器端应用程序中处理不同浏览器功能的最佳方法是什么? - What is the best way to handle different browser features within a Blazor Server-Side Application? ASP.NET 核心托管和服务器端 Blazor 有什么区别,真的吗? - What's the difference between ASP.NET Core Hosted and Server-Side Blazor, really? 服务器端 Blazor 中单个 EditForm 中的多个模型验证 - Multiple Model validation in single EditForm in Server-Side Blazor ASP.NET MVC - 不需要的服务器端验证正在触发 - ASP.NET MVC - Unrequired server-side validation is firing
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM