NHibernate: Mapping Application Domain

Mapping Class Collections

Summary

You can use four different approaches to represent a collection.

Using a Set

  • Uses the ISet interface container that is available in the IESI Collections library.
  • Use the set declaration in the XML mapping declaration.
  • The set container does not allow duplicate elements.

Using a Bag

  • An unordered collection that permits duplicate elements is called a bag.
  • NHibernate lets you use an IList in .NET to simulate bag behaviour. The IBag interface is not currently supported by .NET.
  • Use the idbag declaration in the XML mapping declaration.

Using a List

  • Use the list declaration in the XML mapping declaration.
  • Requires an additional index column on the database table, which defines the position of the element in the collection.
  • NHibernate can then preserve the ordering of the collection elements when retrieving the collection from the database.

Using a Map

  • Use the map declaration in the XML mapping declaration.
  • Permits duplicate elements.
  • By setting the sorted=true attribute, NHibernate will sort the collection according to the CompareTo() method of the collection type. To use a different sort order, specify the name of the class that implements System Collections IComparer class.
  • NHibernate sorted maps use System Collections SortedList in their implementation.