2
votes

I have a class that needs to have all the properties of the parent and the grandparent, but I'm not sure how to structure this in protobuf-net.

Seemingly I should just be able to say:

public class Child : Parent {
   [ProtoMember(1)]
   int childInt;
}

[ProtoInclude(2, typeof(Child))]
public class Parent : GrandParent{
   [ProtoMember(1)]
   int parentInt;
}

[ProtoInclude(2, typeof(Parent))]
public class GrandParent {
   [ProtoMember(1)]
   int grandParentInt;
}

This should serialize all the ProtoMember integer members when I try to serialize an instance of the Child class, as far as I know.

Is this the correct way to do serialization inheritance in protobuf-net? Help me Marc!

2

2 Answers

2
votes

Yes, that is an acceptable way to handle inheritance in protobuf-net. It will certainly work. In protobuf terms, that will be implemented something like:

message GrandParent {
    int grandParentInt = 1;
    optional Parent parent = 2;
}
message Parent {
    int parentInt = 1;
    optional Child child = 2;
}
message Child {
    int childInt = 1;
}

Personally, however, I'd be tempted to introduce a DTO type (rather than serializing your existing model) that simply exposes the required data in the simplest way possible - which probably means all at the same level.

0
votes

Okay so that will work! I made dummy tests, and all the ProtoMembers of the GrandParent will be serialized when you do just the Parent's ProtoInclude statement. This is nice.