简体   繁体   English

用于 UI 的 REST API - 一个通用端点与每个组件的端点

[英]REST API for UI - One General Endpoint vs Endpoint per Component

Let's assume we have two micro-services: companies API and accounts API.假设我们有两个微服务:公司 API 和账户 API。
We also have a dashboard that displays some data and visualizations about accounts.我们还有一个仪表板,显示有关帐户的一些数据和可视化。

The dashboard includes three widgets (for simplification):仪表板包括三个小部件(为简化起见):

Widget 1 - A table of accounts filtered by some criteria, for example, shows only pending accounts.小部件 1 - 按某些条件过滤的帐户表,例如,仅显示待处理帐户。 It also displays a few columns related to the account's company properties.它还显示与帐户的公司属性相关的几列。

Widget 2 - A piechart that displays a breakdown of accounts by one of the account's properties.小部件 2 - 按帐户属性之一显示帐户细分的饼图。

Widget 3 - Another chart that shows a breakdown of the accounts by a company.小部件 3 - 另一个显示公司帐户明细的图表。

Theoretically, all of the widgets in the dashboard display data about the same entity - accounts.从理论上讲,仪表板中的所有小部件都显示有关同一实体 - 帐户的数据。
Now, I would like to ask about two approaches for fetching and displaying the accounts in the UI:现在,我想问一下在 UI 中获取和显示帐户的两种方法:

The general REST endpoint approach通用 REST 端点方法

  • Create one general endpoint for fetching accounts.创建一个用于获取帐户的通用端点。
  • Create one general endpoint for fetching companies.为获取公司创建一个通用端点。
  • Fetch all of the accounts once the user enters the page.用户进入页面后获取所有帐户。
  • Fetch all of the companies once the user enters the page.用户进入页面后获取所有公司。
  • Let each widget manipulate, filter, and join data based on its needs.让每个小部件根据其需要操作、过滤和连接数据。
  • Most of the logic lies in the frontend.大部分逻辑都在前端。

The REST endpoint per widget approach REST 端点每个小部件方法

  • Create an endpoint per widget that returns all the relevant data for a specific widget, already filtered, manipulated, and joined for the widget needs.为每个小部件创建一个端点,该端点返回特定小部件的所有相关数据,这些数据已经过滤、操作和连接以满足小部件的需求。
  • Each widget calls its endpoint and displays the data.每个小部件调用其端点并显示数据。
  • Most of the logic lies in the backend.大部分逻辑都在后端。

My questions are:我的问题是:

  • What are the pros and cons of each approach?每种方法的优缺点是什么?
  • In which case would you choose one way over the other?在哪种情况下,您会选择一种方式而不是另一种方式?
  • Are there any other ideas for addressing this use case?还有其他解决此用例的想法吗?

Here are my ideas around this:以下是我对此的想法:

  • If you create one general endpoint per entity, then the frontend workload depends on the amount of data you get from the backend (number of accounts / companies).如果您为每个实体创建一个通用端点,那么前端工作负载取决于您从后端获得的数据量(帐户/公司的数量)。 If you have a limit on those entities (say like max 10 accounts), then it shouldn't be an issue.如果您对这些实体有限制(比如最多 10 个帐户),那么这应该不是问题。 If not, then it might be problematic (over time) to go on that path.如果不是,那么该路径上的 go 可能会出现问题(随着时间的推移)。
  • One scenario where I would consider one endpoint per entity is if you have other components (more important) which are consuming data from this REST API, and they need to get data in this way.我会考虑每个实体一个端点的一种情况是,如果您有其他组件(更重要)正在使用来自此 REST API 的数据,并且他们需要以这种方式获取数据。 But even so, I would think twice if this is really necessary.但即便如此,如果这真的有必要,我会三思而后行。
  • AFAIK, the best practice around UI data flows is that you should do as much work as you can on the backend, where you have control on the resources and you have room for optimizations. AFAIK,围绕 UI 数据流的最佳实践是你应该在后端做尽可能多的工作,你可以控制资源并且有优化的空间。
  • One idea that you can consider is to think about a way to store the data more efficiently (already prepared for the view), to minimize the backend work.您可以考虑的一个想法是考虑一种更有效地存储数据的方法(已经为视图做好了准备),以最大限度地减少后端工作。

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

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