简体   繁体   English

在Web Api中更新实体的最佳实践

[英]Best Practice For Updating Entity in Web Api

I'm researching best practice for updating entity from action that called by client. 我正在研究从客户要求的操作更新实体的最佳做法。 There are several ways to do that but none of them seem best practice. 有几种方法可以做到这一点,但似乎没有最佳实践。

1- Getting datas that will be updated via reflection from request model and update entity with these properties. 1-从请求模型中获取将通过反射更新的数据,并使用这些属性更新实体。 But reflection doesn't recommended to use in web api. 但是不建议在Web API中使用反射。

2- Sending all datas of entity to client and getting it's updated version from request. 2-将实体的所有数据发送到客户端,并从请求中获取其更新版本。 It seems make unnecessary traffic. 似乎造成不必要的流量。

3- Getting datas that will be updated and check them with if else conditions for getting which ones changed. 3-获取将要更新的数据,并检查是否有其他条件更改获取哪些数据。 It's so basic and not generic, seems so unprofessionally. 它是如此基础而不是通用,似乎如此不专业。

Request Model that i talked about is clone of entity model. 我所说的请求模型是实体模型的克隆。

First off, don't use Reflection. 首先,不要使用反射。 It's slow as hell and makes your code extra fragile. 它像地狱一样缓慢,并使您的代码更加脆弱。

When it comes to EF, usually there are 3 possible solutions: 对于EF,通常有3种可能的解决方案:

1; 1; The client sends the whole updated entity, and only the updated entity. 客户端发送整个更新的实体,并且仅发送更新的实体。 In this case, you simply have to attach the entity to the corresponding entityset and mark the entity state as Modified. 在这种情况下,您只需要将实体附加到相应的实体集,然后将实体状态标记为“已修改”。

2; 2; The client sends both the original entity and the updated entity. 客户端发送原始实体和更新的实体。 You attach the original and set the currentvalues to the the update entity. 您附加原始文件并将当前值设置为更新实体。

3; 3; The client only sends the modified properties, not the whole entity. 客户端仅发送修改后的属性,而不发送整个实体。 In this case you have to query the original entity from the db and set the properties either one by one or again override the currentvalues. 在这种情况下,您必须从数据库查询原始实体,并一一设置属性,或者再次覆盖currentvalue。

The 3 approaches differ in their bandwith requirement and the number of queries they make. 这三种方法的带宽要求和查询数量不同。

1; 1; If we take this as the baseline, it has a bandwith requirement of sending one entity from the client to the server, and then sending this one entity from the server to the db. 如果我们以此为基准,则对带宽有一个要求,即从客户端向服务器发送一个实体,然后从服务器向数据库发送一个实体。 This makes 1 db query altogehter (attaching does not require querying, so only the saving changes part initiates the query). 这样就可以进行1 db查询(附加不需要查询,因此只有保存更改部分才能启动查询)。

2; 2; This has a bandwith of sending two entities from the client to the server. 它具有将两个实体从客户端发送到服务器的功能。 Here you have to send less data from the server to the db, because the changed properties are calculated when you set the currentvalues. 在这里,您必须将更少的数据从服务器发送到数据库,因为更改的属性是在设置currentvalue时计算的。 Again, just 1 query (attaching and setting currentvalues don't initiate queries, so only the saving changes part creates a query). 同样,只有1个查询(附加和设置currentvalue不会启动查询,因此只有保存更改部分才能创建查询)。

3; 3; This has the least bandwith requirement both from the client to the server and from the server to the db (both times only the changed properties are sent). 从客户端到服务器以及从服务器到db的带宽需求最少(两次仅发送更改的属性)。 However, this does need one more query besides saving, because you have to query the original values from the db, before setting the changes. 但是,除了保存之外,还需要再执行一次查询,因为在设置更改之前,您必须从数据库查询原始值。

I ususally find the the first approach is a good trade-off between the other two. 我通常发现第一种方法是在其他两种方法之间进行权衡。 It does send more data than the third, but still less than the second, and it only initiates the one query for saving data. 它确实发送了比第三次更多的数据,但仍然少于第二次,并且仅启动一个查询以保存数据。 Also I like to minimize the traffic between the client and the server even if it means there is more traffic between the server and the db. 我也想最小化客户端和服务器之间的流量,即使这意味着服务器和数据库之间的流量更大。 The clients (for me at least) are usually mobile, so no guaranteed bandwith, no guaranteed battery lifetime. 客户端(至少对我而言)通常是移动的,因此不保证带宽,也不保证电池寿命。 The server and the db are much "closer" and they don't have these restrictions. 服务器和数据库“更紧密”,它们没有这些限制。 But of course this can be different for your application. 但是,当然这对于您的应用程序可能有所不同。

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

相关问题 基于多系统数据库结构的复制/更新最佳实践 - Multi System Database structure based copying/updating best practice 一次插入或更新多个表的最佳做法是什么 - What is the best practice for inserting or updating multiple tables at once 使用实体框架时锁定记录以进行编辑的最佳实践 - Best practice to lock a record for editing while using entity framework 关系数据:实体继承方法。 最佳实践 - Relational Data: entity inheritance approaches. Best practice CRUD WEB API 与实体框架 - CRUD WEB API with entity framework Web应用程序上SQL Server上用户/角色的最佳实践 - Best practice on users/roles on SQL Server for a web application Web App向导的最佳实践-会话变量或嵌入式SQL? - Best Practice for Web App Wizard — Session Variables or In-Line SQL? 关系数据库:关于每个API调用的查询量的最佳实践 - Relational Database: Best practice regarding amount of queries per API call 使用Entity-Framework-Code-First以编程方式设置SQL索引的最佳实践是什么 - What is the best practice to programmatically set SQL indexes with Entity-Framework-Code-First 使用.NET实体框架对第三方应用程序进行数据访问的最佳实践 - Best practice for data access to third-party applications using .NET Entity Framework
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM