我的WPF應用程序應通過WCF從管道接收數據: 我是MVVM的新手,我無法理解在哪裏放置一些東西。我很清楚什麼是視圖,但不清楚什麼是ViewModel,尤其是什麼是模型在我的情況下(在WCF涉及的情況下)問題是:我可以使用WCF接口作爲MVVM模型嗎?
- 什麼是模型?我需要特殊的班級來代表模特嗎?或模型是WCF服務器應用程序?什麼是有效的工作流程「WCF服務器應用程序----命名管道-----> ViewModel < - 數據綁定 - >查看」或「WCF服務器應用程序----命名管道----- > Model < ---> ViewModel < - 數據綁定 - >查看「
如何重構下面的代碼?在哪裏把所有這些初始化?到ViewModel?到Model類(如果我需要它),到特殊的「初始化」類?
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; }
我可以假設,這是我的MVVM模型? :
[ServiceContract]
public interface IManagementConsole
{
[OperationContract]
ConsoleData GetData(int strategyId);
[OperationContract]
List<StrategyDescriptor> GetStrategies();
}
謝謝,我喜歡那樣!我不喜歡用「溝通」的東西來玷污模特。所以ViewModel最適合MVVM,但引入一個「服務層」可能會有所幫助。這也意味着我需要爲模型添加一個類,並且IManagementConsole不是模型,因爲模型可以包含額外的東西,而不僅僅是來自WCF的數據...服務層應該通過WCF到服務器應用程序並創建在ViewModel中使用的模型實例。 – javapowered