1.

How Do I Translate The Nhibernate Collections To .net Collections?

Answer»

The names of the collection mappings is one obvious place where the differences between Java and .NET are shown. Java's collection library has many more options than System.Collection does.

  • The <list> MAPS directly to an IList.
  • The <map> maps directly to an IDictionary.
  • The <bag> maps to an IList. A <bag> 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.

If an order-by attribute is added to the collection mapping element then the concrete collection type will change to one that supports maintaining the order an element was added. For a <bag> it still uses an ArrayList. For the <map> and <set> that have an order-by attribute the concrete collection class changes to a Specialized.ListDictionary and Iesi.Collections.ListSet, respectively. This should be a non-issue since the consumers of your API are using the interfaces IList and Iesi.Collections.ISet.

If a SORT attribute is added to the collection mapping element then the concrete collection type will change to one that supports ordering. For the <map> and <set> that have a sort attribute the concrete collection class changes to a SortedList or Iesi.Collections.SortedSet, respectively, that uses the IComparer provided in the sort attribute. This should be a non-issue to the consumers of your API since they are using the interfaces IList and Iesi.Collections.ISet.

Using an order-by or sort attribute does have implications on performance for maps and sets as the size of the collections grows. Please read the MSDN documentation about the ListDictionary and SortedList performance implications. There are also some good articles on The Code Project about collections.

The names of the collection mappings is one obvious place where the differences between Java and .NET are shown. Java's collection library has many more options than System.Collection does.

If an order-by attribute is added to the collection mapping element then the concrete collection type will change to one that supports maintaining the order an element was added. For a <bag> it still uses an ArrayList. For the <map> and <set> that have an order-by attribute the concrete collection class changes to a Specialized.ListDictionary and Iesi.Collections.ListSet, respectively. This should be a non-issue since the consumers of your API are using the interfaces IList and Iesi.Collections.ISet.

If a sort attribute is added to the collection mapping element then the concrete collection type will change to one that supports ordering. For the <map> and <set> that have a sort attribute the concrete collection class changes to a SortedList or Iesi.Collections.SortedSet, respectively, that uses the IComparer provided in the sort attribute. This should be a non-issue to the consumers of your API since they are using the interfaces IList and Iesi.Collections.ISet.

Using an order-by or sort attribute does have implications on performance for maps and sets as the size of the collections grows. Please read the MSDN documentation about the ListDictionary and SortedList performance implications. There are also some good articles on The Code Project about collections.



Discussion

No Comment Found