本文介绍了我可以使用WCF接口作为一个MVVM模型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的WPF应用程序应该通过WCF接收管数据:
我是新来的MVVM,我能不明白的地方放一些东西。很显然,对我来说是什么看法,但它并不清楚什么是视图模型,尤其是什么在我的情况是模型(在情况下,当WCF参与)的问题是:




  • 是什么型号?我是否需要特殊的类来表示模式?或型号是WCF服务器应用程序?什么是有效的worflow WCF服务器应用程序----命名管道----->视图模型< - 数据绑定 - >查看WCF服务器应用程序--- -named管----->型号< --->视图模型< - 数据绑定 - >查看

  • 如何重构下面的代码?在这里把所有这个初始化?到视图模型?以模型类(如果我需要的话),以特殊的初始化级?



     公共主窗口()
    {
    &的ChannelFactory LT; IManagementConsole> pipeFactory =
    新的ChannelFactory< IManagementConsole>(
    新NetNamedPipeBinding(),
    新的EndpointAddress(
    net.pipe://本地主机/ PipeMBClientManagementConsole));

    IManagementConsole pipeProxy =
    pipeFactory.CreateChannel();

    名单,LT; ConsoleData> DATAS =新的List< ConsoleData>();
    的foreach(StrategyDescriptor SD在pipeProxy.GetStrategies())
    {
    datas.Add(pipeProxy.GetData(sd.Id));
    }
    this.DataContext = DATAS;
    }




我可以假设,这是我的MVVM模式?



  [的ServiceContract] 
公共接口IManagementConsole
{
[OperationContract的]
ConsoleData的GetData(INT strategyId);

[OperationContract的]
名单,LT; StrategyDescriptor> GetStrategies();
}


解决方案

一个模型是一个类,描述了从后端数据源的数据。它们可以是从源(喜欢EF或WCF代理)的实际的类或者它们可以是简单的DTO。这真的取决于你的喜好。



一个视图模型是描述在查看显示数据的类。通常情况下,但不一定,数据来自Model类



视图模型负责管理Model类:暴露自己的信息,以查看和做一些用/他们根据从视图的输入。话虽这么说,我不喜欢看到的视图模型实际的通讯工作。相反,我抽象通信到另一个层(我称之为我的服务层,但可以成为这方面的混乱)。



在本质上,我有视图模型制作请求与所述后端通信以检索/创建模型对象,然后返回到视图模型的服务层。 。如果服务调用是异步的,但还是可以做到这变得复杂了。



有关的一个简单的例子,请从的。


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.

这篇关于我可以使用WCF接口作为一个MVVM模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 07:35
查看更多