简体   繁体   English

业务逻辑和静态API设计

[英]Business logic and restful API design

Let's assume we have a simple API allowing clients to fetch a list of items of a specific type: 假设我们有一个简单的API,允许客户端获取特定类型的项目列表:

   GET /items/foo
   GET /items/bar
   GET /items/blah

A response is a list of items of the requested type, each entry has an unique ID. 响应是所请求类型的项目的列表,每个条目都有唯一的ID。 The client will usually display these items in table/grid/etc. 客户通常会在表/网格/等中显示这些项目。

Now in the client we must implement a pinning feature so another API allows pinning/unpinning items based on their ID & their type. 现在,在客户端中,我们必须实现固定功能,以便另一个API允许根据项目的ID和类型固定/取消固定项目。 So I was discussing with my colleagues possibilities to inform the client about which items are pinned or not. 因此,我正在与同事讨论是否有可能告知客户是否固定了哪些物品。

An option was to have another API GET /pinning/{type} to return the list of all the pinned items of a specified type. 一种选择是让另一个API GET /pinning/{type}返回指定类型的所有固定项的列表。

Another solution was to use a similar API GET /pinning/{type} to return the list of the IDs of all the pinned items. 另一个解决方案是使用类似的API GET /pinning/{type}返回所有固定项的ID列表。 Let the client sort it out. 让客户解决它。

The first solution was accepted. 第一个解决方案被接受。 Their argument was that the backend is responsible for business logic and that the client shouldn't be involved in business logic so the client should just display data it receives from the server. 他们的论点是,后端负责业务逻辑,而客户端不应参与业务逻辑,因此客户端应仅显示从服务器接收的数据。 This argument didn't sell it for me. 这种说法并没有卖给我。 I'm thinking the server should in this case provide the data that allows the client to perform additional presentation logic. 我认为服务器在这种情况下应提供允许客户端执行其他表示逻辑的数据。

Which solution is better? 哪种解决方案更好? Or what other solutions are possible? 或者还有其他解决方案吗?

If the server would only return ItemIds at GET /pinning/{type} , the client would have to repeatedly call something like GET /items/{itemId} in order to obtain data it can display on the UI, right? 如果服务器只返回GET /pinning/{type} ItemIds,则客户端将不得不反复调用GET /items/{itemId} ,以获取可以在UI上显示的数据,对吗? This in turn would just increase the load on the server. 这反过来只会增加服务器上的负载。 If the id would be enough, you can probably get away with the proposed solution. 如果id足够,您可能可以摆脱提议的解决方案。 Since both the client and the server seem to be under the same umbrella (as in your company is also the API consumer), you have enough information to make a decision. 由于客户端和服务器似乎处于同一个伞下(因为您的公司也是API使用者),因此您有足够的信息来做出决定。

Even if it were a Public API with lots of clients I would still go down the route of returning items instead of just itemIds - probably in a paged manner, for performance reasons. 即使它是具有很多客户端的公共API,我仍然会沿途返回项目而不是仅返回itemIds-出于性能原因,可能以分页的方式。

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

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