A Curious Mind
#tastic

Contrived Domain Modeling Exercise

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?


Feedback

# re: Contrived Domain Modeling Exercise

The interface feels right. The way you have layed out the example I feel like I could trip, fall, and still land on the interface. The 25 in Last25Comments just smells. 10/19/2006 5:23 AM | Stephen Nelson

# re: Contrived Domain Modeling Exercise

I'd favor the service+querying approach. I think you'd be opening up a door for unwanted bloat in your domain model if you had properties like that.

In addition, what if the number of preview comments changed? This way you can have a method on the service layer like this:

public IList<Comment> GetTopComments(User u, int number) { ... } 10/19/2006 1:09 PM | Ben Scheirman

Post a comment





 

Please add 3 and 3 and type the answer here: