19

I couldn't find any "Best Practices" online for usage of gRPC and protobuf within a project. I'm implementing an event-sourced server side app. The core defines the domain aggregates, events and services without having external dependencies. The gRPC server calls the core services passing in request objects which eventually translates into events being published. Events are serialized using protobuf and published on the wire. We're currently in a dilemma on whether our events should be the protobuf generated classes directly, or should we keep the core and events separate and implement a mapper/serializer layer to translate events between protobuf <-> core

If there's another approach we're not considering, please guide us :)

Thanks for the help.

2 Answers 2

8

Domain Model Objects and Data Transfer Objects (Protobuf Message) should be separated as much as possible. For this the best way is to transform your Domain Model Objects into Google Protobuf Messages and vice versa. We've made a protobuf-converter to make it extremely simple.

Sign up to request clarification or add additional context in comments.

5 Comments

While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review
Mike, I hope this answer will be alive forever stackoverflow.com/questions/11773552/…
@Mike: Given that the question asks for an approach, and the answerer has outlined the approach along with the link, I would not classify this as a "link-only" answer. See meta.stackoverflow.com/questions/287563/…
Would you say this impacts the Golang implementation equally?
I would say yes. If you are familiar with why ViewModels should be separated from Domain models. Similar idea applies here.
1

Protobufs are really good for serialization and backwards compatibility, but not so good at being first class Java objects. Adding custom functionality to protos is currently not possible. You can get a lot of the benefits by using Protobufs at the stub layer, wrap them in one of your event Pojos, and pass them around internally as such:

public final class Event {
 private final EventProto proto;

 public void foo() {
   // do something with proto.
 }
}

Most projects don't change their .proto file that often, and almost never in a backwards incompatible way (neither wire nor API). Having to change a lot of code because of proto changes has never been a problem in my experience.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.