Click here to Skip to main content
Click here to Skip to main content

Serialization for Rapid Application Development: A Better Approach

, 25 Apr 2008 CPOL
Rate this:
Please Sign up or sign in to vote.
Serialize and Deserialize your objects with ease and simplicity

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 attribute SerializableExtraType. The second requirement is that when you do your normal serialization and deserialization, you pass in the extraTypes that the SerializableExtraTypes is tracking. Static serialize and deserialize methods are part of the class.

Adorn a class with the required attributes like this:

[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:

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. Please send any comments or suggestions on how to improve it. You can email me at danatcofo@gmail.com

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Daniel Gidman
Architect Stackify
United States United States
No Biography provided
Follow on   Twitter   Google+

Comments and Discussions

 
QuestionYour approach versus DataContractSerializer PinmemberDenis Vuyka20-May-08 20:32 
AnswerRe: Your approach versus DataContractSerializer PinmemberDaniel Gidman21-May-08 1:44 
QuestionSample? PinmemberMr.PoorEnglish26-Apr-08 3:56 
GeneralRe: Sample? [modified] PinmemberDaniel Gidman26-Apr-08 9:34 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web04 | 2.8.141015.1 | Last Updated 25 Apr 2008
Article Copyright 2008 by Daniel Gidman
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid