Click here to Skip to main content
11,433,352 members (58,654 online)
Click here to Skip to main content
Technical Blog

Tagged as

XML serializing immutable objects

, 29 Aug 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
Value objects are immutable, so how can we use XmlSerializer to serialize them? This article answers that question.

If you are familiar with Domain Driven Design concepts, you probably know what a Value Object is. For those who don’t, Value Objects are objects that describe things but have no identity, as opposed to Entities. It is also recommended that Value Object are immutable, which means that they are created with constructors and never modified, which means they only have getter properties.

That ruins it all if you need to serialize your object to XML using XmlSerializer, since it requires the classes you want to serialize to have public getters and setters.

If you try to serialize a class like this:

public class Address
{
	public Address(string city, string street, int house)
	{
		this.City = city;
		this.Street = street;
		this.HouseNumber = house;
	}
 
	public string City
	{
		get;
		private set;
	}
 
	public string Street
	{
		get;
		private set;
	}
 
	public int HouseNumber
	{
		get;
		private set;
	}
}

which matches the definition of Value Object, you will get InvalidOperationException saying that some (or all) of the properties are read-only.

How to solve it without exposing setter properties? Implement IXmlSerializable interface! It requires you to implement three methods:

public class Address: IXmlSerializable
{
	private Address()
	{
	}
 
	public Address(string city, string street, int house)
	{
		this.City = city;
		this.Street = street;
		this.HouseNumber = house;
	}
 
	public string City
	{
		get;
		private set;
	}
 
	public string Street
	{
		get;
		private set;
	}
 
	public int HouseNumber
	{
		get;
		private set;
	}
 
	#region IXmlSerializable Members
 
	public System.Xml.Schema.XmlSchema GetSchema()
	{
		return null;
	}
 
	public void ReadXml(System.Xml.XmlReader reader)
	{
		this.City = reader.ReadElementString();
		this.Street = reader.ReadElementString();
		this.HouseNumber = int.Parse(reader.ReadElementString());
	}
 
	public void WriteXml(System.Xml.XmlWriter writer)
	{
		writer.WriteElementString("City", this.City);
		writer.WriteElementString("Street", this.Street);
		writer.WriteElementString("HouseNumber", this.HouseNumber.ToString());
	}
 
	#endregion
}

You can control the way XML is written out. Also notice the private parameterless constructor – it is also a requirement of XmlSerializer (it requires a parameterless constructor, it can have any access modifier, but we use private, since we only want objects created with the main constructor, where you pass the values in). You may also want to implement IXmlSerializable explicitly, to keep your class interface clean.

I believe it’s not very fancy, but it does get the job done without violating Value Object definition. I’d be interested to see any other ways to implement this too, since I failed to find anything useful on the internet. :)

License

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

Share

About the Author

Gediminas Geigalas
Software Developer (Senior)
Lithuania Lithuania
No Biography provided

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150428.2 | Last Updated 29 Aug 2010
Article Copyright 2010 by Gediminas Geigalas
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid