2

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'.

Community
  • 1
  • 1
Dan
  • 901
  • 11
  • 25
  • 1
    Your InnerList (and hence - your CustomCollection) when created - does that implement ISet from Iesi.Collections? If it doesn't - that's where your problem is at. – Goblin Sep 07 '10 at 20:55
  • I'm trying to avoid having a dependency on Iesi.Collections in my domain, which is why I am using the "hack" in the first place. – Dan Sep 08 '10 at 12:06

1 Answers1

1

Change the property to be of type

IList<T>
rebelliard
  • 9,592
  • 6
  • 47
  • 80
  • 1
    Using IList 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 but not IList. – Dan Sep 08 '10 at 12:37