我应该将gRPC的protobuf消息转换为DTO吗? – 最佳实践

huangapple go评论61阅读模式
英文:

Should i convert gRPC protobuf message to DTO? - Best Practice

问题

最近,我开始使用gRPC消息与微服务进行交互。直到现在,我一直在使用REST API与DTO来在代码本身中保持解耦合和封装。

现在我在使用gRPC的protobuf消息时,我开始思考在这里应该是什么最佳实践——在我大声思考的时候,我会说在应用程序的入口获取消息时,最好将其转换为DTO,以保持与gRPC protobuf消息和代码本身的解耦。从另一个角度来看,我只是在转换对象到对象,不知道这些开销是否是必要的或者是否需要。

期待您对这个问题的看法。

英文:

Lately, I started using gRPC messages to interact with microservices. Until now I was using the REST API's with DTOs do keep on decoupling and encapsulation in the code itself.

Now I'm while using the gRPC protobuf msgs, I started wondering what shell be the best practice here - while thinking about it out loud I would say that when getting the message in the entry point of the application the best would be to convert it to DTO to keep on the decupling from gRPC protobuff messages and the code itself, from the other point of view - i'm just converting object to object and wondering if these overhead is mandatory / required

Looking forward to your thoughts around this one 我应该将gRPC的protobuf消息转换为DTO吗? – 最佳实践

答案1

得分: 4

根据您的需求以及服务在长期内需要解耦的程度而定。据我所知,甚至有些服务/人员倾向于将 proto 直接写入数据库(即以 proto 字符串 / 字节字符串或字节数组的形式),以减少映射开销和出于其他原因(我猜是这样的)。

话虽如此,就个人而言,我认为领域模型数据传输对象(Protobuf 消息)应尽可能分开。因此最好为您的请求和响应进行额外的映射,这将有助于未来对接口定义语言(IDL)或 proto 定义的更改。

>如果您完全依赖资源/ API 层的实现,
> 在您的情况下是 gRPC,甚至轻微的库更新都可能会破坏一些东西,
> 而且要遍历使用了 proto 模型的所有地方将是一项麻烦的工作。

>此外,它还可以帮助您轻松适应变化,
> 比如如果您的系统选择完全依赖另一个 API 架构

最后,我认为没有硬性规定需要遵循,但完全取决于您和您的需求,但最好将其转换为您自己的模型,而不是直接使用它,这样可以更好地控制 API 层和业务层的分离,并且对长远发展真的很重要。

英文:

Well it depends on your requirement and how decoupled your service needs to be (for the long run). As far as i know, some services/people even tend to write the proto directly to the database (i.e in proto-string / byte string or byte[]) to reduce the mapping overhead and for other reasons i guess.

Having said that, personally i think Domain Model and Data Transfer Objects (Protobuf Message) should be separated as much as possible. So its better to have extra mapping for your request and response which would help you for future changes to the IDL or proto definitions.

>If you fully rely on the resource/api layer implementation
> which is gRPC in your case, even a little library update may break
> things and it would be a hassle to go through all the places where the
> proto model has been used

> Further more it would helps you accommodate with changes easily
> like if your system chooses to fully rely on another API schema

Lastly i don't think there are any hard rules to follow but it is fully up-to you and your requirement, but it's better to convert it to your own models rather than using it directly, allows you to have more control over the api layer and your business layer segregation and would really matter on the long run.

huangapple
  • 本文由 发表于 2020年9月17日 16:00:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/63933723.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定