# Wednesday, September 02, 2009

I was working on this little app I wrote a while ago and wanted to add some features requiring (de)serialization. So, I took the original class and made it a DataContract so I could use it with the DataContractSerializer. The class then looked more or less like this:

[DataContract]
public class MyClass
{
    List<Trip> m_Items = new List<Item>();

    [DataMember]
    public IList<Item> Items
    {
        get { return m_Items; }
    }
}

Serialization went fine, but when I tried to deserialize the same object, I got a null reference exception. Of course you say, you should have added a method tied to de OnDeserializing event, because the constructor of the object doesn't work and hence the m_Items field is never initialized. The code I added to solve this looked like this:

[OnDeserializing]
protected void Init(StreamingContext context)
{
    m_Items = new List<Item>();
}

To my surprise I still got the same exception. I finally figured out that the problem was the type of Items. It is was an IList<> instead of a List<>. To avoid tying a class to a specific implementation of a list, I usually use an interface, which is good practice in most cases... however, not when you want to do deserialization :).

Wednesday, September 02, 2009 10:11:22 PM (W. Europe Standard Time, UTC+01:00)  #    Disclaimer  |  Comments [0]  | 
# Friday, August 15, 2008

Op zaterdag 6 september organiseren dotNED, SDN en VBCentral samen Code Camp 2008. Vorig jaar kon ik er helaas niet bij zijn, maar dit jaar zal ik er niet alleen bij zijn, maar ook een sessie doen. Omdat ik heel erg hou van interactie doe ik een zogenaamde Chalk & Talk. Dat is een sessie waarbij interactie met de groep voorop staat en er niet een vooraf vastgestelde agenda is. Het onderwerp van de sessie in Omgaan met data in een Service Oriented Architecture. In een SOA is een service namelijk verantwoordelijk voor een bepaald domein en de data die daarbij hoort. Een join doen met in de database met data van een andere service bijvoorbeeld is uit den boze... of toch niet? Dat zijn het soort vragen die aan bod komen in deze sessie. Hopelijk tot ziens op Code Camp 2008!

Friday, August 15, 2008 8:52:32 PM (W. Europe Standard Time, UTC+01:00)  #    Disclaimer  |  Comments [0]  |