Where to put the calls to WCF or other webservices in MVVM?
I'm building Silverlight applicaitions using Prism and MVVM.
When calling WCF serv开发者_运维问答ices on your own server, or even external webservices like the Bing api, would this be done from the Model? or from the ViewModel, thus making the service the Model?
Yes. The View should only manage the transformation of data into user interface elements, the Controller should only orchestrate the interaction between views and model, and the remaining (business logic and data access, this one including calls to remote services) should be in the Model.
Sorskoot,
For your scenario, I create a Services class that interfaces with WCF. My ViewModels then interface with service.
Also, with silverlight introducing Silverlight 4, you may want to create your own models and use converters to convert the WCF objects going into your app so you have a level of separation. Plus you can start utilizing features such as IDataErrorInfo, INotifyPropertyChanged.
Search on Cinch. It's not Silverlight compatible but will give you an idea of what I'm referring too. Look at DataWrappers.
精彩评论