tags:

views:

107

answers:

2

I don't want to use ISet since its not a native collection.

Could I get away with using IList? My relations all have primary keys, and any time they don't I am not using that collection to modify the database etc. (the parent collection will handle any db updates/cascades etc).

+3  A: 

Yes, NHibernate supports IList, but a list is a different collection than a set. If you want to keep the behavior of your application the same, and you don't want to reference iesicollections, you can use System.Collections.Generic.ICollection with implementation System.Collections.Generic.HashSet. You will still need Iesicollections referencensed somewhere in your solutions, because Nhibernate.dll depends on it.

Paco
+2  A: 

You can use IList with <bag> (and <idbag> I believe). I always use IList with nHibernate, and I havn't seen ISet used with nHibernate very often in open source code either.

I am curious are you using HBM files or fluent nHibernate, with Fluent it is pretty straight forward, just make the collection IList and it will take care of the rest.

Also, David, nHibernate may not be "native" but you don't need to reference nHibernate in your domain model either (at least I hope you don't). Persistence ignorance is one of the features of nHibernate.

I often map many-to-one relationships in a similar fashion to this:

Parent:

public IList<Child> Children { get; private set; }
public AddChild(Child child) {
  child.Parent = this;
  Children.Add(child);
}

Child:

public Parent Parent { get; set; }

You can also make Children IEnumerable and using a backing field that is IList, but then you need to tell nHibernate how to access the backing field. This makes it so that children can only be added through the AddChild() method.

Chris Nicola
how do you decide who is the parent, and who is the child? I guess in some relations it is just a matter of choosing?
mrblah
For this one-to-many example, the side with the IList property is the parent and the side with the singular reference is the child. So for example if a blog Post can have multiple Comments, Post is the parent and Comments is the child. Are you dealing with a many-to-many relationship here?
Chris Nicola