Can I use IList instead of ISet?
I don't want to use ISet since its not a native collection.
Could I get away with using IList? M开发者_如何学Pythony 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).
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.
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.
精彩评论