NHibernate: <set> v.s. <bag>
if you read the whole Hibernate in Action book, you'll find that in Java world, people use "set" collection often. most of the NHibernate articles and examples followed Hibernate's Java trend, using <set> to map one-to-many and many-to-many relationships.
if you thought NHibernate is an easy-to-use O/R Mapper, it is, before you really start using it in some real-world applications. I am not saying that NHibernate not able to face the real-world challenges, I am saying that if you want to use NHibernate in your production solutions, you better know it deeper!
Paul Wilson released his WilsonORMapper 4.0 couple days ago. after that soon he announced a "Real NHibernate Example App", a WinForm C# example that covered most of the things to let you start using NHibernate. it's a good one! also by checking this post's comments, James mentioned an opensourced "Real NHibernate Application, Cuyahoga CMS" , using NHibernate for persistanting Content Management data to data store.
Read more on NHibernate reference.chm file about Collection Mapping, you'll understand NHibernate used another open-source implementation of Set collection to port Hibernate's code. there is no Set collection in .NET framework. (and the book Hibernate in Action won't mention it since the book didn't cover NHibernate). so if using <set> to map collection, the return type won't be standard .NET collection type (it's Iesi.Collections.Set). most of NHibernate examples using <set> mapping along with IList in code, but don't know why I just not able to make it work.
both Wilson's example as well as Cuyahoga CMS used <bag> mapping with IList type ArrayList for collection mappings. I did switch to it and found it work. for your reference UncleG's blog post got a table about each mapping to collection type.
| <bag /> |
IList |
| <list /> |
IList |
| <set /> |
Iesi.Collections.Iset (from the Iesi.Collections.dll assembly in the \bin folder of the NH distribution) |
| <map /> |
IDictionary |
Hibernate in Action book got lot's of examples talking about component mapping. together those various mapping ways should fit into most of the data-related applications people are facing. but none of the information I found got complete reference about the nodes and attributes explaination.
Getting into more advanced scenario, you should face about constructing a good session management pattern for NHibernate sessions, and to let your persistant objects be more flexible from switching between those ORM tools.
tools are just building blocks, just a start. you'll need to have your own style frameworks. and that's why architectures fun.