I'm using flatbuffers for the first time. I've generated my java classes and have tested out serializing / deserializaing a message. Now I'm thinking about how to integrate these in to my JavaFx and Android applications.
Is it valid to pass a DataMessage
or a MessageA
directly in to bussiness logic classes or my UI? It's seems really unwieldly to pass the DataMessage and then have to extract the correct payload type. So, I created wrappers around each type, so I have MessageAWrapper
that contains the MessageA
and Header
. Should the client/UI code directly access the values from a MessageA
, or is it a better practice to copy all the fields from MessageA
in to new primitives defined in MessageAWrapper
. Doing so seems against what Flatbuffers is for, but I also feel dirty exposign the MessageA that has flatbuffers-specific parsing methods and such. But, it's also a bit of work to have my wrapper classes copy over fields and store them, and could be error prone. But, at somewhere in my stack, I may want to, say, stuff a latitude/longitude float pair in to a convenience class called GeographicPosition
. I can do this easily in a wrapper.
So I'm asking for what's the best practices for using flatbuffers / protobufs in an application wrt abstraction / separation of concerns. Most examples I see are just doing quick serialization to compare speed to JSON parsing, and I'm not seeing a full-up practical example of using it in an application to, say, stuff values in to UI controls.
For reference, I created a flatbuffers schema as follows:
table MessageA{
myVal1:float;
myVal2:float;
myVal3:int;
}
union MessagePayload { MessageA, MessageB, MessageC }
// top level message class. Contains a header, and a payload consisting of one of the message types specified in the MessagePayload union.
table DataMessage {
header:Header (required);
payload:MessagePayload (required);
}
root_type DataMessage;
I do this so I can examine the payload type to determine what payload I have and serialize that.