tags:

views:

1906

answers:

2
+6  Q: 

ISet Vs IList

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.

+3  A: 

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.

ShuggyCoUk
+5  A: 

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.

Reed Copsey
This is a really really good answer! Cheers!
Ted Smith