简体   繁体   English

我应该将此功能放在View(代码隐藏)或ViewModel中吗?

[英]Should I put this function in View (code-behind) or in ViewModel?

I am creating a simple WPF Application. 我正在创建一个简单的WPF应用程序。 I've a function OpenFile: 我有一个函数OpenFile:

private void OpenFile(string fileName)
{
   if(!File.Exists(Helper.GetPath(fileName)))
   {
      MessageBox.Show("Error opening file");
   }
   else
   {
      //Code to handle file opening
   }
}

Ideally where should this function be present? 理想情况下,此功能应存在于何处? I feel it should be in .xaml.cs because it accesses a MessageBox which comes in the View part. 我觉得它应该在.xaml.cs因为它访问View部分中的MessageBox。 But it also calls my Helper, which is in the model. 但它也称我的助手为模型中的助手。 So I also think it can be in the ViewModel . 所以我也认为它可以在ViewModel What is the advantage of having this in the View or in the ViewModel ? ViewViewModel中使用它有什么好处? Can someone help me with some pointers? 有人可以帮我提一些指示吗?

Thanks. 谢谢。

One of the advantages of placing it in the view model would be testability. 将其置于视图模型中的一个优点是可测试性。 You could write a unit test that checks that the message box is only displayed if the file exists for example (more accurately it would be an integration test if you are hitting the file system). 您可以编写单元测试,检查是否仅在文件存在时才显示消息框(如果您正在访问文件系统,则更准确地说,它将是集成测试)。

However, because you are using a message box directly, your test would never complete on a build server because the machine would be waiting for input from the user whilst the message box is displayed. 但是,由于您直接使用消息框,因此在构建服务器上永远不会完成测试,因为在显示消息框时,计算机将等待用户的输入。

Therefore, I would work against an abstraction in your view model, so that you can mock the message box during tests. 因此,我会在视图模型中反对抽象,以便您可以在测试期间模拟消息框

This function must be in the ViewModel. 此函数必须位于ViewModel中。 You need to create an operation in your view for showing the error message and call this method instead of MessageBox.Show . 您需要在视图中创建一个操作来显示错误消息并调用此方法而不是MessageBox.Show Showing the message box needs to be done in the View . 显示消息框需要在View完成。

Generally you should avoid implementing any business logic inside the View such as validating or handling a file. 通常,您应该避免在View实现任何业务逻辑,例如验证或处理文件。

If you're using Microsoft Prism you can use the IInteractionRequest interface to have the view create the MessageBox , but actually pass back the necessary response to the view-model. 如果您使用的是Microsoft Prism,则可以使用IInteractionRequest接口让视图创建MessageBox ,但实际上会将必要的响应传递给视图模型。

If you are not using Microsoft Prism, then look at how this part works and either simulate it or use a framework that does something similar. 如果您使用Microsoft Prism,那么请查看此部分的工作原理,并模拟它或使用类似的框架。


Basically, that code should go on your view-model for testability, but replace the line where you explicitly call the MessageBox and use the IInteractionRequest mentioned instead. 基本上,该代码应该在您的视图模型上进行测试,但是替换显式调用MessageBox的行并使用提到的IInteractionRequest

Here is the documentation pertinent to the scenario you're looking to implement: Chapter 6: Advanced MVVM Scenarios . 以下是与您要实现的方案相关的文档: 第6章:高级MVVM方案 Look at the section stated User Interaction Patterns . 请查看用户交互模式部分

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM