We have a somewhat complex hierarchy of classes in our entity model. We have a base class BaseEntity for all entities, then some “level 2” classes derived from BaseEntity, some “level 3” classes derived from any “level 2” class, and a lot of “level 4” classes, derived from any “level 3” class. That means, we have something like this:
- BaseEntity
- DerivedEntity_1
- DerivedEntity_1_1
- DerivedEntity_1_1_1
- DerivedEntity_1_1_2
- DerivedEntity_1_1_3
- DerivedEntity_1_2
- DerivedEntity_1_2_1
- DerivedEntity_1_2_2
- DerivedEntity_1_1
- DerivedEntity_2
- DerivedEntity_2_1
- DerivedEntity_2_1_1
- DerivedEntity_2_2
- DerivedEntity_2_2_1
- DerivedEntity_2_2_2
- DerivedEntity_2_2_3
- DerivedEntity_2_2_4
- DerivedEntity_2_1
- DerivedEntity_1
And so on and so forth…
Now, when transferring instances of these types via WCF using protobuf-net, only the properties of the “level 4” classes are transferred – all properties from the base classes are lost!
From several other StackOverflow threads (here, here, and here), I read that this is the default behavior and that you have two options to work around it:
- Use the
[ProtoInclude]attribute on the base class. - Configure the inheritance at runtime using a
TypeModelandAddSubType().
However, none of these options are feasible in our scenario since we have a lot of derived classes. Option 1 would mean hundreds of attributes on the level 1 base class. Option 2 would mean a lot of calls to AddSubType().
Are there any other options?
Ultimately, it needs to understand how it should store the data, so that you can get it back. It can’t just guess… So: the only robust way to do that is to define the model somewhere. That doesn’t have to be in raw code, though: for example, maybe it could be stored in an external file that you include and process (via AddSubType) at runtime.