Protocol buffer and OO design

Viewed 5520

I'm using protocol buffer as a wire data-format in a client-server architecture. Domain objects (java beans) will go through following life-cycle.

  1. Used in client side business logic
  2. Converted to protobuf format
  3. Transmitted to the server
  4. Converted back to domain object
  5. Used in server side business logic

"Protocol Buffers and O-O Design" section in ProtoBuf documentation recommends wrapping generated class inside proper domain model.

I'd like to find-out the best appoach.

For e.g. 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 is how domain model is defined. As you can see, the data is completely stored in 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 in all phases of life-cycle, but we only requires protocolbuf format at client-server transmission phase.

Is there any performance issue when accessing proto builder class getter/setter methods specially when proto definition is complex and nested?

2 Answers
Related