RESTful 软删除 [英] RESTful Soft Delete

查看:67
本文介绍了RESTful 软删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试构建一个 RESTful 网络应用程序,其中我使用了 GET、POST、PUT 和 DELETE.但是我有一个关于在这个特定应用中使用 DELETE 的问题.

I'm trying to build a RESTful webapp wherein I utilize GET, POST, PUT, and DELETE. But I had a question about the use of DELETE in this particular app.

先介绍一下背景:

我的 webapp 管理通用实体,这些实体也在另一个系统中管理(并且,它总是创建).因此,在我的 web 应用程序中,每个实体都将使用唯一键存储在数据库中.但是我们通过 URL 访问它们的方式是使用 other 系统的唯一键.

My webapp manages generic entities that are also managed (and, it happens, always created) in another system. So within my webapp, each entity will be stored in the database with a unique key. But the way we will be accessing them through URLs is with the unique key of the other system.

我想,一个简单的例子就能说明这一点.获取 URL /entity/1.这将显示 ID 为 1 的实体的信息在其他系统,而不是我自己的系统.事实上,我系统中的 ID 将被完全隐藏.在我自己的系统中将没有用于访问 ID 为 1 的实体的 URL 方案.

A simple example will make this clear, I think. Take the URL /entity/1. This will display information for the entity with ID 1 in the other system, and not my own system. In fact, IDs in my system will be completely hidden. There will be no URL scheme for accessing the entity with ID of 1 in my own system.

好的,既然我们知道了我的 web 应用程序的结构,让我们返回删除这些实体.

Alright, so now that we know how my webapp is structured, let's return to deleting those entities.

将有一种方法可以在我的系统中删除"实体,但我在它周围加上引号,因为它实际上不会从数据库中删除它们.相反,它会用一个属性来标记它们,以防止它在您转到 /entity/1 时出现.

There will be a way to 'delete' entities in my system, but I put quotes around it because it won't actually be deleting them from the database. Rather, it will flag them with a property that prevents it from appearing when you go to /entity/1.

因此,我觉得我应该使用PUT(以这种方式'删除'将是幂等的),因为从数据的角度来看,我只是简单地设置一个属性.

Because of this, I feel like I should be using PUT ('deleting' in this way will be idempotent), since I am, from the perspective of the data, simply setting a property.

那么,问题是:RESTful 方法是否对数据(在这种情况下很明显我是 PUTing)或数据在应用程序中的表示(在其中情况似乎我正在DELETEing)?

So, the question: does the RESTful approach have fidelity to the data (in which case it is clear that I am PUTing), or the representation of the data in the app (in which case it seems that I am DELETEing)?

推荐答案

你应该使用 DELETE.

您打算对数据执行的操作称为软删除":您设置一个标记并避免出现标记的项目.这是您的 web 应用程序内部的,用户不必知道您正在软删除而不是删除或您想做的任何事情.这就是您应该使用 DELETE 动词的原因.

What you intend to do with your data is called "soft deleting": you set a flag and avoid flagged items from appearing. This is internal to your webapp and the user doesn't have to know that you're soft deleting instead of deleting or whatever you want to do. This is why you should use the DELETE verb.

这篇关于RESTful 软删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆