Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating an extensible properties class (OOP)

I have an application which supports multiple types and versions of some devices. It can connect to these devices and retrieve various information.

Depending on the type of the device, I have (among other things) a class which can contain various properties. Some properties are common to all devices, some are unique to a particular device.

This data is serialized to xml.

What would be a preferred way to implement a class which would support future properties in future versions of these devices, as well as be backwards compatible with previous application versions?

I can think of several ways, but I find none of them great:

  • Use a collection of name-value pairs:
    • pros: good backward compatibility (both xml and previous versions of my app) and extensibility,
    • cons: no type safety, no intellisense, requires implementation of custom xml serialization (to handle different value objects)
  • Create derived properties class for each new device:
    • pros: type safety
    • cons: have to use XmlInclude or custom serialization to deserialize derived classes, no backward compatibility with previous xml schema (although by implementing custom serialization I could skip unknown properties?), requires casting for accessing properties in derived classes.
  • Another way to do it?

I am using C#, by the way.

like image 522
Groo Avatar asked Oct 15 '22 15:10

Groo


1 Answers

How about something similar to a PropertyBag ?

like image 112
abhilash Avatar answered Oct 21 '22 04:10

abhilash