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.