Published: 12 Aug 2011
By: Igor Ignatov

An overview of .NET type generation for NHibernate mapping collections.

Contents [hide]

Introduction

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.

Collection mapping

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.

<<  Previous Article Continue reading and see our next or previous articles

About Igor Ignatov

Sorry, no bio is available

View complete profile here.

Other articles in this category


A Feature-driven Comparison of Entity Framework and NHibernate-Lazy loading
Last month I took the challenge of trying to compare Entity Framework (EF4) and NHibernate (NH) in a...
A Feature-driven Comparison of Entity Framework and NHibernate-2nd Level Caching
Where would you place caching in your layered solution? Two main patterns exist for caching in appli...
A Feature-driven Comparison of Entity Framework and NHibernate - Queries
Let's explore what Entity Framework and NHibernate has to offer when it comes to their query capabil...
A Feature-driven Comparison of Entity Framework and NHibernate—Fetch Plans
This article is about fetch plans-a recognized and common way for developers to instruct the O/RM ab...
A Feature-driven Comparison of Entity Framework and NHibernate - Self-tracking entities
In this article, Dino Esposito introduces self-tracking entities.

You might also be interested in the following related blog posts


Fun with C# 4.0s dynamic read more
A total n00bs guide to migrating from a custom data layer to Nhibernate: so many choices read more
A big step for Stored Procedures in EF4 read more
Create NHibernate classes using T4 read more
Business Apps Example for Silverlight 3 RTM and .NET RIA Services July Update: Part 6: Data Transfer Objects (DTOs) read more
Dynamic Languages: A Separation of Concerns read more
NHibnerate : Handling the Special Cases read more
Binding a ComboBox to Enum Values with RadControls for Silverlight read more
NHibernate : Some Naked Thoughts read more
ASP.NET MVC HttpModule Registration Under IIS Integrated Mode vs. Classic Mode read more
Top
 
 
 

Please login to rate or to leave a comment.

Free Agile Project Management Tool from Telerik
TeamPulse Community Edition helps your team effectively capture requirements, manage project plans, assign and track work, and most importantly, be continually connected with each other.