Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NHibernate ISet Vs IList

Tags:

c#

nhibernate

In most of the NHiberate examples they use an ISET over an IList. I am aware of the basic differences between the two ie a set is unique. However, I am unsure why they use an ISet over an IList when doing lazy loading.

What advantage does (Iesi.Collections.Generic)ISet have over IList? Particularity when lazy loading.

like image 873
Ted Smith Avatar asked Mar 22 '09 20:03

Ted Smith


People also ask

What is the difference between ICollection and IList?

ICollection<T> is an interface that exposes collection semantics such as Add() , Remove() , and Count . Collection<T> is a concrete implementation of the ICollection<T> interface. IList<T> is essentially an ICollection<T> with random order-based access.

Which is better IEnumerable or IList?

You use IEnumerable when you want to loop through the items in a collection. IList is when you want to add, remove, and access the list contents out of order.

What is difference between IList and IEnumerable?

IList doesn't support further filtering. IEnumerable exists in System. Collections Namespace. IEnumerable is a forward only collection, it can't move backward and between the items.

What is the use of Ienumerator ICollection Idictionary & IList?

ICollection is the most basic of the interfaces you listed. It's an enumerable interface that supports a Count and that's about it. IList is everything that ICollection is, but it also supports adding and removing items, retrieving items by index, etc.


2 Answers

I believe this is mostly due to NHibernate's java heritage.

See the FAQ entry on persistent collections. The specifically mention how Java contains more collections by default that C#, and they, in particular map to an ISet.

I believe the reason most samples use this is mainly just because this is more common in Java, and the samples were ported from Java. (Some of the samples use set without really needing it to be a set...)

That being said, there are distinct differences to a set vs. a list. The behavior is different, so there are use cases where a set's behavior is more appropriate. For lazy loading, this allows you to reduce the restriction on the order things are loaded, so you can potentially load less information and keep the interface contracts in place.

like image 52
Reed Copsey Avatar answered Oct 04 '22 07:10

Reed Copsey


A set interface expresses only that the resulting collection can be:

  1. tested for membership
  2. enumerated over in some arbitrary order (which might change)

a list interface expresses the first but the second is stronger, the ordering is well defined and will not change unless something instructs it to.

By using a less restrictive contract (set) you gain more freedom in what you can use (you may load the set in any order for example and no one using your class should care if this ordering is not consistent)

I have here glossed over the fact that the set further ensures than it cannot contain duplicates (which is a constrain on implementers). However if you needed to store such instances a set would simply be unacceptable so this ceases to be a problem in and instead becomes a positive. Consumer's work becomes even easier as they need not worry that duplicates exist at this has already been guaranteed for them.

like image 40
ShuggyCoUk Avatar answered Oct 04 '22 07:10

ShuggyCoUk