REST - 模型状态转换 [英] REST - model state transitions

查看:33
本文介绍了REST - 模型状态转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

REST - 可恢复的 DELETE 中,很好地介绍了如何在 REST 中模拟状态变化被给予.基本上,如果您有一个带有 status 字段的资源,您只需将该资源的新版本与更新 status 字段一起放置.

In REST - revertable DELETE a nice introduction on howto model state changes in REST was given. Basically, if you have a resource with a field status, you just put a new version of that resource with an update status field.

在这个主题中,我想扩展这个模型.假设您有一个资源,它可以处于两种状态:1 和 2.与引用的帖子中描述的简单模型相比,从状态 1 到状态 2 有三种转换,而不是只有一种.

In this topic, I would like to extend this model. Say you have a resource which can be in two states: 1 and 2. In contrast with the simple model as described in the cited post, there are three transitions to traverse from state 1 to state 2, instead of just one.

我的问题是:您将如何在 REST 中为状态转换建模?

我自己无法想出类似 RPC 的 POST,这可能不是 RESTian:

I myself cannot come up with an RPC-like POST, which isn't very RESTian probably:

POST http://server/api/x
     target_state=2&transition=3

这通过使用转换 3 将资源 x 从状态 1 更改为状态 2.

This changes resource x from state 1 to state 2 by using transition 3.

推荐答案

这并不仅限于 REST;它更像是一个关于状态机的基本问题.状态机应该封装所有的状态,这样如果您发现自己创建了从一个状态到另一个状态的多个转换,并且差异很大,那么这种差异也必须在状态中捕获.

This isn't really limited to REST; it's more a basic question about state machines. The state machine should encapsulate all of the state, so that if you find yourself creating multiple transitions from one state to another, and the difference is significant, then that difference must also be captured in the state.

例如,假设我没有家.我可以通过三种方式从无家可归"状态转变为家"状态:我可以租一个,我可以买一个,我可以偷一个.从无家可归"到有家"的三个转变?不是在机器的世界里.差异要么显着,要么不显着.如果不是,那么对于机器来说,根本没有必要进行区分.只是把家"的新地位.如果是,那么我们需要扩展我们的状态概念以包含差异.例如:

For example, say I have no home. I can move from the "homeless" state to the "home" state in three ways: I can rent one, I can buy one, I can steal one. Three transitions from "homeless" to "home"? Not in the world of machines. Either the differences are significant, or they're not. If they're not, then to the machine there's no point in making a distinction at all. Merely PUT the new status of "home". If they are, then we need to expand our concept of state to encompass the differences. For example:

         homeless
        A    A   A
       /     |    \
      V      |     V
possessor <--|---  renter
       A     |    /
        \    |   /
         V   V  V
           owner

我可以通过偷房子从无家可归者变成拥有者.如果我蹲得够久,我可能会成为它的主人.或者我可能无家可归,租房子,甚至先租后买.或者我可以直接买一个.所有三个路径都让我进入所有者"状态,但它们使用中间状态来表示显着不同的转换.

I can move from homeless to possessor by stealing a house. If I squat on it long enough, I might become the owner. Or I could be homeless and rent a house, or even rent-to-own. Or I could buy one outright. All three paths get me to the "owner" state, but they use intermediate states to represent the significantly different transitions.

然后,如果您想代表无家可归者"与在家中"(拥有者|租用者|所有者),那么将其作为只读、计算资源本身公开是没有问题的.只是不要让任何人 PUT/POST 到它,因为转换是模棱两可的.如果该状态很重要,那么将状态转换的历史记录作为一组额外的资源也没有问题.

If you then want to represent "homeless" vs. "in a home" (possessor|renter|owner), there's no problem exposing that as a read-only, calculated resource in its own right. Just don't let anyone PUT/POST to it, since the transition is ambiguous. There's also no problem keeping the history of state transitions as an additional set of resources, if that state is significant.

这篇关于REST - 模型状态转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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