Inherited from generated protocol buffer classes

The protocol buffer documentation warns that ...

You should never add behavior to generated classes, inheriting from them. This will disrupt internal mechanisms and not help object-oriented practice.

Source: Protocol Buffer Basics

My second part of the question:

  • What internal mechanisms can be broken?
  • How is this not good, good OO practice anyway?
+4
source share
1 answer
  • "What goes wrong" will be very implementation specific. If you specified a specific implementation, you could answer, but in a more general sense: this is not a supported scenario, and implementations do not need to work correctly or in general if you are a subclass. This behavior is undefined, with all of this implied. In addition, protocol buffers do not support inheritance, since not all target platforms can support it. Key thoughts:

    • there may be code that checks the incoming object for a list of expected types - if yours does not exist, it may fail
    • it will not process any fields, etc. that you add
    • the whole idea of ​​a serializer is to reliably return what you serialize; if you serialize SomeDerivedClass , then the serializer will not be able to give you this back
    • the whole point of a library like this is to hide the details of the gory implementation; you should not bother with any of the implementations (polymorphism should be designed for)
  • How to care for OO; this is not your type; it is a DTO that is designed to fulfill a specific purpose. A common use may be to match the DTO to / from your domain model, which may be more complex or perhaps encapsulate the DTO if it is useful (perhaps like a facade).

+3
source

All Articles