Serialization for Rapid Application Development: A Better Approach

Introduction

A common problem that developers have with serialization is dealing with the unknown derived classes of the serialized types. Often serialization errors occur when Typed object collections have instances of an object derived Type. This is due to the XmlSerializer not knowing how to resolve the type of the object.

A common workaround is to register those derived types using the [XmlInclude(Type type)] custom attribute. While this works, it does not support future extensibility. Any new derived classes are not elegantly or easily handled. Custom serializers would have to be written on a case by case basis.

This is unacceptable for supporting rapid development. To solve the issue of supporting elegant and rapid development, I have created a simple service to make use of the XmlSerializer constructors with the inclusion of Type[] extraTypes and custom attributes that can be adorned to class declarations.

The solution presented is easy to use, elegant and requires a minimum of developer intervention to implement and make use of.

Using the Code

The use of this code is very simple and requires two things. The first requirement is that you adorn your class declarations with the custom attributeSerializableExtraType. The second requirement is that when you do your normal serialization and deserialization, you pass in the extraTypesthat the SerializableExtraTypes is tracking. Static serialize and deserialize methods are part of the class.

Adorn a class with the required attributes like this:

 Collapse
[Serializable]
// example of registering class and all derived classes
[SerializableExtraType(typeof(Foo))]
// example of registering class with an unrelated class
[SerializableExtraType(typeof(SomethingElse))]
// example of registering with multiple classes in same attribute
[SerializableExtraType(new Type[] { typeof(ClassOne), typeof(ClassTwo) })]
public class Foo { public Foo() {} }

As with all serialization in .NET, all classes to be serialized also require the [Serializable] attribute. Here we simply add an additional attribute of[SerializableExtraType(Type type)]. Any standard serialization rules still apply. You do not need to adorn any derived classes of Foo as it will be inherited in all derived classes. This means you could add the attribute to an abstract base class and all your classes would be registered. I have given an example here where I register Foo for itself as well as for several other Types. This shows all possible variations of using the[SerializableExtraType] attribute.

Here is an example of serializing and deserializing an object that contains a collection of Foo and Foo derived objects:

 Collapse
string s = SerializableExtraTypes.Serialize(someObject, typeof(Foo));
object o = SerializableExtraTypes.Deserialize
            (strSomeObject, typeof(SomeObject), typeof(Foo));

That's it. It's very simple code and easy to use but the concept is wonderful in its simplicity and elegance. This in effect is like attaching[XmlInclude] from the opposite direction.

History

  • 25th April, 2008: Initial post

This is the first publication of this method. 

License

Comments