views:

110

answers:

1

Is it possible to have a collection of entities that are mapped via an IUserType? For example:

class Foo
{
    public int Id { get; set; }
    public ISet<Special> Items { get; set; }
}

class Special
{
    public Special(string columnValue)
    {
        _val = columnValue;
    }

    public override string ToString()
    {
        return _val.TranformInInterestingWays();
    }

    private string _val;
}

class SpecialUserTypeForMapping : IUserType
{
    // assume that all the right stuff is here
    // for using the non-default constructor on the way in
    // and calling ToString() on the way out
}

If I'm reading the documentation correctly, neither the <set>, <one-to-many>, <many-to-many>, nor <class> element supports the "type" attribute as is used to apply IUserType mapping to <property>s. So how would I go about mapping this?

+1  A: 

The most expedient solution seems to be to use the <element> element, like so:

<class name="Foo" table="foos">
     <id  name="Id" />
     <set name="Items" table="foo_special">
         <key column="fooId" />
         <element column="special_value" type="SpecialUserTypeForMapping" />
     </set>
</class>

Retrieving different Foo instances from the DB is no problem, but it's not clear whether it's possible to write queries against the special_value column, which is a requirement in my scenario. This question seems to indicate that it's not.

David Rubin