8

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.

Thanks a lot!

ulu

1 Answer 1

7

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

  • 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.

Mapping:

<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>

Maps to three Tables:

  • Table Entity
  • Table Entity_StringDict
    • Column Entity_FK
    • Column Key
  • Table Entity_StringDict_Strings
    • Column Entity_StringDict_FK
    • Column String
Sign up to request clarification or add additional context in comments.

7 Comments

Fantastic, I'll give it a try!
Hmm I'm getting an error: XML validation error: The element 'composite-element' in namespace 'urn:nhibernate-mapping-2.2' has invalid child element 'bag' in namespace 'urn:nhibernate-mapping-2.2'. List of possible elements expected: 'parent, property, many-to-one, nested-composite-element' in namespace 'urn:nhibernate-mapping-2.2'. Using Nhibernate 2.0.1..
Also, the docs say, "Composite elements may contain components but not collections".
I ended up adding an entity instead of a value class (StringList in your example), and my design became better I believe. Can we call it Mapping Driven Development? Anyway, thanks for your help, you pointed me to the right direction.
In NH 2.1, you can put collections into composite-elements. Sometimes it's better to write some additional classes to design complex structures. "Mapping Driven Development" sound rather bad to me, because the class model shouldn't be driven by NH, even if there are some trade-offs. NH tries to influence your model as little as possible.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.