简体   繁体   English

API设计:将内部功能与外部功能分开或组合?

[英]API Design: Separate or combine internal with external functions?

For a simple example, let's say you create guestbook in your site and you plan to give external access for Create and Read functions via API. 举一个简单的例子,假设您在站点中创建了留言簿,并计划通过API为创建和读取功能提供外部访问。 Update and Delete will not be included in the API since it should only be used internally. 更新和删除将不包含在API中,因为它只应在内部使用。

What's a better practice? 什么是更好的做法?

  • Make the whole CRUD functions available via API and just restrict public access to Update and Delete. 通过API使整个CRUD功能可用,并限制公共访问更新和删除。 So you yourself will also use the API for all internal workings of the app. 因此,您自己也将使用API​​进行应用程序的所有内部工作。

  • Make internal CRUD functions without API for all internal workings, then create a separate API for Create and Read only. 在没有API的情况下为所有内部工作制作内部CRUD函数,然后为Create和Read创建单独的API。 Basically this give you two ways to use Create and Read - one with API, one without. 基本上,这为您提供了两种使用Create和Read的方法 - 一种使用API​​,一种不使用。

  • Make internal Update and Delete functions without API for internal workings, then create API for Create and Read only. 在没有API的情况下进行内部更新和删除功能以进行内部工作,然后为创建和只读创建API。 If you need to use Create and Read functions for internal workings, then you must use API. 如果您需要使用创建和读取功能进行内部工作,则必须使用API​​。

I hope it's quite clear, what's generally the best practice? 我希望很清楚,通常最好的做法是什么?

I like the first option. 我喜欢第一个选择。 It has a clear example of separation of concerns and using right tools in the right context. 它有一个明确的例子,即在正确的背景下分离关注点和使用正确的工具。

By using CRUD approach for the API, you gain uniformity and integrity. 通过对API使用CRUD方法,您可以获得一致性和完整性。 It results in a more cohesive design, less code duplication and easier evolution down the road. 它可以实现更具凝聚力的设计,更少的代码重复以及更容易的发展。 The security aspect could be implemented either inside or outside of the application powering the API. 安全方面可以在为API提供动力的应用程序内部或外部实现。 For the outside option you could use 3-d party software or hardware solutions like firewalls, for example. 对于外部选项,您可以使用三维聚会软件或防火墙等硬件解决方案。

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

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