views:

390

answers:

2

Hi!

Can I somehow tell NHibernate to map my one-to-many relationship to a property which is of type List instead of the interface IList?

I know that NHibernate uses its own IList-implementation for lazy loading, but I don't need this feature. Instead I need a class that is serializable, which I cannot accomplish by using the IList interface instead of the concrete List-class.

Best Regards
Oliver Hanappi

A: 

The simple answer is no, you can't tell NHibernate to use List<T>. This earlier question discusses it in more detail.

The best article I could find on serializing NHibernate objects is targeted to WCF users.

Jamie Ide
A: 

If you really need to do this your only real option is to change your serialization strategy.

You can use the DataContract attribute in System.Runtime.Serialization rather than the standard Serialization attribute. This will support the serialization of interfaces.

If you need to use standard serialization then you could use a separate property for storing the values you wish to be serialized and simply populate it on serialization:

//NHibernate property
public virtual IList<Entity> Entities
{
    get;
    set;
}

//Serialized property
public List<Entity> SerializedEntities
{
    get;
    set;
}

[OnSerializing]
void DoStuff(StreamingContext context)
{
    SerializedEntities = // Whatever you want to serialize...
}
Nigel