Protocol buffer and OO design

后端 未结 2 1155
小蘑菇
小蘑菇 2021-02-05 08:10

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
2条回答
  •  梦如初夏
    2021-02-05 08:49

    We've made a protobuf-converter to solve the problem of transformation of your Domain Model Objects into Google Protobuf Messages and vice versa.

    How to use it:

    Domain model classes that have to be transformed into protobuf messages must satisfy conditions:

    • Class has to be marked by @ProtoClass annotaion that contains reference on related protobuf message class.
    • Class fields has to be marked by @ProtoField annotaion. These fields must have getters and setters.

    E.g.:

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

    Code for conversion User instance into related protobuf message:

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

    Code for backward conversion:

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

    Conversion of lists of objects is similar to single object conversion.

提交回复
热议问题