开发者

Mapping for multi-interface-inherited class in NHibernate

I have interface:开发者_StackOverflow中文版

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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜