tags:

views:

168

answers:

3

I've got to the point (unintentionally) that I feel in some parts I'm doing too much within the view (.aspx) itself, too much formatting, concatenation, in one place a little regex replace.

I was starting to work on a new part and trying to improve my approach.. Then it hit me why don't I just make all my View Models (in /Models/ in .Web project) strings or a list of string at a push. Note: I'm not referring to my model/domain, but rather to specifically my ViewModel.

public class FinanceQuoteView
{
    public string Provider; 
    public string Broker; // rather than Broker == null ? "N/A" : Broker.ToUpperCase();
    public string Monthly; // rather than Monthly.ToString("C")
    public string PaymentTerm; // rather than "1+" + PaymentTerm.ToString();
    public string FreeInsurance; // rather than insuranceIncluded ? "Yes" : "No";
    public string[] Restrictions;
}

For Form submission (adding editing) I use a seperate view model to feed the controller action (form model if you will in /Models/Form). So FinanceQuoteForm does contain doubles etc... built via a binder.

What everyone think about this approach? Is doing the .ToString("C") in the mapping from domain to view model too much?

+1  A: 

Your model should produce the correct data, then it is up to the view to produce the data in the format that it requires. If you built another view on top of the model, you might want to do different manipulations of the data, so i suggest NOT returning them as strings from the model.

ck
He's not talking about the Domain Model - he's talking about the View Model.
Mark Seemann
Mark. Correct. I am not referring to my Model. I am referring specifically to my View Model that my controller actions generate (either manually, automapper or whatever - small impl detail) and send to to the View for rendering.
mattcodes
I agree on ck, View model is the one to "format" data. By passing ONLY strings, you lose the chance for view model to format the data. One simple example I can have, is that if you pass a "date" as a string to view model, then you need further manipulation in controller to serve the content to an US visitor or an UK visitor.
xandy
xandy, interesting point, but I think localization should be considered in the controller level either extra logic or part of your mapping code. As Mark suggests below, this way you can test easily and I can't see how you would save any code pushing this down to the View (infact you'll probably have duplication)
mattcodes
Code saving should not be any concern when comparing to reusability of components. I think ck points out one very important issue is that, you should be able to build another view on top of the same controller. Take the date as example (again!), you are right that mapping and localization logic could do in controller level, but how about long date and short date issues? Consider a scenario that I would give only short date in mobile platform, but the long date string to Desktop platform. Make date in string simply put the controller too involved in formatting.
xandy
Xandy, I see your point, perhaps another small layer then, in ASP.Net MVC an action filter that intercepts and replaces the rich ViewData.Model that has unformatted types (bool, Date, etc..) with one that is string only relevant to the particular destination platform. This way the formatting is not in the view but its all not embeded in a hard to test .aspx/ascx. I mention code saving to avoid repetition of formatting throughout a view.
mattcodes
+2  A: 

I tend to lean pretty heavily towards strings myself for the View Models that I design. After all, most of the data displayed in the View takes the form of strings. Any time I am about to perform data manipulation in the View (.aspx/.ascx), I seriously consider pushing that logic down to my View Model so that I can unit test it. After all, Testability is the major benefit you get from MVC, so why not use it?

In WPF (just to take a short detour) many of the controls natively understand other types of data (such as numbers, booleans and so forth), but on a platform so inherently bound up on strings as HTML, it makes a lot of sense for me to treat most of the output as text.

All data has to round-trip between server and browser encoded as strings anyway, so often, you would just be explicit about it.

I definitely don't think it's too much - I only think you can do it too little :)

Mark Seemann
"Any time I am about to perform data manipulation in the View (.aspx/.ascx), I seriously consider pushing that logic down to my View Model so that I can unit test it." Can you please elaborate on that part? I was going to route of a seperate viewmodel for editing but I am interested in your approach
mattcodes
I think we are talking about the same thing, but that I may have expressed myself less than stellarly clear. With data manipulation I was simply thinking about small things such as currency formatting, string concatenation etc. - you know: Manipulating the data on its way to the output stream. Having one View Model for display and another for editing sounds very reasonable to me. In the stateless world of HTTP, one is output while the other is input. They are very different things and should not be confused. Modeling them as different captures that distinction very clearly.
Mark Seemann
A: 

Your situation seams to be very special and limited in a way: Your ViewModel is either for editing or Displaying. Most Views do both: they have a lot of display stuff ans some editing stuff. Lets say this Page:

Display question, answers and notes. Edit: Answer.

I would not want to distinguish the 2. It would end in duplication of a lot of display fields.

I would rather have one view with possibly 2 properties: one for display and one for the form value.

Malcolm Frexner