Protocol Buffer and OO Design

I use the protocol buffer as a wired data format in a client-server architecture. Domain objects (java beans) will go through the next life cycle.

  • Used in client-side business logic.
  • Converted to protobuf format
  • Sent to server
  • Converted back to a domain object
  • Used in server side business logic.

The "Protocol Buffers and OO Design" in the ProtoBuf documentation recommends wrapping the generated class inside the correct domain model.

I would like to find the best app.

For example, I have a simple proto definition.

package customer; option java_package = "com.example"; option java_outer_classname = "CustomerProtos"; message Customer { required string name = 1; optional string address = 2; } 

This determines the domain model. As you can see, the data is completely stored in the proto-builder object.

 package com.example; public class CustomerModel { private CustomerProtos.Customer.Builder builder = CustomerProtos.Customer.newBuilder(); public String getName() { return builder.getName(); } public void setName(String name) { builder.setName(name); } public String getAddress() { return builder.getAddress(); } public void setAddress(String address) { builder.setAddress(address); } public byte[] serialize() { return builder.build().toByteArray(); } } 

Is this a good practice? because these objects are used at all stages of the life cycle, but we need a protocol format only in the client-server transfer phase.

Is there a performance issue when accessing the getter / setter methods of the proto builder class specifically when the prototype definition is complex and nested?

+8
oop protocol-buffers
source share
2 answers

I have no experience with protocol buffers, but I would not recommend implementing objects in your domain that are adapted to a specific serialization / transfer structure. You may regret it in the future.

The objects and logic of the software application should be as independent as possible of the specific implementation problems (in your case of serialization / transfer), because you want your domain to be easy to understand and can be reused / maintained in the future.

If you want to define your domain objects regardless of serialization / transfer, you have two options:

  • Before serialization / transfer, you copy information to the protocol, buffer specific objects, and send them to your server. There you will need to copy the information back to your domain objects.
  • Use a serialization library without protocols, such as Kryo or ProtoStuff , to directly transfer domain objects to the server.

The disadvantages of option 1 are that your domain is defined twice (which is undesirable with respect to changes) and copying information (which creates vulnerable and unsupported code).

The disadvantages of option 2 are that you lose the evolution of the circuit (although ProtoStuff seems to support it ), and the full (potentially large) graph of objects is serialized and transferred. Although you can crop the object graph (manually or using JGT ) before serializing / wrapping .

+5
source share

We created protobuf-converter to solve the problem of converting domain model objects to Google Protobuf messages and vice versa.

How to use it:

Domain model classes that must be converted to protobuf messages must satisfy the following conditions:

  • The class must be marked with the @ProtoClass annotation, which contains a link to the corresponding protobuf message class.
  • Field fields must be marked with @ProtoField annotation. These fields must have getters and setters.

eg:.

 @ProtoClass(ProtobufUser.class) public class User { @ProtoField private String name; @ProtoField private String password; // getters and setters for 'name' and 'password' fields ... } 

Code for converting a User Instance to a related protobuf message:

 User userDomain = new User(); ... ProtobufUser userProto = Converter.create().toProtobuf(ProtobufUser.class, userDomain); 

Code for inverse conversion:

 User userDomain = Converter.create().toDomain(User.class, userProto); 

Converting lists of objects is similar to converting a single object.

+2
source share

All Articles