views:

94

answers:

1

Just some architectural question: I use ASP.net MVC and exclusively rely on Strongly-Typed Views with View Model Classes.

As presentation is the job of the View, these Classes do not only contain some fields, but also some formatting functions, e.g.,

// The ViewModel contains a List<Comment> which the view.aspx iterates
// through, calling this function in a foreach-loop
// PostingDateFormat and DesiredCulture are fields set by the controller,
// and I don't know if they should be
public string GetCommentDateLine(Comment c)
{
    return c.CommentDate.ToString(this.PostingDateFormat, this.DesiredCulture);
}

I just wonder if that is correct, or if I should move it somewhere else? This is especially a concern for functions used by Multiple Views. Should they live in a special Class outside of the hierarchy? Or be copy/pasted (yikes) into each View Model Class?

This is also given the fact that I might have multiple Views on the same ViewModel: A normal view for Browsers, another View for RSS Readers. Naturally, the controller should only populate the viewmodel with Data, and the View itself should format the data according to the target medium (i.e. Dates in RSS Feeds are formatted differently than dates on normal Websites). Should I have separate ViewModels for Normal and RSS? Or should the controller know that I want an RSS field and populate the "PostingDateFormat" field with a different Value? That seems the better solution (no need to duplicate the ViewModels for each view), but I'm not sure if it's the job of the Controller to know which DateFormat the View needs.

+2  A: 

I would suggest putting all your view formatting logic into HtmlHelper extension methods. This way your viewmodels won't contain any formatting logic and will simply do what they are meant to do - carry objects from the controller the the view.

So in your case, I'd guess you'd create some thing like:

namespace System.Web.Mvc
{
    public static class HtmlExtensions
    {
        public static string CommentDateLine(this HtmlHelper html,
                                             Comment comment,
                                             string format,
                                             IFormatProvider formatProvider)
        {
            return comment.CommentDate.ToString(format, formatProvider);
        }
    }
}

As for the PostingDateFormat issue, like you hint, I would definatly not create separate ViewModels for each view. You could create a separate extension method for RSS date formatting (which would maybe do away with the need for the PostingDateFormat set in the ViewModel?).

HTHs,
Charles

Charlino
Just to add to this: I've put the actual Format Provider and Date Format into the web.config and access it through a central settings-class. That way, the Html Helper really only formats it, but isn't concerned with the actual format. http://www.stum.de/2009/12/28/having-a-nested-configuration-section-in-web-config/
Michael Stum