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

What is the difference between using IList , IEnumerable , ISet or ISet for collections of child objects in NHibernate entity 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... } 
+5
source share
2 answers

IList

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

Bags

  • Bidirectional ratio: supported
  • Order: NO
  • Type: IList
  • Duplicates: allowed
  • On Adding an item: only one hit in the database. No performance issues.

Iset

  • Bidirectional ratio: supported
  • Order: NO
  • Type: ISet (before NHibernate 3.0 it supported the interface from Iesi.Collections )
  • Duplicates: NOT ALLOWED
  • When adding an item: the entire collection will be loaded to check for duplicates. Avoid if the list of children is huge.

ICollection can be used as a type of child collection that can be displayed by any of the three NHibernate mappings

Nhibernate Cookbook 3.0 has a good explanation of how to use each collection, just in case you come across it.

+13
source

Any IEnumerable assumes deferred execution, so you want to use it in your query when your search is further processed downstream (filtering is the most obvious scenario).

If this is not the case, and you really want to make sure that you have not listed the collection more than once, you will make it available as IList, ISet or ICollection. IList would be preferable if you want to make sure that the collection can have duplicates, while ISet is preferable if you want the set to not be cheated (the given behavior was more useful in my experience).

Both IList and ISet are ICollections that expose the behavior of a collection with bones, such as Count. In the context of NHib, you can see this in implementations where Iesi collections are used instead of .net HashSet, but this is more likely due to the fact that once there was no .net HashSet, and then there was a lag in NHib's ability to use it easily (but NHib will be able to display ICollection in Iesi set).

NTN

+1
source

Source: https://habr.com/ru/post/1490585/


All Articles