在MVVM,每视图模型耦合到只有一个型号? [英] In MVVM, is every ViewModel coupled to just one Model?

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

问题描述

在一个MVVM的实现,是每一个视图模型连接到只有一个模式

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

我想在项目中实现MVVM模式,但我发现,有时候,一个查看可能需要从多个信息模式

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 可能需要从信息 UserAccountModel UserProfileSettingsModel UserPostsDataModel

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

不过,在我读到MVVM大多数文章,视图模型只包含在通过依赖注入一个模型。所以构造函数中只有一种模式。

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.

怎么会当它有获得多个模式信息视图模型工作?或将这样的情况曾经发生在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:我不使用棱镜或统一框架。我想实现类似的模式进入,我上工作不使用棱镜或统一的项目。这就是为什么我需要知道究竟是如何对这些事情的工作。

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模式的理解,唯一的实际要求就是查看会从一个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.

因此​​,它并不重要视图模型内执行的操作,以获得其性能应显示数据。你可以通过查询一些服务,读一个或多个业务实体的模型,它产生在现场,或上述所有得到它。这是完全正常的需要的所有这些事情的结合,使一个功能视图。

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.

正如在任何presentation图案,点仅仅是在屏幕上显示一些数据的过程中分离,从获得的数据的过程。这样,你可以单独测试过程的每个部分。

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,每视图模型耦合到只有一个型号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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