views:

19

answers:

0

I am trying to use a custom collection on a business object via a collection factory class that implements IUserCollectionType. The relevant methods on this class are implemented like so:

    public object Instantiate(int anticipatedSize)
    {
        return new MyCustomCollection<T>()
    }

    public IPersistentCollection Instantiate(ISessionImplementor session, ICollectionPersister persister)
    {
        return new PersistentCustomCollection<T>(session);
    }

My PersistentCustomCollection<T> class inherits from MyCustomCollection<T> and wraps an instance of PersistentGenericList<T>:

public class PersistentCustomCollection<T> : MyCustomCollection<T>, IPersistentCollection
{
    private PersistentGenericList<T> _wrapped;

    public PersistentCustomCollection(ISessionImplementor session)
    {
        _wrapped = new PersistentGenericList<T>(session);
    }

    public PersistentCustomCollection(ISessionImplementor session, IList<T> list)
    {
        _wrapped = new PersistentGenericList<T>(session, list);
    }

    #region IPersistentCollection Members

    public bool AfterInitialize(ICollectionPersister persister)
    {
        return _wrapped.AfterInitialize(persister);
    }

    // ... rest of IPersistentCollection interface, forwarding calls to _wrapped ... //

    #endregion
}

The mapping I'm using looks like this:

    <bag name="ChildCollection" generic="true" collection-type="Namespace.MyFactory`1[[My.ChildClass, MyAssembly]], FactoryAssembly">
        <key column="PARENT_ID" />
        <one-to-many class="ChildClass" />
    </bag>

But now when I load a class with a child collection, it fails with an Invalid Cast error that says it cannot cast NHibernate.Collection.Generic.PersistentGenericList<My.Class> to my MyCustomCollection<My.Class>. So it looks like it's not calling my factory at all. Can anyone see why?