With an application I’m currently developing I’ve come across the need to serialize some business objects to a number of different proprietary formats.
I’m struggling with coming to a decision on the best way to approach this and would like some opinions.
I know that .NET provides us with the ISerializable interface which I can implement on my business objects, however I’m struggling to understand how this would cope when I need to serialize to maybe 3 different formats?
On top of this I’m not too keen on the idea of having all of the serialization specific code “gumming up” my nice clean business objects – I’d prefer to parcel off that responsibility to a dedicated object. This seems to make sense as well when there are a number of different formats as serialization options could then be extended in the future by simply extending a base class, or modified without editing the business objects themselves. Then again, I’d rather not “re-invent the wheel”.
I’m wondering what your collective experience and opinions are in cases like these?
Is ISerializable a viable option for multiple formats?
Or is a custom class or service more suited to the task?
Are there any design patterns or further framework features that cover this area?
You can use
ISerializableto get yourself a form of generic intermediate store of the data for each object (SerializationInfo), and you could then implement three separate handlers for that intermediate type to turn them into the final form. Personally I certainly wouldn’t want to see three forms of serialization code in a single business object.It may be simpler, depending on the number and complexity of your objects, for you to create your own interface which each class must support serializing to. This then allows you complete flexibility, at the cost of not being usable with the standard serialization bits.