使用协议缓冲区作为通用数据对象?

发布于 2024-08-12 15:32:10 字数 249 浏览 5 评论 0原文

我们引入协议缓冲区作为某些后端 RPC 服务的新传输。由于在不同形式的相似对象之间手动传输数据存在阻力,因此我可以预见 Protocol Buffer 实例将在堆栈中向上传递,而不仅仅是传递到 RPC 服务器接口。

这是我应该尽量避免的事情吗?将协议缓冲区对象视为普通数据持有者是否安全,并且可以快速有效地将其转换为二进制文件或从二进制文件中转换出来,非常方便?

我认为它是生成数据对象的好方法的另一个原因是必填/可选字段的概念和自动生成的构建器接口。

We're introducing protocol buffers as the new transport for some back end RPC services. Because there's resistance to manually shuttling data between different forms of similar objects, I can forsee the Protocol Buffer instances being passed up the stack a bit higher than just to the RPC server interface.

Is this something that I should try to avoid? Is it safe to treat a protocol buffer object like a plain data holder, with the nice convenience that it can quickly and efficiently be transformed into and out of binary?

The other reason I see it as being a nice way to generate data objects is that the notion of required/optional fields and the automatically generated builder interface.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

月下伊人醉 2024-08-19 15:32:10

嗯,以这种方式使用它们并不是非常方便,因为它们是不可变的 - 您可以传递构建器,但这会导致类型名称相当长。这也意味着您仅限于协议缓冲区(以及您自己的消息)支持的数据类型。

这样做安全,但并不总能创造出最好的设计。另一方面,有时这正是医生所吩咐的:)

我建议你尝试一下 - 这里没有“一刀切”。

Well, they're not terribly convenient to use that way as they're immutable - you could pass the builders around, but that makes for rather long type names. It also means you're limited to the data types supported by protocol buffers (and your own messages).

It's safe to do this, but it doesn't always create the nicest of designs. On the other hand, sometimes it's just what the doctor ordered :)

I suggest you experiment - there's no "one size fits all" here.

末が日狂欢 2024-08-19 15:32:10

一般来说,我设计系统的各个层,以便一层的实现细节不会泄漏到另一层。我没有对 Google 协议缓冲区的直接经验,但听起来您想在系统的传输层和更高层使用相同的表示形式。

如果您决定停止使用 Protocol Buffers 作为传输表示,那么使用其他东西有多容易?

In general, I design the layers of my systems so that implementation details from one layer don't leak into one another. I don't have direct experience of Google's Protocol Buffers, but it sounds like you want to use the same representation for the transport and at higher layers of your system.

If you decided you wanted to stop using Protocol Buffers as the transport representation, how easy would it be to use something else?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文