Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between using IList, IEnumerable, ISet or ICollection in NHibernate?

What's the difference between using IList, IEnumerable, ISet or ICollection for collections of child objects in NHibernate's entities classes? I.e:

public class Parent
{
    public virtual int IdParent { set; get; }

    public virtual IList<Child> Children { set; get; }
    // Or
    public virtual ISet<Child> Children { set; get; }
    // Or so on...
}
like image 226
Guillermo Gutiérrez Avatar asked Jul 09 '13 21:07

Guillermo Gutiérrez


1 Answers

IList

  • Bidirectional Relation : NOT SUPPORTED. (since there is an additional index column that is introduced to maintain the order of the children, which the child element cannot sense)
  • Ordered : Yes
  • Type : IList
  • Duplicates : Allowed
  • On Adding an element : entire collection will be loaded to get the index column value. Avoid if the list of children is huge.

Bags

  • Bidirectional Relation : Supported
  • Ordered : NO
  • Type : IList
  • Duplicates : Allowed
  • On Adding an element : only a single hit to the database. No performance issues.

ISet

  • Bidirectional Relation : Supported
  • Ordered : NO
  • Type : ISet ( till NHibernate 3.0 it supported the interface from Iesi.Collections)
  • Duplicates : NOT ALLOWED
  • On Adding an element : entire collection will be loaded to check for duplicates. Avoid if the list of children is huge.

ICollection can be used as the type of the child collection which can be mapped by any of the three NHibernate Mappings

Nhibernate Cookbook 3.0 has a good explaination of using each of the collection , Just in case you happen to come across it.

like image 86
frictionlesspulley Avatar answered Oct 23 '22 14:10

frictionlesspulley