0

People on SO often say:"A ViewModel holds methods that can be executed by the view, properties to indicate how toggle view elements, etc. ..."

When my ViewModel is sent as a WebApi response to the client serialized to JSON, how can this ViewModel execute a method on the client?

This is not clear at all to me.

Blumer
  • 5,005
  • 2
  • 33
  • 47
Elisabeth
  • 20,496
  • 52
  • 200
  • 321

2 Answers2

0

You can understand viewmodel in at least two ways

  • instead of passing your business objects to the view (for example MVC Razor view) you pass stripped down objects that contain properties that are needed for this view and nothing else. View creation is easier and you avoid problems when view designer uses fields that are lazy loaded from database (avoid Select N+1 problem and others)

  • you can create viewmodel that will be used client side (in Javascript). You create it in Javascript as object and thus it can contain methods that view can call. What you are describing (sending JSON objects using WebAPI) is just data that will feed that viewmodel. For example of this you can look on main page here knockoutjs. You can see TicketsViewModel that contains tickets array. In this example you can see three kinds of tickets hardcoded in viewmodel. But you could get them as JSON from WebAPI like you described. After downloading them just put them in this array.

Community
  • 1
  • 1
Piotr Perak
  • 10,718
  • 9
  • 49
  • 86
  • good point about " What you are describing (sending JSON objects using WebAPI) is just data that will feed that viewmodel." Because my asp.net mvc viewmodel will be sent as JSON does not mean its used as a viewmodel at client side. My asp.net mvc viewmodel is just data for the client side created viewmodel with javascript/knockoutjs... So why then the argument in this context to use a viewmodel when it has a behavior? I can not execute it anyway because its just json data on the client... then I can also return a DTO and avoid the tedious DTO to ViewModel mapping... – Elisabeth Feb 26 '13 at 19:53
  • In this context your 'viewmodel' is same as DTO I think. – Piotr Perak Feb 26 '13 at 19:57
  • Yes for this case using a javascript viewmodel I will sent my DTO instead of a server side viewmodel and my DTO has the reshaped data for the viewmodels needs on the client side. – Elisabeth Feb 26 '13 at 20:09
  • I should the the DTOs responsibility more serious. The DTO is not the Model/Shape of the View. its just to group data/service calls for saving network traffic. I am using now a ViewModel which has a shape just for the view to consume it easily by javascript. see here:http://stackoverflow.com/questions/15122485/should-automapper-also-be-used-as-a-reshaper-tool – Elisabeth Feb 27 '13 at 21:23
-1

A DTO (data transfer object) contains data in a consumable format. A ViewModel/ActionModel contains data formatted for the View to consume.

A DTO might look like:

public class OrderDTO
{
    public decimal Price { get; set; }
    public int Amount { get; set; }
}

While a ViewModel might look like:

public class OrderViewModel
{
    public decimal Price { get; set; }
    public int Amount { get; set; }
    public string PriceBackgroundColor { get; set;}
    public Uri ImageUri { get; set; }
}
Dustin Kingen
  • 20,677
  • 7
  • 52
  • 92
  • Yes I know the difference between DTO/VM. For example when I use a seperation of a DTO and ViewModel and switch to another technology where no viewmodels are used like winForms, then it would have been better when I would not have used a VM but a DTO because then all logic stays inside my Service which is reusable! What do you think? – Elisabeth Feb 26 '13 at 19:56
  • "A DTO (data transfer object) contains data in a consumable format" consumed by whom? Of course by the View ALSO formatted for the View. The difference is just the behavior. Both VM and DTO are for rich clients. The question is just do I need a VM when this is sent via json to the client. I would say no. – Elisabeth Feb 26 '13 at 20:15
  • There's nothing that prevents us from replacing DTO to ViewModel in first class and ViewModel to DTO in second. This example doesn't show anything. – Piotr Perak Feb 26 '13 at 23:04