tags:

views:

747

answers:

1

I've got two tables in my database: Articles and Tags

The Tags tables consist of ArticleID (foreign key) and a Tag (varchar).

Now I need to map an articles tags into a readonly collection on Article entity, either using IList Tags or ReadOnlyCollection Tags.

I've consulted the NHibernate reference material, but I can't seem to figure when to use Set, Bag and the other Nhibernate collections. I've seen examples using the ISet collection, but I really don't like to tie my entity classes to a NHibernate type.

How can I do the mapping in NHibernate?

Thank you in advance.

edit: I ended up using a <bag> instead, as it doesn't require an index:

<bag name="Tags" table="Tag" access="nosetter.camelcase" lazy="false">
  <key column="ArticleId" />
  <element column="Tag" type="System.String" />
</bag>
+4  A: 

The type of collection to use in your mapping depends on how you want to represent the collection in code. The settings map like so:

  • The <list> maps directly to an IList.
  • The <map> maps directly to an IDictionary.
  • The <bag> maps to an IList. A does not completely comply with the IList interface because the Add() method is not guaranteed to return the correct index. An object can be added to a <bag> without initializing the IList. Make sure to either hide the IList from the consumers of your API or make it well documented.
  • The <set> maps to an Iesi.Collections.ISet. That interface is part of the Iesi.Collections assembly distributed with NHibernate.

so if you want an IList to be returned, then you would use the <list> mapping. In your case, I'd probably map using the <list> mapping.

lomaxx