views:

62

answers:

1

Following the technique described here, I was able to populate a domain object that uses custom collections for its children. The relevant property mapping looks like this:

    <component name="Contacts" class="My.CustomList`1[[Domain.Object, DomainAssembly]], MyAssembly">
        <set name="InnerList">
            <key column="PARENT_ID" />
            <one-to-many class="Contact" />
        </set>
    </component>

My custom collection class exposes the InnerList property as an ICollection like so:

    protected System.Collections.ICollection InnerList
    {
        get
        {
            return this;
        }
        set
        {
            Clear();
            foreach (DomainObject o in value)
            {
                Add(o);
            }
        }
    }

This worked like a charm to load data from the database and not have to abandon my rather useful custom collection class.

Then I moved on to try implement saving, and following the advice given in this thread, decided to wrap every call to NHibernate in a transaction.

Now when I commit following my load, NHibernate throws an InvalidCastException: "Unable to cast object of type 'My.CustomList`1[Domain.Object, DomainAssembly]' to type 'Iesi.Collections.ISet'."

Is there a way to make this work the way I expect it to?

EDIT:

Following the lead provided by Raphael, I tried switching to ICollection<T> which gives me a different InvalidCastException when I commit the transaction: Unable to cast object of type 'My.CustomList`1[Domain.Object]' to type 'NHibernate.Collection.IPersistentCollection'.

A: 

Change the property to be of type

IList<T>
Rafael Belliard
Using IList<T> and a set mapping causes the initial query to fail when it tries to cast NHibernate.Collection.Generic.PersistentGenericSet`1[MyDomain.Contact] to System.Collections.Generic.IList`1[MyDomain.Contact]. PersistentGenericSet supports ICollection and ICollection<T> but not IList<T>.
Dan