I have many (at the moment around 30) different message classes in an application I am creating. Each of these messages need to be serialized and deserialized. However, the process of serializing and deserializing is mostly unique to each class (the format of the serialized message is specified in a document, and says which bits in the message goes where), but some are pretty straight forward and use the same process.
I thought about having each of them implement an interface, with
Deserialize() methods. However, it seems cumbersome to implement this for each message, especially when many of the messages do in fact use the same code to do this serializing and deserializing.
I thought about having a class that contains a
Dictionary<T, Func<IMessage, byte>, and a function to map a function to serialize to a specific type. Then use this dictionary to get the appropriate serializer.
Any thoughts? How would you go about this problem?