简体   繁体   English

如何在Restful Web API(.NET / JSON / EF6)服务器端实现部分更新(PATCH)

[英]How to implement Partial Updates (PATCH) in Restful Web API (.NET/ JSON/ EF6) Server side

I have been working on a Web API project (Restful), where the size of models (and respective table) is pretty huge, hence there is an absolute need of Partial Updates. 我一直在研究一个Web API项目(Restful),其中模型(和各自的表)的大小相当大,因此绝对需要部分更新。

I have reviewed the option of POST to a subsection of the model, but given that the model has huge amount of fields (and custom fields), don't think it feasible to expose that many URIs). 我已经回顾了POST对模型子节的选项,但鉴于模型有大量的字段(和自定义字段),不要认为暴露那么多URI是可行的。

I understand that using PATCH verb, I can allow a client to do partial updates, but I am struggling to conceptualize how should I capture the same on the server side and then find out what's changed (post JSON to model conversion) so that I can distinguish what field(s) are explicitly set as NULL by client (as she wanted to update field) Vs. 我理解使用PATCH动词,我可以允许客户端进行部分更新,但我很难概念化我应该如何在服务器端捕获相同内容,然后找出更改的内容(将JSON发布到模型转换)以便我可以区分哪些字段被客户端显式设置为NULL(因为她想更新字段)。 what is NULL as a result of no update. 由于没有更新,什么是NULL。

Should I use a custom mapper? 我应该使用自定义映射器吗? or should I create generic properties and capture state at a field level? 或者我应该创建通用属性并在字段级别捕获状态?

I have searched (and still doing) net and here at SOF.com, but there is nothing authoritative/ concrete that I have found, so reaching out to pros for feedback. 我已经搜索过(并且还在做)网络和SOF.com,但是我找不到任何权威/具体的内容,所以请专业人士联系以获得反馈。

appreciate your help! 感谢您的帮助!

KevinDockx created plugin to MVC that handle PATCH. KevinDockx创建了处理PATCH的MVC插件。 Here is nuget link https://www.nuget.org/packages/Marvin.JsonPatch/ 这是nuget链接https://www.nuget.org/packages/Marvin.JsonPatch/

This plugin is RFC 6902 implementation for .NET.. 这个插件是.NET的RFC 6902实现..

basically when you send json patch from your client you have to build array of operations 基本上当你从客户端发送json补丁时,你必须构建一系列操作

[
    { "op": "add", "path": "/foo", "value": "bar"},
    { "op": "replace", "path": "/baz", "value": "boo" }
]

Of course it only matters if you want to follow Specification. 当然,只有你想遵循规范才有意义。 In other case (I mean your own understanding of how PATCH works) you have to implement your custom logic. 在其他情况下(我的意思是你自己对PATCH如何工作的理解)你必须实现你的自定义逻辑。

EDIT 编辑

Personally I found that only 'replace' operation was useful in my use-cases because of flat nature of my DTOs. 我个人发现只有'替换'操作在我的用例中是有用的,因为我的DTO具有扁平的性质。

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

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