posted on Wednesday, September 13, 2006 9:42 AM by timbarcz

Custom Collections, what responsibilities should they have?

I just got sent a bug that I need to fix.  It was my fault.  While fixing the bug though I had the chance to look at some old code I had written.  I know learning is an continuing thing, but I was suprised to see some of the decisions I made way back when.  One thing in particular caught my eye were some custom collection classes I wrote.

The particular code dealt with a blog module in our product.  Having two domain objects, blog and post, I had create a postcollection class which would store a collection of posts (go figure eh?).  I'd like to think I'm becoming better with separating concerns in my classes in that I've become more aware of places where I should impart some separation.  Looking at my old code though, I found that I had built some static methods inside the postcollection class.  For example if you wanted to get back the list of posts for a blog you could say postcollection.Load(int weblogId) and you would be returned a postcollection.

If I were building the class today I would put that static method into the post class which would do the same thing but leave the implementation details out of the postcollection class.  The thinking is, "Post" is the domain object, "PostCollection" is simply a "bucket" that holds multiple posts, it shouldn't do anything more than hold posts (separation of concerns).

If it were you, which way would you do it?  Would you put the logic to get all the posts for a blog into the post class, the collection class, or elsewhere?  Just curious what others think.

Comments