Reputation: 24202
The title says it all...
I have an ASP.NET Mvc controller, that sends data to the view using a ViewModel
class.
Is it a bad practice, to use that same class as the return type of an web-service? Those marked with [WebMethod]
?
I guess it is not bad, since a view-model is something that is going to be rendered in the form of HTML by the view... but I need more than a guess.
Should I foresee any problem with this? Did anyone had problems by doing this and regret latter?
Upvotes: 0
Views: 110
Reputation: 156624
I think it depends on the purpose of your web service: if it's supposed to be consumed by your view code, then that makes sense. In that case, you should consider using a regular controller action instead of a WebMethod.
If it's supposed to be part of a separate web API, to be consumed by a separate presentation layer, then you'll likely end up wanting to change some aspect of the ViewModel in order to keep your view code's responsibilities limited, and be unable to make the change because it's a part of an API that other presentation layers are depending on. In that case, I'd say you should not use your ViewModel class in this way, and you should consider using the newer WebApi controller actions instead of WebMethods.
Upvotes: 2
Reputation: 37406
I dont see how reusing your classes would be a bad practice, that's the purpose of object oriented programming afterall.
Just make sure to not violate the single responsibility principle, that your viewmodel doesnt try to do too much.
Upvotes: 1