最佳实践以及如何在客户端的 C# 包装器中支持不同版本的 REST API [英] Best Practices and How to support different versions of REST APIs in C# wrapper on client-side

查看:13
本文介绍了最佳实践以及如何在客户端的 C# 包装器中支持不同版本的 REST API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个 C# 包装器来支持我们公司项目的 REST API.但是,这些 API 现在正在发生变化 - 在 URL(可能会在 URL 中引入版本号)和它期望并返回的数据对象方面.

I have written a C# wrapper to support our company project's REST APIs. However, these APIs are changing now - in terms on URL (may introduce version number in URL) and data objects it expects and returns back.

我想知道在我的 c# 包装器中支持不同版本的 REST API 的最佳实践是什么.

I would like to know what would be the best practice on supporting different versions of REST APIs in my c# wrapper.

我应该如何做 - 在代码设计和类定义方面 - 以便一个包装器可以与不同版本的 API 无缝协作 - 并且它也应该是可扩展的 - 以便将来任何更新版本的 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.

我编写的 c# 包装器正在使用我们的 Web 服务 API.我已经在使用 RestSharp 客户端在 c# 包装器中使用我们的 Web 服务 API.

The c# wrapper I have written is consuming our web service APIs. I am already using RestSharp client to consume our web service APIs in c# wrapper.

推荐答案

你的问题有点奇怪.让我重申你所说的:

There's a subtle weirdness to what you are asking. Let me reiterate what you have stated:

1) 在您的组织中,同一服务有多个版本.也许它们是像 Bob 建议的那样建立的,/current/api.../v1/api.../v2/api...等等.或者可能是其他一些模式.

1) There are multiple versions of the same service at your organization. Perhaps they are established like Bob suggested, /current/api... /v1/api... /v2/api... etc. Or perhaps some other pattern.

2) 您希望单个 C# 库了解每个不同的版本.

2) You want a single C# library to be aware of each of the different versions.

这第二部分很奇怪.通常,客户端库仅针对特定版本.服务器有责任确保服务的新版本向后兼容旧版本,或者隔离在新 url 上.

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.

问问自己 - 如果您要继续构建这个能够识别同一服务的多个版本的库,对于您的库的使用者来说,它会是什么样子?他们是否必须特别告诉您使用哪个版本?这不是我希望必须意识到的问题.

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?

这篇关于最佳实践以及如何在客户端的 C# 包装器中支持不同版本的 REST API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆