1

From REST in Practice: Hypermedia and Systems Architecture:

The current state of a resource is a combination of:

  • The values of information items belonging to that resource
  • Links to related resources
  • Links that represent a transition to a possible future state of the current resource
  • The results of evaluating any business rules that relate the resource to other local resources

a) why would "links to related resources" also represent the current state of a resource?

b) I also don't quite understand why "Links that represent a transition to a possible future state of the current resource" also represent the the current state.

Namely, those links represent the possibility, not the current state. Analogy would be an int variable set to value 10. It's possible that in the future this variable will get processed and set to value 100, but we don't claim its current state also includes possible future state of 100?!

REPLY:

1)

links to related resources: imagine a "chat_room" resource. The links to people in that room will change as they come and go. In other words, a resource's relationships to other resources may change.

a) I also began learning domain driven design and there an entity E may have child entities ( thus E is a parent in a relationship ) and E's relationship with its child entities may change ( ie some child entities may be assigned to different parent or may get deleted).

But even though E has one-to-many relationship with its child entities, we don't consider that relationship as also representing the current state of E. That's is why I'm puzzled as to why in REST the links to related resources also represent the current state?!

b)

imagine a university has a "student" resource. There might be a link to "register" in a class, a link to "apply for housing", and links to "tune in", "turn on" and "drop out". Each link may change the state of the object, or may yield appropriate information for the resource state. For example, the correct Student Housing application for that student.

It sort of makes sense that the state of current resource also constitute those links to related resources, which may state change the state of the current resource. But it doesn't make sense that the state of the current resource also constitute links to those related resources which don't change the state of the current resource?!

2)

But the resource may be a sort of state machine. So a "car" resource might have links to "start_engine" if it's stopped. It might links to "speed_up", "slow_down", and if the speed is slow enough, "stop_engine". In other words, links to transition to other valid states based on the current state.

a)

links to transition to other valid states based on the current state.

I assume you've made a typo and what you meant to write is "links to transition to other valid states is based on the current state"?

b) So could we say the reason why links that represent transitions to other states also represent current state of a resource is because every resource with such links could be considered a state machine, and with state machine, links to other valid states do represent current state ( I hope my ramblings make some sense )?!

2. REPLY:

1)

But it seems more natural in handling structured data to just bundle the "chat_room" and "chat_room_person" information together.

I assume technically, related resources themselves don't represent the current state of a resource. Instead, only links to these related resources represent the current state of a resource?!

2)

In REST, the goal is to eliminate all out-of-band communication. So we supplement the state (ON) with links that represent all the valid transitions for that state. So we want to avoid more documentation. The resource representation can tell us, "This is where I am, and this is where I can go next."

In REST the links that represent transitions to other states also represent the current state of a resource, but could we argue that logically ( thus not in the context of REST ) these links don't really represent the current state of a resource, and thus only reason why in REST they do represent the current state of a resource is because REST is build around that assumption ( or is at least more efficient that way )?

thank you

  • 1
    you seem to be quoting from some source, what is that you're quoting? Google suggests it's some book, "REST in Practice: Hypermedia and Systems Architecture" - is that it? – gnat Jun 4 '14 at 19:15
  • 1
    @gnat: yes, it's from that book. I've made an edit to acknowledge that – bckpwrld Jun 4 '14 at 19:36
  • If the relationship between a parent resource and children (e.g. the number of child resources) isn't part of the parent's state, what state is it a part of? – Mike Partridge Jun 5 '14 at 16:55
  • @MikePartridge: In DDD at least, the relationship is part of child's state ( at least that's my understanding, though I've only began learning DDD, so I could be wrong ) – bckpwrld Jun 5 '14 at 17:33
2
  • links to related resources: imagine a "chat_room" resource. The links to people in that room will change as they come and go.

In other words, a resource's relationships to other resources may change.

  • links that represent a transition... : an example is pagination. The resource might have a "next" link that links to page 2 from page 1, page 3 from page 2, etc.

But the resource may be a sort of state machine. So a "car" resource might have links to "start_engine" if it's stopped. It might links to "speed_up", "slow_down", and if the speed is slow enough, "stop_engine".

In other words, links to transition to other valid states, based on the current state.

(That's not the most REST-ful example, but to convey the idea...)

  • The results of evaluating business rules: imagine a university has a "student" resource. There might be a link to "register" in a class, a link to "apply for housing", and links to "tune in", "turn on" and "drop out". Each link may change the state of the object, or may yield appropriate information for the resource state. For example, the correct Student Housing application form specifically for that student's situation.

...

But even though E has one-to-many relationship with its child entities, we don't consider that relationship as also representing the current state of E.

I think that's just a difference in methodologies. For example, in a database table, you wouldn't include records related to a table in the table itself; you'd have foreign keys in the other table, or else an association array. So, just like your example, the state of a record in a database table doesn't include references to other things (usually).

In a Resource-oriented view, the book is saying you can contain those links in the resource itself, as part of the Resource definition.

Note that you don't have to do it that way. You could still have a "chat_room" resource, a "person" resource, and a "chat_room_person" resource. But it seems more natural in handling structured data to just bundle the "chat_room" and "chat_room_person" information together.

...

Sorry, I just added a comma for clarity: "transition to other valid states, based on the current state"

...

Usually when we represent a state machine, we just say "This is the state: ON". The client needs to know what transitions are available now that the thing is ON.

In REST, the goal is to eliminate all out-of-band communication. So we supplement the state (ON) with links that represent all the valid transitions for that state.

So we want to avoid more documentation. The resource representation can tell us, "This is where I am, and this is where I can go next."

...

related resources themselves don't represent the current state of a resource. Instead, only links to these related resources represent the current state of a resource

That's tricky a tricky question, because the meaning of "Resource" in REST is so amazingly broad. The answer is, yes, if you define your resource that way. And no if you don't.

For example, the "chat_room" might have a list of links to "people" in the chat room. Or it might just include the JSON for each of those people. If your resource includes the nested data, does that mean that the data is part of the state?

It's fair to say that the state of the chat room does not include the state of the people in it. But it turns into a "One, but not the same" kind of deal, that depends on how you define it.

...

in REST they do represent the current state of a resource is because REST is build around that assumption

I think that's a fair statement. I've never charted out the philosophical implications of that, but in practice it works out that way.

Again, the idea is to represent as much as you can, so that no out-of-band communication is needed. That's the motivation for rolling all this up into a Resource.

  • I've made a reply. If you can, please read it – bckpwrld Jun 5 '14 at 16:05
  • Sorry, I haven't noticed your reply. Anyhow, I've made another reply ( it's final, I promise ) and if you can, please see it – bckpwrld Jun 5 '14 at 18:51
  • 1
    I added to my original answer. :) – Rob Jun 5 '14 at 19:32
  • 1
    OK try now. 1234 – Rob Jun 5 '14 at 20:44
  • 1
    you bet! Good question! – Rob Jun 5 '14 at 22:04
1

I'll try to answer with another example, and use some code to explain.

a) why would "links to related resources" also represent the current state of a resource?

Consider this class and assume it is the implementation of the REST resource /Human, abstracting from real live (Python-ish pseudo-code):

class Human:
  offsprings = [] # array of links (e.g. object id) to other Humans
  age = 20
  def is_adult():
     return age >= 20
  def is_parent():
     return len(offsprings) > 0 

peter = Human()
print peter.is_parent()
=> False

kate = Human()
peter.offsprings.append(kate.id)
print peter.is_parent()
=> True

Now quite clearly is_parent represents a state information. But this state is one that can only be answered by knowing about the links to related objects.

b) I also don't quite understand why "Links that represent a transition to a possible future state of the current resource" also represent the the current state.

Consider an extension of the above resource:

class Human:
  offsprings = [] # array of links (e.g. resource id) to other Humans
  avg_expected = [...] # array of statistic resources by birth date
  age = 20
  year_of_birth=1994
  def is_adult():
     return age >= 20
  def is_parent():
     return len(offsprings) > 0
  def live_expectancy():
     # consider the [ ] operator to look up the respective resource
     return avg_expected[year_of_birth] - age 

Again, live_expectancy is a state information for this resource. And it represents a possible future state, namely end of life. But without the link to the statistics resource, it is impossible to tell.

  • I thought link L ( that represents transition to future state S2 of our current resource R ) actually navigates to R in state S2. But your example suggests that L ( ie live_expectancy ) doesn't navigate to R in state S2, but instead just contains values for S2, which R may contain sometime in the future? – bckpwrld Jun 5 '14 at 22:08
  • 1
    @bckpwrld actually L (statistics) doesn't itself constitute the current state (live_expectancy) or future state (possible end of live in year X), or indeed any specific values relating to R, it simply contains information that you need in order to determine the current state of R and a possible future state. – miraculixx Jun 5 '14 at 22:21
  • Following question is about REST in general and not about your specific example: So would you say that link L ( that represents transition to future state S2 of our current resource R ) doesn't usually navigate to R in state S2, but instead simply contains information needed to determine possible future state of R and/or set R to S2? – bckpwrld Jun 5 '14 at 22:35
  • 1
    @bckpwrld not usually, yes. But I'm sure <some> specific example would easily proof me wrong on this. I guess, it depends... – miraculixx Jun 5 '14 at 22:37
  • 1
    by "usually" I mean "in the specific cases that I can think of right now", and "there might be some specific case where this is not true". – miraculixx Jun 5 '14 at 22:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.