开发者

Strongly typing a view to a class from the webservice?

Currently I use a webservice to call the database and pass back an object back to my MVC app. The purpose is to display the data on a table.

So would it better if I just type the View to the class from the webservice, or should it always be typed to a model inside the MVC ap开发者_JAVA技巧p? If I type it to a model inside my app, then I would have to create a new instance of the class and copy all the information over, which seems like a waste of time.

Which one is considered best practice? Strongly typing to a class from the webservice or a class within the MVC app?

Cheers.


Best practice is to shield your application from external changes and your views from knowing the internals of the application. So I would just create a separate class and then map it to the web service class. You can also use something like AutoMapper to do the mapping for you.


You shouldn't have a dependency on the class from WebService. MVC's ViewModel is desined to be simple data representation. You can easely map your WebService class to ViewModel class using AutoMapper. This is considered as best practice and described in this book.


Yes. The best practice is to use a view model with no coupling to the domain model - a simple DtO. However, any validation applied to your domain needs to be replicated. So sometimes the trade-off is to pass domain entities to the view - and I'm very happy to do that.

In your case - go with the DTO as others on here suggest.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜