为什么要在viewmodel上实现一个接口,在mvvm中实现view [英] Why implement an interface on viewmodel and view in mvvm

查看:85
本文介绍了为什么要在viewmodel上实现一个接口,在mvvm中实现view的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 MVVM 模式很陌生,所以请耐心等待.我在 wpf +mvvm +prism 中看到了实现,其中所有视图都倾向于将 IView 作为最顶层的界面.然后各个模块中的视图都有一个视图特定的接口,如 IViewA、IViewB 等,它们实现了 IView 接口.甚至视图模型都有 IViewModel 最顶层的接口,后续模块有 IViewAViewModel , IViewBViewModel 等继承自 IViewmodel.IViewmodel 引用了 Iview,而 Iview 引用了 IViewModel.

I am quite new to MVVM pattern so please bear with me. I have seen implemnentations in wpf +mvvm + prism where all the views tend to have an IView as top most interface. Then the views in the respective modules have a view specific interface like IViewA, IViewB etc which implement the IView interface. Even the viewmodel has IViewModel top most interface and the subsequent modules have IViewAViewModel , IViewBViewModel etc that inherit from the IViewmodel. IViewmodel has a reference to Iview and the Iview has a reference to IViewModel.

namespace xxx.xxx.infrastructure
{
public interface IView
{
  IViewModel ViewModel {get;set;}
}

public interface IViewModel 
{
  IView View {get;set;}
}

public abstract class ViewModelBase : IViewModel, INotifyPropertyChanged
{

   public IView View {get;set;}

   public ViewModelBase(IView view)
   {
     View = view;
     View.ViewModel = this;
   }
   //INotifyPropertyChanged left out
 }
}

namespace xxx.xxx.Modules.Customer
{
   public interface ICustomerDetailsView : IView
   {

   }

   public partial Class CustomerDetailsView : UserControl, ICustomerDetailsView 
   {
       public CustomerDetailsView ()
       {
         InitializeComponent();
       }

       //Is this implementation acceptable?The view is supposed to have zero code in       the code behind.....
        public IViewModel ViewModel
        {
          get
          {
            return (ICustomerDetailsViewViewModel)DataContext; 
          }
          set
          {
             DataContext = value;
          }
        }

    }  

    public interface ICustomerDetailsViewViewModel : IViewModel
    {
       string Message {get;set;}
    }

     public class CustomerDetailsViewViewModel : ViewModelBase,       ICustomerDetailsViewViewModel 
    {
      //Will be injected by unity as i have set up mappings in module initilize.
      public CustomerDetailsViewViewModel(ICustomerDetailsView view)
          :base(view)
      {
      }

       public string Message
       {
          //INotifyPropertyChanged left out for brevity
          get;set;
       }
   }

我有几个问题.

1.)这是否违反了 MVVM,因为文件背后的代码应该为零代码?

1.)Isnt this violation of MVVM as code behind file is supposed to have zero code?

2.) 在 MVVM 视图模型中不应该担心视图或其契约吗?上面的实现不会破坏它吗?

2.)in MVVM view model should not worry the view or its contract?Doesnt the above implementation break it?

3.) 我不明白这个实现有什么用.事实上,这与 MVP 接壤,需要大量代码.

3.)I cannot understand what is the use of this implementation. In fact this borders on MVP and lot of code is required.

4.)如果这是一种可接受的实现方式,我是否需要为所有模块中的所有视图和视图模型提供接口.?

4.)If this is an acceptable way to implement, do i need to have interfaces for all the views and viewmodels in all of my modules.?

推荐答案

基本上,我们只需要用于注入的接口.有两种方式:

Basically, we need interfaces for injections only. There are 2 ways:

  • 将视图模型注入视图

这意味着 ViewModel 不再有对 View 的任何反向引用.这意味着在对 ViewModel 进行单元测试时,您不需要模拟视图.此外,它还使代码更简洁,因为在 View 的构造函数中,它只是将 DataContext 设置为注入的 ViewModel.

It will mean that the ViewModel no longer has any back reference to the View. This means when unit-testing the ViewModel you don't need a mock view. Additionally it makes the code cleaner, in that in the constructor of the View, it simply sets the DataContext to the ViewModel that was injected.

  • 将视图注入到 ViewModel.

它避免将工作流逻辑保留在表示层中.这意味着应用程序层负责应用程序工作流.这样,工作流逻辑与视图高度耦合,因此几乎不可能为此编写单元测试.

It avoids to keep workflow logic into the Presentation layer. It means that the Application layer keeps responsible for the application workflow. This way the workflow logic is highly coupled with the Views and so it is quite impossible to write unit test for this.

这篇关于为什么要在viewmodel上实现一个接口,在mvvm中实现view的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆