简体   繁体   English

最佳实践以及如何在客户端的C#包装器中支持不同版本的REST API

[英]Best Practices and How to support different versions of REST APIs in C# wrapper on client-side

I have written a C# wrapper to support our company project's REST APIs. 我编写了一个C#包装器来支持我们公司项目的REST API。 However, these APIs are changing now - in terms on URL (may introduce version number in URL) and data objects it expects and returns back. 但是,这些API现在正在发生变化 - 就URL而言(可能会在URL中引入版本号)和它期望的数据对象并返回。

I would like to know what would be the best practice on supporting different versions of REST APIs in my c# wrapper. 我想知道在我的c#wrapper中支持不同版本的REST API的最佳做法是什么。

And how should I do it - in terms of code design and class definitions - so that one wrapper can work seamlessly with different versions of API - and it should be extensible also - so that any newer version of APIs in future can also be easily supported. 我应该如何做 - 在代码设计和类定义方面 - 以便一个包装器可以与不同版本的API无缝协作 - 并且它也应该是可扩展的 - 以便将来可以轻松支持任何更新版本的API 。

The c# wrapper I have written is consuming our web service APIs. 我编写的c#包装器正在使用我们的Web服务API。 I am already using RestSharp client to consume our web service APIs in c# wrapper. 我已经在使用RestSharp客户端在c#wrapper中使用我们的Web服务API。

There's a subtle weirdness to what you are asking. 你要问的是一种微妙的怪异。 Let me reiterate what you have stated: 让我重申你所说的话:

1) There are multiple versions of the same service at your organization. 1)您的组织有多个版本的同一服务。 Perhaps they are established like Bob suggested, /current/api... /v1/api... /v2/api... etc. Or perhaps some other pattern. 也许它们像Bob建议的那样建立,/ current / api ... / v1 / api ... / v2 / api ...等等或者也许是其他一些模式。

2) You want a single C# library to be aware of each of the different versions. 2)您希望单个C#库知道每个不同的版本。

It's this second part that is strange. 这是第二部分,很奇怪。 Normally a client-side library would target a specific version only. 通常,客户端库仅针对特定版本。 It would be the server's responsibility to make sure that new versions of a service were either backwards compatible with old versions, or isolated on a new url. 服务器有责任确保新版本的服务向后兼容旧版本,或隔离在新的URL上。

Ask yourself this - if you were to go ahead and build this library that was aware of multiple versions of the same service, how would it look to the consumers of your library? 问问自己 - 如果您要继续构建这个知道同一服务的多个版本的库,那么它对您库的消费者的看法如何呢? Would they have to specifically tell you which version to use? 他们是否必须明确告诉您使用哪个版本? That's not a concern I would expect to have to be aware of. 这不是我期望必须注意的问题。

var client = new MyClient();
client.DoSomething();   // Makes sense    
client.DoSomethingV2(); // Huh?

Set up API versions, keep your current version as say version 1 and then add a v2 something like 设置API版本,将当前版本保留为版本1,然后添加类似的v2

/current/api?id=x....
/version2/api?id=x...

This allows you to update your api to support new functionality, and then you can set up a sunset plan for your out dated versions. 这允许您更新api以支持新功能,然后您可以为过时版本设置日落计划。

This way you can let your customers move over to your new system without it being an emergency. 通过这种方式,您可以让客户在没有紧急情况的情况下转移到新系统。

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

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