Creating an Extensible Property Class (OOP)

I have an application that supports several types and versions of some devices. It can connect to these devices and receive various information.

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

This data is serialized in xml.

What would be the preferred way to implement a class that will support future properties in future versions of these devices, as well as be backward compatible with previous versions of applications?

I can think of several ways, but I do not consider them insignificant:

  • Use a collection of name-value pairs:
    • pros : good backward compatibility (both xml and previous versions of my application) and extensibility,
    • cons : no type security, no intellisense, implementation of custom XML serialization required (for processing various objects value)
  • Create a class of derived properties for each new device:
    • pros : security type
    • cons : do you need to use XmlIncludeeither custom serialization to deserialize derived classes, without backward compatibility with the previous xml schema (although, while implementing custom serialization, could I skip unknown properties?), a listing is required to access the properties in the derived classes.
  • Another way to do this?

I am using C # by the way.

+5
6
+2

, SoapFormatter. , .

XML- XmlInclude, , , .

+1

/ .

- , /, op /. (, ) , .

, "PrintsColor", "true" "false". - PrintsColor = "CMYK", Printer .

, , : (, checkForValidBoolean()) , , , XML.

intellisense - , . Intellisense .

- OO, . IMO, clunkier , .

+1

, - .

xml. xsd.exe.

.net , xml.

public static String toXmlString<T>(T value)
{
    XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
    StringWriter stringWriter = new StringWriter();
    try { xmlSerializer.Serialize(stringWriter, value); }
    catch (Exception e)
    {
        throw(e);
    }
    finally { stringWriter.Dispose(); }
    String xml = stringWriter.ToString();
    stringWriter.Dispose();
    return xml;
}

public static T fromXmlFile<T>(string fileName, Encoding encoding)
{
    Stream stream;
    try { stream = File.OpenRead(fileName); }
    catch (Exception e)
    {

      e.Data.Add("File Name", fileName);
      e.Data.Add("Type", typeof(T).ToString());
      throw(e);
    }

    BufferedStream bufferedStream = new BufferedStream(stream);
    XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));

    TextReader textReader;
    if (encoding == null)
        textReader = new StreamReader(bufferedStream);
    else
        textReader = new StreamReader(bufferedStream, encoding);

    T value;
    try { value = (T)xmlSerializer.Deserialize(textReader); }
    catch (Exception e)
    {
        e.Data.Add("File Name", fileName);
        e.Data.Add("Type", typeof(T).ToString());
        throw(e);
    }
    finally
    {
        textReader.Dispose();
        bufferedStream.Dispose();
    }
    return value;
}
0

, , Decorator Pattern. , , . , , . . :

alt text

You can see a more detailed description on the Wikipedia page. After that, it would be easy to refine some serialization to tell the program which decorators will load.

0
source

The general idea of ​​what you are trying to accomplish here is exactly what EAV solves . EAV is the template most commonly used for database development, but this concept is equally applicable to applications.

0
source

All Articles