How to implement MVVM using WCF?

I am new to MVVM. I am currently developing a WPF project in C # that will have an SQl server server, and I will use the standard WCF service to communicate with it. All the tutorials I've seen on MVVM so far seem to always use some static data repository, such as an xml file for their backend. I have not yet seen the implementation using the database and the data access level, so I am confused as to where my WCF service fits in. Does a service have all the data objects defined in it, and does it become a model? In addition, how can I enable the service in ViewModel so that the designer does not throw an error stating that he cannot create an instance of the service class? Any help here would be greatly appreciated, since it seemed strange to me that so many textbooks on this subject omit the highest priority implementation for a business application.

PS I would like to avoid the WCF RIA and Silverlight services, since the lack of Silverlight support for teams makes my next book (Pro WPF and Silverlight MVVM Effective Application Development with Model-View-ViewModel) difficult to understand.

+4
source share
1 answer

Ok, I’ll try to get you up to speed ...

First, I really understand the issue of the model and object model exposed with WCF. They are the same? Well, I would like to make this assumption now for the sake of simplicity. So, we do not need a client side MVVM model part? Depends ...

ViewModel is located in the driver's seat. We allow it to create a client proxy for your WCF service. The objects used in the request and returned as a result make your model. Everything that you want to cache on the client side or cannot directly communicate with the user interface will be placed in the properties in the model container class. Create binding properties from these model properties for consumption in the user interface. All others will be just direct properties in your view model.

There are several important points to recognize about WCF and the level of data access. First of all, you will need a separation between your logical (information) model and your physical (base) model. One reason is to distract your database technology from the application. Another to allow slight deviations between your application / domain logic and your physical implementation. Make sure that the model classes (entities) are universal enough to support changes in the user interface without the need to change the full application stack for each change in the user interface.

It's hard to talk about this without a clear example, so for a wrapping I would like to invite you to see http://aviadezra.blogspot.com/2010/10/silverlight-mvvm-odata-wcf-data.html . I know it uses WCF and SilverLight data services. Do not be angry with me directly in order to direct this pattern and give me a thumb. This is just such a damn good example of what you want to achieve, and what to enter and what to think about creating such an application. Just replace Silverlight with WPF and Data Services with plain printed data, and the rest of the story will help you understand your thoughts.

I hope he helps you in your quest!

+10
source

All Articles