NHibernate Forge
The official new home for the NHibernate for .NET community

.NET type generation for NHibernate mapping collections

Wiki Page Hierarchy

Pages

Page Details

Published by:
Igor Ignatov
on 08-09-2011
1 person found this article useful.
Article
Comments (0)
History (1)
100% of people found this useful

.NET type generation for NHibernate mapping collections

As our users might know, the Devart company has recently released a new version of  Entity Developer that supports NHibernate that is intended to provide users with a powerful and convenient visual model designer for NHibernate

When writing templates for generating entity classes for NHibernate models in  Devart Entity Developer for NHibernate, I tried to understand to which .NET types in generated entity classes NHibernate collection were mapped to and in which structure they were stored. I read quite a lot of online articles on the subject, but none provided me an exhaustive answer. After I cleared up this issue, I decided to describe this aspect in an article.

So, there are the following types of collection mapping in NHibernate:

  • Set;
  • List;
  • Bag;
  • Idbag;
  • Map;
  • Array.

 

The Generic collection can be used in all type of NHibernate collection mapping, except for Array.

Set maps to an Iesi.Collections.ISet. The interface is part of the Iesi.Collections assembly distributed with NHibernate. The field of the class, in which the collection is stored, is initialized with an object of the  Iesi.Collections.HashedSet type; it is also part of Iesi.Collections assembly distributed with NHibernate. If the collection is mapped as the generic one, Set maps to an Iesi.Collections.Generic.ISet<T>, while the field is initialized with an object of the Iesi.Collections.Generic.HashedSet<T> type, where T is the details entity.

List, Bag and Idbag map to System.Collections.IList. The field of the class, in which the collection is stored, is initialized with an object of the System.Collections.ArrayList type. When mapped as generic, the collection maps to System.Collections.Generic.IList<T>, while the field is initialized with an object of the System.Collections.Generic.List<T> type, where T is the details entity.

Map maps to System.Collections.IDictionary. The field of the class, in which the collection is stored, is initialized with an object of the System.Collections.Hashtable type. If the collection is mapped as the generic one, Map maps to  System.Collections.Generic.IDictionary<TIndex, T>, while the field is initialized with an object of the System.Collections.Generic.Dictionary<TIndex, T>, where TIndex is the type of the collection index, and  T is the details entity.

Array maps to an array of collection elements(T[] for C#).  The field of the class, where the the collection is stored, is initialized as an array of collection elements. 

The initialization of the field in the class constructor, where the collection is stores, is declarative and is performed in order to prevent the null reference exception when the property of a newly created class object is called. When data are obtained from the database and mapped to objects, NHibernate run-time re-initializes the field and fills in the collection. 

 

Recent Comments

Leave the first comment for this page.
View All
Powered by Community Server (Commercial Edition), by Telligent Systems