SIGN UP MEMBER LOGIN:    
ARTICLE

XML Serialization/Deserialization of Immutable Objects

Posted by Matthew Cochran Articles | Learn .NET April 18, 2010
Here’s a brief example of how to serialize and deserialize immutable objects to XML.
Reader Level:

Immutable objects are valuable in many ways.  They are the cornerstone of many functional languages and help to greatly reduce complexity.  We don't have to worry about state so references can be shared without the usual worry about nasty side effects and so we don't have to sit around looking at the code editor scratching our heads thinking: "How did the value get to be X?".  Even difficult multi-threading concurrency issues go away with immutability.  Also, we can treat immutable objects more like mathematical entities and adding operators makes the code consuming them even simpler.

The problem is when we want to pass these objects around to disconnected systems and persist them.  If the object is immutable it is not as easy to rebuild the instance because we have only 'getters' and no 'setters' so we have to approach it differently than we would with a mutable object.  The solution is using the [DataContract] and [DataMember] attributes in the correct places.

Let's say we have an immutable 'Dog' class:

public class Dog

{

    public Dog(String name)

    {

        m_Name = name;

    }

 

    private readonly String

        m_Name;

 

    public String Name { get { return m_Name; } }

}

 

If we want to serialize, the serialization engine needs access to the read only property.  We can accomplish this by adding a reference to System.Runtime.Serialization and decorating our 'Dog' class with a [DataContract] attribute and decorating our read only member variable with the [DataMember] attribute.

[DataContract]

public class Dog

{

    public Dog(String name)

    {

        m_Name = name;

    }

 

    [DataMember(Name="Name")]

    private readonly String

        m_Name;

 

    public String Name { get { return m_Name; } }

}

 

And that's pretty much it, now we can serialize and deserialize our immutable object with a DataContractSerializer.

I'll add the code for the serialization and have it live in the dog class below so you can see the implementation.  Just a side note… The serializer can live anywhere and it is not required that it be a member variable of the class being serialized as in the following code sample.

[DataContract]

public class Dog

{

    public Dog(String name)

    {

        m_Name = name;

    }

 

    private static readonly DataContractSerializer

        c_Serializer = new DataContractSerializer(typeof(Dog));

 

    [DataMember(Name="Name")]

    private readonly String

        m_Name;

 

    public String Name { get { return m_Name; } }

 

    public String ToXml()

    {

        using(MemoryStream stream = new MemoryStream())

        {

            c_Serializer.WriteObject(stream, this);

            stream.Position = 0;

 

            using (StreamReader reader = new StreamReader(stream))

            {

                return reader.ReadToEnd();

            }

 

        }

    }

 

    public static Dog FromXml(String str)

    {

     

        using(MemoryStream stream = new MemoryStream())

        using(StreamWriter writer = new StreamWriter(stream))

        {

            writer.Write(str);

            writer.Flush();

 

            stream.Position = 0;

 

            return c_Serializer.ReadObject(stream) as Dog;

 

        }

       

    }

}

 

 And now we could consume our class with the following code

Dog fido = new Dog("Fido");

 

String flattenedFido = fido.ToXml();

 

Dog reconstitutedFido = Dog.FromXml(flattenedFido);

 

String name = reconstitutedFido.Name;

 
Until next time,
Happy coding

Login to add your contents and source code to this article
share this article :
post comment
 
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
Team Foundation Server Hosting
Become a Sponsor