简体   繁体   English

RESTful API中的向后兼容性

[英]Backward compatibility in a RESTful API

I am currently designing an RESTful API implemented using the ASP.NET Web API. 我目前正在设计使用ASP.NET Web API实现的RESTful API。
One very important point is backward compatibility. 一个非常重要的点是向后兼容性。
I would like to keep the actual API code free of any thoughts about backward compatibility, to ensure clean and up-to-date design. 我想保持实际的API代码不受任何关于向后兼容性的想法,以确保清晰和最新的设计。
I thought about solving this by having a layer of version translators. 我想通过拥有一层版本翻译来解决这个问题。
Each of these translators knows how to transform the requests and responses of one specific version (vPrevious) into those of another specific version (vCurrent). 这些翻译中的每一个都知道如何将一个特定版本(vPrevious)的请求和响应转换为另一个特定版本(vCurrent)的请求和响应。
These translators could easily be stacked to ensure backward compatibility with an arbitrary number of old versions. 这些转换器可以轻松堆叠,以确保向后兼容任意数量的旧版本。
Each of these translators would be responsible for doing all kinds of stuff, like setting default values for new properties or even performing an array of different operations. 这些翻译中的每一个都负责执行各种操作,例如为新属性设置默认值,甚至执行不同操作的数组。

I thought about implementing a DelegatingHandler that examines the version number of the API the client wants to use and based on that knows which translators to use. 我考虑过实现一个DelegatingHandler ,它检查客户端想要使用的API的版本号,并根据它知道要使用哪些翻译器。
However, I fail to see how I would actually go about implementing this. 但是,我没有看到我将如何实现这一目标。 As I see it, each translator actually needs to be a full blown ASP.NET Web API of its own, along with a routing table and controllers. 在我看来,每个翻译器实际上都需要是一个完整的ASP.NET Web API,以及路由表和控制器。 That actually would be acceptable, but how would that translator controller route the data to the next one in the chain? 这实际上是可以接受的,但是翻译控制器如何将数据路由到链中的下一个?

I think that the cleanest way is to keep each version you need to support, running. 我认为最干净的方法是保留您需要支持,运行的每个版本。 That means that each version has its own branch in your VCS and is focused on serving clients that is using that specific version. 这意味着每个版本在您的VCS中都有自己的分支,并专注于为使用该特定版本的客户提供服务。 Code is clean and focused on one version. 代码很干净,专注于一个版本。 If you need to work against common components/databases etc, I think that you should implement that layer further down in your architecture, not in the REST API layer. 如果您需要处理常见的组件/数据库等,我认为您应该在架构中进一步实现该层,而不是在REST API层中。 This versioning layer should be the first layer, behind the REST API. 此版本控制层应该是REST API后面的第一层。

If it really does not work, I would probably design it so that the latest API is the only API that works against your real backend. 如果它确实不起作用,我可能会设计它,以便最新的API是唯一适用于您的真实后端的API。 The other versions simply call the other versions through a regular HTTP request... the older versions are REST clients of the newer API's. 其他版本只是通过常规HTTP请求调用其他版本...旧版本是较新API的REST客户端。 This is not super fast but the design is very clean and it is easy to understand where versioning code goes. 这不是超级快,但设计非常干净,很容易理解版本代码的位置。 If you have many clients that work against old API verisons, this might cause the solution to be too slow and you need to find more effective ways of doing it, but then, we are almost back to the first solution. 如果您有许多客户端可以解决旧的API问题,这可能会导致解决方案太慢,您需要找到更有效的方法,但是,我们几乎回到了第一个解决方案。

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

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