Click here to Skip to main content
15,881,881 members
Articles / Programming Languages / XML

XML Serializing Immutable Objects

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
29 Aug 2010CPOL1 min read 11.6K   6   1
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:

C#
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:

C#
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)


Written By
Software Developer (Senior)
Lithuania Lithuania
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionInterface Immutability Pin
r.venant.valery@gmail.com30-Jul-15 5:16
r.venant.valery@gmail.com30-Jul-15 5:16 

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

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