简体   繁体   English

在SOA架构中,单个API应该执行所有操作,或者应将API拆分为多个操作

[英]In SOA architecture should single API do everything or API should be split as multiple action

We have an app which is exposing a RESTful API to a UI for purchasing an item. 我们有一个应用程序,它将RESTful API暴露给用于购买项目的UI。 I have a question regarding API design. 我对API设计有疑问。 Lets say the following action should be taken in order 让我们说应该按顺序采取以下行动

  1. Item to be chosen for purchase 要选择购买的物品
  2. Next give the address to be delivered to 接下来给出要传递的地址

My question is: should we design a single API which gets both data perform both? 我的问题是:我们应该设计一个API来同时获取两个数据吗? Or should we design two API calls - one that creates a purchase record and second that update the address to be delivered to? 或者我们应该设计两个API调用 - 一个创建购买记录,另一个更新要传递的地址?

The recommended SOA approach is to choose coarse-grained services, which would seem to argue the minimum amount of API calls. 推荐的SOA方法是选择粗粒度服务,这似乎是最少量的API调用。

However, from a business point of view, item selection and purchase and item delivery are two very different concerns that should be separate components in your architecture. 但是,从业务角度来看,项目选择和购买以及项目交付是两个非常不同的问题,应该是架构中的独立组件。 For item selection, you need to consider things like inventory and pricing. 对于项目选择,您需要考虑库存和定价等内容。 For delivery address, you need to consider user address lists, address validation, shipping, and taxation. 对于送货地址,您需要考虑用户地址列表,地址验证,运输和税收。

Those two components are not likely to interact much except maybe some external association between an item id and address id. 除了项ID和地址id之间的某些外部关联之外,这两个组件不太可能相互作用。 For this reason, I'd recommend two API calls. 出于这个原因,我建议两个API调用。 Functionally, this would also allow your API users do things like update the delivery address without re-purchasing an item, send the bill to one address and the item to another address, etc. 从功能上讲,这也可以让您的API用户在不重新购买商品的情况下更新送货地址,将帐单发送到一个地址,将商品发送到另一个地址等。

As you state that you design a RESTful API you usually start by designing resources instead of the intended calls. 当您声明设计RESTful API时,通常首先要设计资源而不是预期的调用。 Later on, resource representations can be chosen that include other resources for optimised HTTP request counts. 稍后,可以选择包含用于优化HTTP请求计数的其他资源的资源表示。

You might want to choose to proceed the following way: 您可能希望选择以下列方式继续:

  1. Model the item list resource (GET - lists all items, POST - allows item creation) /items/ 对项目列表资源建模(GET - 列出所有项目,POST - 允许项目创建)/ items /
  2. Model the address list resource /addresses/ 建模地址列表资源/地址/
  3. Model the item instance resource /items/item/resourceId 为项目实例资源/ items / item / resourceId建模
  4. Model the address instance resource /addresses/address/resourceId 为地址实例资源/ addresses / address / resourceId建模

Now all of your resources are available you can think about usage patterns. 现在您可以使用所有资源,您可以考虑使用模式。 All your resources are generically available and can be mashed up. 您的所有资源都是一般可用的,可以将其混淆。 Possible approaches to answer your questions would be: 回答问题的可能方法是:

  1. Extend the item instance resource with the needed address details (coarse grained as lreeder stated) 使用所需的地址详细信息扩展项目实例资源(粗粒度为lreeder声明)
  2. Model a resource /deliverydetails/ as list and instance resource containing item(s) and address, make the deliverydetails to be queried by item id or whatever your use case fits best 将资源/ deliverydetails /建模为包含项目和地址的列表和实例资源,使项目ID或任何用例最适合的查询交付详细信息

Hope that helps! 希望有所帮助! Btw. 顺便说一句。 you are automatically following SOA approaches with a Resource Oriented Design. 您将使用面向资源的设计自动遵循SOA方法。 Interfaces will be naturally fitting your business requirements and generic enough to support more advanced use cases. 接口将自然地满足您的业务需求和通用性,足以支持更高级的用例。

Here is a good example 这是一个很好的例子

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

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