NHibernate:映射列表字典 [英] NHibernate: mapping a dictionary of lists

查看:76
本文介绍了NHibernate:映射列表字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的班级有一个类型为Dictionary<string, List<string>>的字段.用NHibernate映射它的最佳方法是什么?我最好将其保留为一个字段,不想公开它.

My class has a field of type Dictionary<string, List<string>>. What's the best way to map it with NHibernate? I'd better leave it as a field, don't want to expose it.

非常感谢!

ulu

推荐答案

您不能直接将其映射.有两个要考虑的规则:

You can't directly map it. There are two rules to consider:

  • 始终使用集合接口(例如IList<T>IDictionary<K,V>)
  • NH不支持嵌套集合.我以前从未见过申请 从来没有听到有人要求它.
  • Always use interfaces for collections (eg. IList<T>, IDictionary<K,V>)
  • NH does not support nested collections. I've never seen an application for it before and never heard someone requesting it.

将字符串列表放入类并使用接口:

Put your list of string into a class and use interfaces:

class StringList
{
  IList<string> Strings { get; private set; }
}

class Entity
{
  private IDictionary<string, StringList> stringDict;
}

您甚至可能会发现拥有这样一个类的一些优点.

You might even see some advantages of having such a class.

映射:

<class name="Entity">
  ...
  <map name="stringDict" table="Entity_StringDict" access="field">
    <key column="Entity_FK"/>
    <index column="Key" type="System.String"/>
    <composite-element class="StringList">
      <bag name="Strings" table="Entity_StringDict_Strings">
        <key column="Entity_StringDict_FK"/>
        <element type="System.String" column="String"/>
      </bag>
    </composite-element>
  </map>
</class>

映射到三个表:

  • Entity
  • Entity_StringDict
    • Entity_FK
    • Key
    • Table Entity
    • Table Entity_StringDict
      • Column Entity_FK
      • Column Key
      • Entity_StringDict_FK
      • String
      • Column Entity_StringDict_FK
      • Column String

      这篇关于NHibernate:映射列表字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆