Thursday, October 19, 2006 3:20 AM
Lets pretend that we are creating a blog engine. We are going along modeling our blogging domain until we hit this thought. How do I model a blog post and it comments?
My first attempt is simply this:
public class BlogPost
{
//stuff
private IList _comments = new ArrayList();
public IList Comments
{
get { return this._comments; }
}
}
Except what if a post has a gajillion comments and it shows up on the main feed where we don't see the comments. We end up paying the cost of loading the comments even when we aren't using them.
Ok, so lets make it lazy load. With NHibernate that pretty easy "lazy-load='True'".
But what if this massive amount of comments is happening on every blog post, and we want to limit it to the last 25 comments on the post. (Also easy in NHibernate). But how do we want to map that to the class?
public class BlogPost
{
//stuff
private IList _last25Comments = new ArrayList();
public IList Last25Comments
{
get { return this._comments; }
}
private IList _comments = new ArrayList();
public IList Comments
{
get { return this._comments; }
}
}
Or do we build some service class that will return the last X comments for a given blog post?
public interface CommentService
{
IList LastXComments(BlogPost post, int numberOfComments);
IList LastXComments(Guid id, int numberOfComments);
}
I am not sure which way I want to lean or not. What do you think?