Is it bad practice to return Viewmodel from a Webservice?

Asked

Viewed 106 times

0

It is a bad practice to use the same class that is used as Viewmodel, that a Action passes to the View, to return data from a Webservice? (like those methods marked with [WebMethod])

I imagine not, but it might give me trouble someday, so I’d like to know if anyone might see any problems, or if they’ve ever had any real problems doing so.

'Cause I figured you wouldn’t: the view model, is a data object that will be sent to the user, encoded in the form of HTML... so it already represents something that can be sent to the outside world.

1 answer

1


I consider a Viewmodel in itself, as a representation of a model to a view. I don’t see as a problem the return of ViewModel from a Action or Web Service, since the information being returned is what you intend with this method.

The problem, in a case like this, is whether one of the cases needs a lot of information (example: many properties, for a detailed view of an entity) and the other with a few (example: a listing in the format of grid), in such cases it is preferable to carry out the separation into different ViewModel's, especially when working with distributed information (Web Services, Apis) where traffic is important. If the intention of the method is the same for both cases, I don’t see as a problem since you save code.

Viewmodel is not sent to the user in HTML format, this happens when your presentation layer renders HTML to the user. What is common is that in the body of requests to Apis or Web Services, this Viewmodel is serialized according to what was requested in the Header Request (content-type), and this format may vary according to the support of the API or Web Service. The most common are: json, xml and soap.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.