RPC 样式的示例如何以资源为中心的样式建模 [英] How an RPC-style example would look modelled in a resource-centric style

查看:38
本文介绍了RPC 样式的示例如何以资源为中心的样式建模的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我几天来一直在网上阅读有关 REST 的内容,并且正在为 HATEOAS 的概念而苦苦挣扎.

I have been reading around the web about REST for several days now and am struggling with the concept of HATEOAS.

我想我很挣扎,因为我没有正确理解如何将数据建模为资源和(状态转换?)资源之间的链接.我相信我的问题是我所有的经验都是面向对象和 RPC,我不习惯以资源为中心的方式思考.

I think I am struggling because I don't properly understand how to model data as resources and (state-transitioning?) links between resources. I believe my problem is that all my experience is OO and RPC and I am unused to thinking in a resource-centric manner.

获得理解的唯一方法是从我的世界中举出一个例子,说出我认为以资源/链接为中心的方式建模的内容,然后将其扔到那里,然后在火焰中被击落.刻录完成后,我至少应该对我不理解的东西有更好的了解.

The only way to get an understanding is to take an example from my world, say what I think that might look like modelled in a resource/link-centric manner and throw it out there to be shot down in flames. After the burn is complete I should at least have a better understanding of what I don't understand.

我的(简化的)例子是:

My (simplified) example is:

我是承包商,例如水管工.我有许多工作分配给我.我可以搜索我的工作,指定简单的参数,例如目标日期范围.我可以开始任何分配给我的工作.当我开始工作时,我可以选择指定我开始工作的时间,或者如果我现在开始工作,则将其留空.

I am a contractor e.g. a plumber. I have a number of jobs assigned to me. I can search my jobs specifying simple paramaters such as a target date range. I can start any job assigned to me. When I start the job, I can optionally specify the time I started it or leave it blank if I am starting it now.

如果我以 RPC 方式实现它,我可能会向调用者公开两个方法:

If I were implementing this in an RPC manner I might expose two methods to callers:

ListOfJobs GetJobs(search parameters)
StartJobResult StartJob(jobID, optional start datetime)

如您所见,我正在考虑对象和操作.

As you can see, I am thinking objects and operations.

如果我在考虑资源和链接,资源可能是什么?

If I were thinking resources and links, what might the resources be?

我的猜测是:

  • 承包商:~/contractor/plumbersareus ?
  • 工作搜索:~/contractor/plumbersareus/searches/searchidentifier ?
  • 工作:~/job/12345 ?
  • 出勤:~/job/12345/attendances/attendanceidentifier ?

假设以上任何一项是正确的(我怀疑它是正确的),searchidentifier"和attendanceidentifier"应该是什么?前者在我的 RPC 世界中没有身份;它只是参数.后者将由 DateTime 标识.

Assuming any of the above is correct (and I doubt it is), what should "searchidentifier" and "attendanceidentifier" be? The former wouldn't have an identity in my RPC world; it would just be parameters. The latter would be identified by a DateTime.

链接可能是什么(忽略与自身的链接)?

What might the links be (ignoring links to self)?

  • 承包商:~/contractor/plumbersareus/searches ?
  • 工作搜索:~/job/12345、~/job/12346 等?
  • 工作:~/job/12345/attendances ?
  • 出勤:?

如果这是一个重复的问题,请接受我的道歉并关闭它.(我找不到重复的,但我可能一直在用不正确的术语进行搜索.)

Please accept my apologies if this a repeat question and close it down. (I couldn't find a repeat but I may have been searching with the incorrect terms.)

推荐答案

你可能读过这两本好书:

You may have read these two great books:

Restful Web 服务

回到你的问题.以 REST 为起点的更实用的思考或应用方式(至少对我有用)是从以下方式思考:

Back to your questions. The more practical way of thinking or applying REST as the starting point (at least it works for me) is to think in the following ways:

1) 仅使用 HTTP 'GET/POST/PUT/DELETE' 作为建模域操作"的方式.就像处理数据库一样,您的所有操作都映射到 CURD.

1) Use only HTTP ‘GET/POST/PUT/DELETE’ as the way to model your domain ‘actions’ . Just like when you dealing with database, all your actions are mapped to CURD.

2) URI/URL 仅用于标识资源.在您的 URI 中永远不应该有任何动作".

2) URI/URL is to identify resources only. Should never have any ‘actions’ in your URI.

3) 交换的数据应该在 HTTP 消息的正文中.只是为了简化讨论,而不是深入了解如何对数据本身进行建模

3) The data exchanged should be in the body of the HTTP messages. Just to simplify the discussions, not getting into how to model the data itself

这是我想到的.我相信有更好的方法来模拟你所拥有的东西,但它应该围绕相同的原则☺URL 模式的外观完全取决于您的应用程序域以及客户端和后端之间的意义.

Here is what comes to my mind. I’m sure that there are better ways to model what you have but it should be around the same principles☺ E.g. how the URL patterns look like is all depending on your application domain and what makes sense between the client and the backend.

模拟找工作":

  • HTTP:获取
  • 网址:~/contractors/[plumberID]/jobs/

建模,求职:

  • HTTP:获取
  • 网址:~/jobs/?[一些搜索参数]/

将工作分配给用户的模型:

To model assign the job to user:

  • HTTP:PUT
  • 网址:~/contractors/[plumberID]/assignments/[jobID]

使用 PUT 和 POST 有一些区别,你可以搜索一下.

There are some differences in using PUT vs POST, you can search around.

希望对您有所帮助.

这篇关于RPC 样式的示例如何以资源为中心的样式建模的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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