views:

45

answers:

1

I have interface:

public interface IHasList<T>
{
    IList<T> Items { get; set; }
}

And I want to map such class using one-to-many mapping to the lists:

public class Model : IHasList<A>, IHasList<B>
{
    ...
}

Can I do this? If yes, how to write mapping?

A: 

It's possible, but a little weird.

First, in order to declare this in C#, Model would look like this:

public class Model : IHasList<A>, IHasList<B>
{
    IList<A> IHasList<A>.Items { get; set; }
    IList<B> IHasList<B>.Items { get; set; }
}

So you need to make NHibernate understand that:

<bag name="IHasList&lt;A&gt;.Items" table="ModelItemA">
  <key />
  <one-to-many class="A" />
</bag>
<bag name="IHasList&lt;B&gt;.Items" table="ModelItemB">
  <key />
  <one-to-many class="A" />
</bag>

(I'm assuming A and B are mapped entities with a regular one-to-many relationship, change that to many-to-many or element and add cascade/inverse attributes as needed)

It's pretty clean, the mess is actually introduced by XML escaping. You'll also have to use full names for the classes.

Diego Mijelshon

related questions