tags:

views:

189

answers:

3

Imagine a blog engine in ASP.NET MVC where I want strongly-typed Views. I have a Model for BlogPost and a Model for Comments. A typical page would display a blog post and all of that post's comments. What object should I pass to the View()? Should I create a Presentation Model? Maybe I'm using that term incorrectly, but by that I mean should I create a composite model just for a View(), something like BlogAndComments? Should my controller create an instance of BlogAndComments and pas that?

Or, should I somehow pass both a BlogPost and Comments object to the View?

+3  A: 

I think you're on the right track with your understanding of Presentation Models. As to when you should create a View Model, the answer is probably 'it depends'. In your example, you can probably get away with passing the BlogPost and Comments in the ViewData object. It's not gorgeous, but hey, it gets the job done.

When and if that starts to feel ugly or unwieldy, then I would start thinking about making a View Model. I usually end up with the notion of some sort of 'Page', which includes the page title, common data, and then specific stuff for a particular page. In your case, that might end up as a BlogViewPage, which includes Title, BlogPost and List comments.

The nice thing about that approach is that you can then test that controller by making a request and testing the BlogViewPage to ensure that it contains the expected data.

Travis
The term "presentation model" is also used for this case. It is not WPF specific.
Craig Stuntz
Actually, I think I got it wrong. Model-View-ViewModel is the pattern I was thinking of. See http://blogs.msdn.com/dancre/archive/2006/10/11/datamodel-view-viewmodel-pattern-series.aspx, where collections are presented as IObservable, etc. This is a nitpicking level of detail, but I'll update my post to reflect your suggestion.
Travis
+1  A: 

In my opinion comments belong to the view as much as the post itself.

Make a BL class for your comments like:

class CommentBO
{
    Guid UserID;
    string Text;
}

Then you have a BO for your post.

class PostBO
{
    Guid UserID;
    List<CommentBO> Comments;
}

Then your model can be really simple.

class BlogModel
{
    string AuthorName;
    string BlogTitle;

    List<PostBO> Posts;
}

Pass it to the view and render it.

You may be tempted to omit all BO and just fill the model directly from the database. It is an option, but not exactly the right one. A model is just a package of things for a view to display. These things however should be prepared somewhere else, namely at the business logics level with just a nominal participation of the controller.

User
A: 

In my opinion if comments belong to a blog post why not create a collection of comments on the blog post model? That makes perfect sense from a domain modeling stand-point and chances are whatever ORM you are using would support lazy-loading that collection which simplifies your controller logic as well.

James Avery