Blog Stats
  • Posts - 62
  • Articles - 1
  • Comments - 35
  • Trackbacks - 76

 

Wintellect's PowerCollections come thru again

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.


Feedback

# re: Wintellect's PowerCollections come thru again

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. 9/4/2006 12:38 AM | Frederik

# re: Wintellect's PowerCollections come thru again

Gravatar Absolutely this would work. The only reason I wanted to be able to access via property was for HQL purposes. For example, if the field name is _emails and the property is Emails, I'd rather use Emails in my HQL than _emails. Not sure how to get around that since NHibernate would be unaware of the Emails name. 9/4/2006 8:44 AM | Mike

# re: Wintellect's PowerCollections come thru again

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. :)
9/4/2006 9:40 PM | frederik

# re: Wintellect's PowerCollections come thru again

Gravatar I read about that too! I looked for it again and then I couldn't find it so I am real glad you posted it. I'll try it today and let you know what happens. I have a test environment looking at me righ tnow so should be easy.
MIKE 9/5/2006 4:36 AM | Mike

# re: Wintellect's PowerCollections come thru again

Gravatar I've tried it myself, and it seems to work fine. :) 9/6/2006 4:26 AM | frederik

Post a comment





 

Please add 6 and 4 and type the answer here:

 

 

Copyright © Mike Nichols