Yossi Dahan [BizTalk]


Thursday, July 28, 2005

Unexpected behaviour with the configuration block and Xml Serialization

As been discussed so many times we're facing over and over again the issue of storing configuration, and it's quite obvious the last word has not beed heared on this (I'm probably going to deal more with this subject in the coming few weeks)

Yesterday I've been playin around with the configuration application block in the entterprise library and Xml Serialization when I found out something about serialization that surprised me (if it's obvious and I'm the only one puzzled by this forgive me for waisting your time..)

I've created a class to represent a piece of data that I will store using the config block's xml provider.

Since in my case, after the initial creation of a piece of data it is expected to be read-only I've created only getters in the properties of my class, allowing only an internal constructor to receive parameters and update the members, which meant normal access to the configuration data will not be allowed.
an additional parameter-less constructor was created to satisfy the serialization rules (as kindly instructed by the compiler)

I then created an instance of my data class (providing data throught the first constructor) and stored it witht the configuration block, but when I opened the saved xml file I found out the although data structure was saved correctly, it did not contain any values! all the members we're empty.

After a little bit of investigation I've noticed that the empty constructor was called everytime the configuration was stored. which means a new instance has been created.
My assumption was that the XmlSerializer calles the empty constructor to create a new instance at some stage of it's operation and without setters to the members, the data could not be provided to that newly created the instance and therefore it's empty.

I've added setters to all the properties and indeed now the configuration block managed to stor the information correcly.

I don't know why this is happening, and if there's another way to handle this case so I had to leave the setters for now, not a great concern after all.


Post a Comment

<< Home