ISet vs ICollection

723 views
Skip to first unread message

Max André Bündchen

unread,
Dec 23, 2010, 6:26:20 AM12/23/10
to nhusers
I'm reading the NHibernate 3.0 Cookbook and a question appeared when I
played with NHibernate. As the System.Collections.Generic.ISet is not
supported in <set> mappings, I can use the
Iesi.Collections.Generic.ISet or the
System.Collections.Generic.ICollection.

What's the drawback of use native ICollection instead of ISet? I found
a few topics about it but not a clear answer.

José F. Romaniello

unread,
Dec 23, 2010, 6:44:07 AM12/23/10
to nhu...@googlegroups.com
The only way to use SETs in nhibernate is with Sets from Iesi.Collections.
Having say that your field or property type could be ICollection but the instance in transients objetcs must be set, this is an example:

public class Invoice
{
   private ISet<InvoiceLIne> lines = new HashedSet<InvoiceLine>();

   public ICollection<InvoiceLine> Lines {get{return lines;}}
}

this is ok too:

public class Invoice
{
   private ICollection<InvoiceLine> lines = new HashedSet<InvoiceLine>();

   public ICollection<InvoiceLine> Lines {get{return lines;}}
}


but you miss sets operations.


2010/12/23 Max André Bündchen <maxbu...@gmail.com>

--
You received this message because you are subscribed to the Google Groups "nhusers" group.
To post to this group, send email to nhu...@googlegroups.com.
To unsubscribe from this group, send email to nhusers+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/nhusers?hl=en.


Jason Meckley

unread,
Dec 23, 2010, 8:51:00 AM12/23/10
to nhusers
the biggest difference is sets have exactly 1 instance of an object in
their collection. a collection can have multiple instances.
There are also set operations which are part of the ISet<> interface
which can be useful. here is an example of exactly 1 instance:

var entity = new Entity(1);

var set = new HashedSet<Entity>();
set.Add(entity);
set.Add(entity);
Assert.That(set.Count, Is.EqualTo(1));

var collection = new List<Entity>();
collection.Add(entity);
collection.Add(entity);
Assert.That(collection.Count, Is.EqualTo(2));

I have also found that Lists and Sets behave differently when they are
mapped to M:N relationships.

On Dec 23, 6:44 am, José F. Romaniello <jfromanie...@gmail.com> wrote:
> The only way to use SETs in nhibernate is with Sets from Iesi.Collections.
> Having say that your field or property type could be ICollection but the
> instance in transients objetcs must be set, this is an example:
>
> public class Invoice
> {
>    private ISet<InvoiceLIne> lines = new HashedSet<InvoiceLine>();
>
>    public ICollection<InvoiceLine> Lines {get{return lines;}}
>
> }
>
> this is ok too:
>
> public class Invoice
> {
>    private ICollection<InvoiceLine> lines = new HashedSet<InvoiceLine>();
>
>    public ICollection<InvoiceLine> Lines {get{return lines;}}
>
> }
>
> but you miss sets operations.
>
> 2010/12/23 Max André Bündchen <maxbundc...@gmail.com>
>
> > I'm reading the NHibernate 3.0 Cookbook and a question appeared when I
> > played with NHibernate. As the System.Collections.Generic.ISet is not
> > supported in <set> mappings, I can use the
> > Iesi.Collections.Generic.ISet or the
> > System.Collections.Generic.ICollection.
>
> > What's the drawback of use native ICollection instead of ISet? I found
> > a few topics about it but not a clear answer.
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "nhusers" group.
> > To post to this group, send email to nhu...@googlegroups.com.
> > To unsubscribe from this group, send email to
> > nhusers+u...@googlegroups.com<nhusers%2Bunsu...@googlegroups.com>
> > .

Berryl Hesh

unread,
Dec 23, 2010, 1:19:55 PM12/23/10
to nhusers
@Jose

I have been using an ICollection field as in your example, but with
a .Net HashSet instance (sometimes with an IEqualityComparer as
well).

I get set semantics in my object model - can you please clarify what
set operations are missed this way?

As an aside, is there any reason to think NHib will ever support the
HashSet?

Cheers,
Berryl

On Dec 23, 3:44 am, José F. Romaniello <jfromanie...@gmail.com> wrote:
> The only way to use SETs in nhibernate is with Sets from Iesi.Collections.
> Having say that your field or property type could be ICollection but the
> instance in transients objetcs must be set, this is an example:
>
> public class Invoice
> {
>    private ISet<InvoiceLIne> lines = new HashedSet<InvoiceLine>();
>
>    public ICollection<InvoiceLine> Lines {get{return lines;}}
>
> }
>
> this is ok too:
>
> public class Invoice
> {
>    private ICollection<InvoiceLine> lines = new HashedSet<InvoiceLine>();
>
>    public ICollection<InvoiceLine> Lines {get{return lines;}}
>
> }
>
> but you miss sets operations.
>
> 2010/12/23 Max André Bündchen <maxbundc...@gmail.com>
>
>
>
>
>
>
>
> > I'm reading the NHibernate 3.0 Cookbook and a question appeared when I
> > played with NHibernate. As the System.Collections.Generic.ISet is not
> > supported in <set> mappings, I can use the
> > Iesi.Collections.Generic.ISet or the
> > System.Collections.Generic.ICollection.
>
> > What's the drawback of use native ICollection instead of ISet? I found
> > a few topics about it but not a clear answer.
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "nhusers" group.
> > To post to this group, send email to nhu...@googlegroups.com.
> > To unsubscribe from this group, send email to
> > nhusers+u...@googlegroups.com<nhusers%2Bunsu...@googlegroups.com >
> > .

Jason Meckley

unread,
Dec 23, 2010, 2:22:31 PM12/23/10
to nhusers
system.collections.set is new to .net 4.0. Nh 3.0 targets .net 3.5.
I'm sure future versions will support system.collections.set, but not
until .net 4.0 is the standard. some are still maintaining .net 2.0
applications.

Diego Mijelshon

unread,
Dec 26, 2010, 9:31:22 PM12/26/10
to nhu...@googlegroups.com
Berryl,

That works, but NH will replace your .Net HashSet with a Iesi one when loading (which also means you'll get erratic results if you are depending on that IEqualityComparer).

Otherwise, there's nothing wrong with that.
 
    Diego


To unsubscribe from this group, send email to nhusers+u...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages