I was wondering what is the recommended way to open a collection inside a class, and if it is different from the way to do the same when working with NHibernate objects.
Let me explain ... I have never had any special problems with my classes displaying collection properties, such as:
IList<SomeObjType> MyProperty { get; set; }
Having setter as protected or private gives me several times more control over how I want to handle the collection. I recently came across this article by Davy Brion:
http://davybrion.com/blog/2009/10/stop-exposing-collections-already/
Davy explicitly recommends having collections as IEnumerables, rather than allowing lists to be used to prevent users from being able to directly manipulate the contents of these collections. I can understand his point of view, but I'm not completely convinced, and, reading the comments on his post, I am not the only one.
When it comes to NHibernate entities, it makes sense to hide collections as it suggests, especially when cascades are in place. I want to have full control over the entity that is in the session and its collections, and also identifying AddXxx and RemoveXxx for collection properties makes much more sense to me.
The problem is how to do this?
IEnumerables, / , , ToList(), , , - .
, (add.remove elements) , , , .
.