Geeks With Blogs
Mike Nichols - SonOfNun Technology If I were the captain on a big steamboat...

While trying to tighten down access to invariants within an Aggregate, I'd like to make the exposed collections ReadOnly. Then I control the addition of child objects thru exposed methods, thus enforcing the Aggregate's role as gatekeeper for these objects. The problem is, I am mapping NHibernate to my fields which implement IList which, of course, allow .Add(T entity) and casting to an ReadOnlyCollection in the exposed property isn't going to work.

So I poked around the Algorithms utility class from Wintellect's Power Collections and saw they have a nifty little wrapper that returns an IList for exactly this application. So to lock down my Addresses property that returns an IList, I just have to return Algorithms.ReadOnly(_addresses);.

Now NHibernate is happy and my domain is locked down from dumb mistakes I might make trying to add a child to a collection without following biz rules.

I love these little utilities the Wintellect guys put out there...real handy. They are fast and handy.

Posted on Friday, September 1, 2006 11:09 AM O/R Mappers , General , C# | Back to top


Comments on this post: Wintellect's PowerCollections come thru again

# re: Wintellect's PowerCollections come thru again
Requesting Gravatar...
I'm thinking of letting NHibernate use the IList field directly.
Wouldn't it then be possible to just return a ReadOnlyCollection in the property ?
Since NHibernate won't be using the property, I think it shouldn't have any problem with that ?

This is just a hypothese, since I haven't tested it yet.
Left by Frederik on Sep 04, 2006 12:38 AM

# re: Wintellect's PowerCollections come thru again
Requesting Gravatar...
Yes, I totally agree with that, I also prefer the 'property - name' instead of the fieldname.
However, as I was reading a bit about NHibernate last weekend, I saw that you could specify a 'naming' convention in your mapping.

Something like this:
<property name="Emails" access="field.underscore-lowercase" ... />

I have to test this out as well :) (short on time), but this should do the following:
In your HQL you should be able to use 'Emails', but NHibernate will access a field that is called _emails (because of the access attribute).
But, as I've said, I only read about it, and haven't tested it for myself (it's on my todo-list), so don't pin me down on it. :)
Left by frederik on Sep 04, 2006 9:40 PM

# re: Wintellect's PowerCollections come thru again
Requesting Gravatar...
I've tried it myself, and it seems to work fine. :)
Left by frederik on Sep 06, 2006 4:26 AM

Your comment:
 (will show your gravatar)


Copyright © Mike Nichols | Powered by: GeeksWithBlogs.net