在 MVVM 中,是否每个 ViewModel 都只耦合到一个 Model? [英] In MVVM, is every ViewModel coupled to just one Model?

查看:17
本文介绍了在 MVVM 中,是否每个 ViewModel 都只耦合到一个 Model?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 MVVM 实现中,是否每个 ViewModel 只耦合到一个 Model?

In an MVVM implementation, is every ViewModel coupled to just one Model?

我正在尝试在一个项目中实现 MVVM 模式,但我发现有时一个 View 可能需要来自多个 Models 的信息.

I am trying to implement the MVVM pattern in a project but I found that sometimes, a View may need information from multiple Models.

例如,对于UserProfileView,它的UserProfileViewModel可能需要来自UserAccountModelUserProfileSettingsModel的信息>UserPostsDataModel

For example, for a UserProfileView, its UserProfileViewModel may need information from UserAccountModel, UserProfileSettingsModel, UserPostsDataModel, etc.

然而,在我读到的大多数关于 MVVM 的文章中,ViewModel 仅包含一个通过依赖注入的模型.所以构造函数只接受一个模型.

However, in most articles I read about MVVM, the ViewModel only consists on one Model via Dependency Injection. So the constructor takes in only one Model.

ViewModel 必须从多个 Models 获取信息时,它如何工作?或者在 MVVM 中会出现这种情况吗?

How would the ViewModel work when it has to get information from multiple Models? Or would such a situation ever occur in MVVM?

PS:我没有使用 Prism 或 Unity 框架.我正在尝试将类似的模式实施到我正在处理的不使用 Prism 或 Unity 的项目中.这就是为什么我需要准确了解其中一些事情的工作原理.

PS: I am not using the Prism or Unity Framework. I am trying to implement similar patterns into a project that I am working on which doesn't use Prism or Unity. That's why I need to understand exactly how some of these things work.

推荐答案

在我对 MVVM 模式的理解中,唯一实际的要求是 View 从 ViewModel 的属性中获取其所有数据(可能通过绑定机制).ViewModel 是您专门为该视图制作的类,并承担根据需要填充自身的责任.你可以把它想象成视图的 ActiveRecord.

In my understanding of the MVVM pattern, the only practical requirement is that the View gets all its data from the properties of a ViewModel (probably through a binding mechanism). The ViewModel is a class that you craft specifically for that view, and takes on the responsability of populating itself as required. You could think of it like ActiveRecord for the view.

因此,您在 ViewModel 中做什么来获取其属性应显示的数据并不重要.您可以通过查询某些服务、读取一个或多个业务实体模型、现场生成它或以上所有方式来获取它.需要将所有这些东西组合起来才能创建一个功能视图是完全正常的.

As such, it doesn't matter what you do inside the ViewModel to obtain the data that its properties should show. You could get it by querying some services, reading one or more business entity models, generating it on the spot, or all of the above. It's perfectly normal to need a combination of all these things to make a functional view.

与任何演示模式一样,重点只是将在屏幕上显示某些数据的过程与获取该数据的过程分开.这样您就可以分别测试流程的每个部分.

As in any presentation pattern, the point is just to separate the process of showing some data on the screen, from the process of obtaining that data. That way you can test each part of the process separately.

这是一个很小但希望完整的依赖流示例.

Here's a small but hopefully complete example of the flow of dependencies.

// Model/service layer

public class MyModelA
{
  public string GetSomeData()
  {
    return "Some Data";
  }
}

public class MyModelB
{
  public string GetOtherData()
  {
    return "Other Data";
  }
}

// Presentation layer

public class MyViewModel
{
  readonly MyModelA modelA;
  readonly MyModelB modelB;

  public MyViewModel(MyModelA modelA, MyModelB modelB)
  {
    this.modelA = modelA;
    this.modelB = modelB;
  }

  public string TextBox1Value { get; set; } 

  public string TextBox2Value { get; set; }

  public void Load()
  {
    // These need not necessarily be populated this way. 
    // You could load an entity and have your properties read data directly from it.
    this.TextBox1Value = modelA.GetSomeData();
    this.TextBox2Value = modelB.GetOtherData();
    // raise INotifyPropertyChanged events here
  }
}

public class MyView
{
  readonly MyViewModel vm;

  public MyView(MyViewModel vm)
  {
    this.vm = vm;
    // bind to vm here
  }
}

// Application layer

public class Program
{
  public void Run()
  {
    var mA = new MyModelA();
    var mB = new MyModelB();
    var vm = new MyViewModel(mA, mB);
    var view = new MyView(vm);
    vm.Load();
    // show view here
  }
}

这篇关于在 MVVM 中,是否每个 ViewModel 都只耦合到一个 Model?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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