views:

785

answers:

2

I am trying to use the XMLSerializer with a castle active record class which looks like the following:

[ActiveRecord("Model")]
public class DataModel : ActiveRecordBase
{
    private IList<Document> documents;

    [XmlArray("Documents")]
    public virtual IList<Document> Documents
    {
        get { return documents; }
        set
        {
            documents = value;    
        }
    }
}

However, the XMLSerializer runs into trouble because of the IList interface. (Raises exception: Cannot serialize member 'DataModel.Documents' of type 'System.Collections.Generic.IList`1....)

I read elsewhere that this is a limitation in the XMLSerializer and the recommended workaround is to declare it as a List<T> interface instead.

Therefore I tried changing the IList<Document> to List<Document>. This causes ActiveRecord to raise an Exception: Type of property DataModel.Documents must be an interface (IList, ISet, IDictionary or their generic counter parts). You cannot use ArrayList or List as the property type.

So, the question is: How do you use the XMLSerializer with a Castle ActiveRecord containing an IList member?

+1  A: 

Interesting... the best I can suggest is to use [XmlIgnore] on Documents - and does ActiveRecord have a similar way of ignoring a member? You could do something like:

[XmlIgnore]
public virtual IList<Document> Documents
{
    get { return documents; }
    set
    {
        documents = value;    
    }
}

[Tell ActiveRecord to ignore this one...]
[XmlArray("Documents"), XmlArrayItem("Document")]
[Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
public Document[] DocumentsSerialization {
    get {
         if(Documents==null) return null;
         return Documents.ToArray(); // LINQ; or do the long way
    }
    set {
         if(value == null) { Documents = null;}
         else { Documents = new List<Document>(value); }
    }
}
Marc Gravell
Thanks, it works. The interface becomes a bit polluted because of the extra property, but I don't really see much of a choice unless Microsoft does something about the IList<T> serialization issue.
I read that this isn't a bug, but a feature, and the way to get around it is to use data transfer objects (or DTOs).
Rew
A: 

Microsoft won't implement this, so you have to work around it. One way would be to use the non-generic IList:

[ActiveRecord("Model")]
public class DataModel : ActiveRecordBase<DataModel> {
    [XmlArray("Documents")]
    [HasMany(typeof(Document)]
    public virtual IList Documents {get;set;}
}

Here's some more information about this bug.

Mauricio Scheffer
Thanks for pointing me to the Microsoft bug report. It really sucks that they aren't fixing this. I don't want to lose the type-safety benefit of using the generic list though, so I'll have to either go for the solution suggested by Marc above, or else shift everything to the DataContractSerializer.