My WPF application should receive data from pipe via WCF:
I’m new to MVVM and I can’t understand where to put some stuff. It’s clear for me what is View but it not clear what is ViewModel and especially what is Model in my case (in case when WCF involved) The questions are:
- what is Model? Do I need special class to represent model? Or Model is WCF server application? What is valid worflow “WCF Server application —-named pipe—–> ViewModel <–Data binding–> View” or “WCF Server application —-named pipe—–> Model<—>ViewModel <–Data binding–> View”
-
how to refactor code below? where to put all this initialization? to ViewModel? to Model class (if I need it), to special “initialization” class?
public MainWindow() { ChannelFactory<IManagementConsole> pipeFactory = new ChannelFactory<IManagementConsole>( new NetNamedPipeBinding(), new EndpointAddress( "net.pipe://localhost/PipeMBClientManagementConsole")); IManagementConsole pipeProxy = pipeFactory.CreateChannel(); List<ConsoleData> datas = new List<ConsoleData>(); foreach (StrategyDescriptor sd in pipeProxy.GetStrategies()) { datas.Add(pipeProxy.GetData(sd.Id)); } this.DataContext = datas; }
Can I assume that this is my MVVM Model? :
[ServiceContract]
public interface IManagementConsole
{
[OperationContract]
ConsoleData GetData(int strategyId);
[OperationContract]
List<StrategyDescriptor> GetStrategies();
}
A Model is a class that describes data from backend data source. They can be the actual classes from the source (like EF or WCF Proxies) or they can be simple DTOs. It really depends on your preference.
A ViewModel is a class that describes data for display in the View. Frequently, but not necessarily, the data comes from Model classes.
The ViewModel is responsible for managing the Model classes: exposing their information to the View and doing something with/to them based on input from the View. That being said, I prefer not to see the actual communications work in the ViewModel. Instead, I abstract the communications into another layer (I call mine the Service layer, but that can be confusing in this context).
In essence, I have the ViewModel make requests to the Service layer which communicates with the backend to retrieve/create Model objects that are then returned to the ViewModel. This gets complicated if the service calls are Asynchronous but can still be done.
For a simple example of this, download the code sample from practicalmvvm.com.