views:

26

answers:

2

I was currently discussing with a few friends on how to design easily serializable and deserializable classes. We are currently using a "home grown" attempt involving lots of spooky interfaces, custom attributes and loads of reflection which has by now outgrown the extend it was designed for. So we are looking for alternatives. Preferably mature frameworks already in production quality. But so far we couldn't find anything that fits our needs.

Our current system provides the following features:

  • It uses XML, which is not directly vital. But: The content should be human readable and produce "nice" diffs when using it in the context of a VCS. So I guess XML is reasonable.
  • It works with Silverlight, XNA and the "normal" .Net Framework. We are currently only using the "normal" .Net framework but would not like to cut Silverlight. XNA is a bonus, but not too strictly required.
  • It supports "partial" deserialization, where it basicly only loads a subset of the our resources properties. This is not strictly required, but dramatically speeds up loading times because resources can be loaded "on demand".

We know that especially the last requirement is a little unusal, but maybe we are lucky and somebody knows a decent framework? We would also love to hear from frameworks not quite fitting our requirements and outside the .Net Standard Library, as we couldn't really find many.

A: 

You can try serializing it with System.Xml and the XmlSerializer and then use System.Xml.Linq and XDocument for partial deserialization in .NET 3.5. Perhaps you need other approaches in Silverlight and XNA.

Steven
Hm, the idea of serializing it automatically and deserialize it myself sounds not too bad. Decreases at least the possibility in serialization errors.
Marcus Riemer
+1  A: 

Have you tried using either the XmlSerializer or the BinaryFormatter (and related) classes?

Most things can be serialised and deserialised painlessly using either of those two, and in many cases even if there is something preventing serialisation using those classes (e.g. circular references) its fairly easy to get around by using either the serialisation attributes, or in extreme cases by implementing IXmlSerializable and ISerializable.

The .Net serialisation mechanisms are incredibly versatile - your objects need to be pretty special before you rule out the standard .Net serialisation techniques.

Kragen
Yes, I also took a look at the DataContracts. None of them allows "partial" deserialization and all of them require entirely public getters and setters.
Marcus Riemer