这些是 RestSharp 和 ServiceStack 的客户端代码之间的主要区别吗? [英] Are these the main differences between RestSharp and ServiceStack's Client Code?

查看:59
本文介绍了这些是 RestSharp 和 ServiceStack 的客户端代码之间的主要区别吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直无法做出明确的选择,希望有人(或几个人的组合)可以指出使用 RestSharp 与 ServiceStack 的客户端服务之间的区别(请记住,我已经在使用 ServiceStack 了)我的服务).这是我到目前为止所拥有的(仅差异).该列表相当小,因为它们确实非常相似:

I have been unable to make a definitive choice and was hoping that somebody (or a combination of a couple of people) could point out the differences between using RestSharp versus ServiceStack's client services (keeping in mind that I am already using ServiceStack for my service). Here is what I have so far (differences only). The list is fairly small as they are indeed very similar:

  • 来自我已经创建的服务 POCO 对象的流畅验证
  • 客户端和服务都使用一个 API
  • 代码可读性更好(即 Get<>()、Post<>())
  • 我的一些字符串必须写出(即,如果我使用查询参数发出 GET 请求,我必须在我的代码中创建该字符串)
  • 我必须为每个请求/响应类型(JsonServiceClient、XmlServiceClient)创建一个不同的类
  • 几乎所有东西都可以是 POCO(即,如果我使用查询参数发出 GET 请求,我只需通过代码添加参数)
  • 在请求/响应类型之间切换很简单 (request.RequestFormat = DataFormat.Json/Xml)
  • 手动验证(超出数据注释中的验证)
  • 要学习的两个 API(这是次要的,因为它们都相当简单)
  • 代码一目了然(勉强)(即 request.Method = Get/Post.. 并且主要调用是 Execute())

我倾向于 RestSharp,因为它更倾向于直接使用 POCO 和很少的字符串操作,但是我认为 ServiceStack 可能是可以接受的,以获得更容易阅读的验证和代码.

I was leaning towards RestSharp since it tends more towards straight POCO use and very little string manipulation, however I think ServiceStack might be acceptable to gain the validation and code that is more easily read.

那么,这里是问题:

  • 你更喜欢哪个?
  • 为什么是一个而不是另一个?

我知道这不是一个完全主观的问题,但至少我正在寻找这个问题的答案(这是主观的):

I know this is not a totally subjective question, but at bare minimum I am looking for the answer to this question (which is subjective):

  • 我的发现是否有任何错误和/或我遗漏了什么?

推荐答案

作为 ServiceStack 的项目负责人,我可以列出一些ServiceStack 服务客户端:

As the project lead of ServiceStack I can list some features of the ServiceStack Service clients:

ServiceStack 服务客户端在使用 ServiceStack Web 服务及其约定方面固执己见.即它们具有对结构化验证和错误处理的内置支持以及所有客户端实现相同的接口,因此您可以将 相同的单元测试用作集成测试 在每个 JSON、JSV、XML、SOAP 甚至 Protobuf 服务客户端上 - 允许您无需更改代码即可轻松更改服务使用的端点/格式.

The ServiceStack Service Clients are opinionated in consuming ServiceStack web services and its conventions. i.e. They have built-in support for structured validation and error handling as well as all clients implement the same interface so you can have the same unit test to be used as an integration test on each of the JSON, JSV, XML, SOAP and even Protobuf service clients - allowing you to easily change the endpoint/format your service uses without code-changes.

基本上,如果您正在使用 ServiceStack Web 服务,我建议您使用 ServiceStack 客户端,这将允许您重用您定义 Web 服务所用的 DTO,从而为您提供端到端的类型化 API.

Basically if you're consuming ServiceStack web services I'd recommend using the ServiceStack clients which will allow you to re-use your DTOs you defined your web services with, giving you a typed API end-to-end.

如果您使用的是 3rd Party API,我会推荐 RestSharp,它是一种更通用的 REST 客户端,非常适合该任务.此外,由于 ServiceStack 只是通过线路返回干净的 DTO,它也可以从 RestSharp 轻松使用,如果您喜欢它的 API 也是一个不错的选择.

If you're consuming a 3rd Party API I would recommend RestSharp which is a more general purpose REST client that is well suited for the task. Also as ServiceStack just returns clean DTOs over the wire it would also be easily consumable from RestSharp, which if you prefer its API is also a good option.

ServiceStack 现在通过其 HTTP 客户端实用程序扩展方法提供了使用 3rd Party API 的替代选项围绕常见的 HttpWebRequest 访问模式提供 DRY、可读的 API,例如:

ServiceStack now provides an alternative option for consuming 3rd Party APIs with its HTTP Client Util extension methods that provides DRY, readable API's around common HttpWebRequest access patterns, e.g:

List<GithubRepo> repos = "https://api.github.com/users/{0}/repos".Fmt(user)
    .GetJsonFromUrl()
    .FromJson<List<GithubRepo>>();

网址扩展

var url ="http://api.twitter.com/statuses/user_timeline.json?screen_name={0}"
    .Fmt(name);
if (sinceId != null)
    url = url.AddQueryParam("since_id", sinceId);
if (maxId != null)
    url = url.AddQueryParam("max_id", maxId);

var tweets = url.GetJsonFromUrl()
    .FromJson<List<Tweet>>();

替代内容类型

var csv = "http://example.org/users.csv"
    .GetStringFromUrl(acceptContentType:"text/csv");

更多示例可从 HTTP Utils wiki 页面获得.

这篇关于这些是 RestSharp 和 ServiceStack 的客户端代码之间的主要区别吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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