C# 在 MVVM 中,是否每个 ViewModel 都只与一个 Model 耦合?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13085670/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
In MVVM, is every ViewModel coupled to just one Model?
提问by Carven
In an MVVM implementation, is every ViewModelcoupled to just one Model?
在 MVVM 实现中,每个都只ViewModel与一个耦合Model吗?
I am trying to implement the MVVM pattern in a project but I found that sometimes, a Viewmay need information from multiple Models.
我试图在一个项目中实现 MVVM 模式,但我发现有时,aView可能需要来自多个Models.
For example, for a UserProfileView, its UserProfileViewModelmay need information from UserAccountModel, UserProfileSettingsModel, UserPostsDataModel, etc.
例如,对于一个UserProfileView,其UserProfileViewModel可能需要从信息UserAccountModel,UserProfileSettingsModel,UserPostsDataModel等。
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 的文章中,ViewModel 仅包含一个通过依赖注入的模型。所以构造函数只接受一个模型。
How would the ViewModelwork when it has to get information from multiple Models? Or would such a situation ever occur in MVVM?
ViewModel当它必须从多个获取信息时如何工作Models?或者在 MVVM 中会发生这种情况吗?
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.
PS:我没有使用 Prism 或 Unity 框架。我正在尝试将类似的模式实施到我正在处理的不使用 Prism 或 Unity 的项目中。这就是为什么我需要确切地了解其中一些事情是如何工作的。
采纳答案by Alex J
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.
在我对 MVVM 模式的理解中,唯一实际的要求是 View 从 ViewModel 的属性中获取所有数据(可能通过绑定机制)。ViewModel 是您专门为该视图制作的类,并承担根据需要填充自身的责任。你可以把它想象成视图的 ActiveRecord。
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.
因此,您在 ViewModel 中做什么来获取其属性应显示的数据并不重要。您可以通过查询某些服务、读取一个或多个业务实体模型、现场生成它或以上所有方式来获取它。需要所有这些东西的组合来创建功能视图是完全正常的。
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.
与任何演示模式一样,重点只是将在屏幕上显示某些数据的过程与获取该数据的过程分开。这样您就可以分别测试流程的每个部分。
Edit: 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
}
}
回答by PVitt
Usually there is one ViewModel per Model. These ViewModels contain the logic to handle the model's data. On the other side every view has it's own view model, too. So this means:
通常每个模型有一个 ViewModel。这些 ViewModel 包含处理模型数据的逻辑。另一方面,每个视图也有自己的视图模型。所以这意味着:
class ModelA
{
bool TestValue{get;set;}
}
class ViewModelA<ModelA>
{
ValueViewModel<bool> TestValue{get; private set;}
public ViewModelA(ModelA model)
{
base.Model = model;
this.Initialize();
}
}
class ModelB
{
string Username;
}
class ViewModelB<ModelB>
{
ValueViewModel<string> Username{get; private set;}
public ViewModelB(ModelB model)
{
base.Model = model;
this.Initialize();
}
}
These are the ViewModels that encapsulate the models. The views have their own ViewModels:
这些是封装模型的 ViewModel。视图有自己的 ViewModel:
public ViewModelForExactlyOneView
{
public ViewModelA{get;set;}
public ViewModelB{get;set;}
}
To answer your question, ViewModel1 refers to ViewModelA and ViewModelB. The View therefore can get it's data from ViewModel1.ViewModelA.TestValue.
为了回答您的问题,ViewModel1 指的是 ViewModelA 和 ViewModelB。因此,视图可以从ViewModel1.ViewModelA.TestValue.
回答by jgauffin
You can use multiple models in a view model. The purpose of the view model is to abstract away the business / data layer (i.e. the model).
您可以在一个视图模型中使用多个模型。视图模型的目的是抽象掉业务/数据层(即模型)。
However, using more than one model usually indicates that the view is too large. You might want to split it into user controls (which have their own view models).
但是,使用多个模型通常表示视图太大。您可能希望将其拆分为用户控件(它们具有自己的视图模型)。
回答by Andrew Grothe
A ViewModel may and in many cases does use multiple Models. It is itself a "Model" of your view.
一个 ViewModel 可能并且在许多情况下确实使用多个模型。它本身就是您视图的“模型”。
Consider a profile screen that a user enters their personal information including address. If the address is stored in an "addresses" table and the rest in a "profile" table, then the ViewModel uses both the Profile and Address models to create a unified ViewModel.
考虑用户输入包括地址在内的个人信息的个人资料屏幕。如果地址存储在“addresses”表中,其余存储在“profile”表中,则 ViewModel 同时使用 Profile 和 Address 模型来创建统一的 ViewModel。
As jgauffinmentioned in his answer, many times you can use user controls to achieve a one to one relationship, but you can also introduce needless complexity by trying for this 100% of the time.
正如jgauffin在他的回答中提到的,很多时候你可以使用用户控件来实现一对一的关系,但你也可以通过 100% 的时间尝试来引入不必要的复杂性。
回答by Akku
I would make sure you understand the difference between view, viewmodel, and all other model classes. The ViewModel is the model object that is filled with data that the view can be bound to. It just exists to provide data to the view, which makes the ViewModel object unit-testable, and the whole business logic separate from the view. So, you can develop your business logic entirely without using the view itself, and can replace the view with just building or using another view and binding to the ViewModel object's properties. If a view is full of empty text fields for example, the contents of the text fields can be bound to different properties of the view model.
我会确保您了解视图、视图模型和所有其他模型类之间的区别。ViewModel 是填充了视图可以绑定到的数据的模型对象。它只是为了向视图提供数据而存在,这使得 ViewModel 对象可进行单元测试,并且整个业务逻辑与视图分离。因此,您可以在不使用视图本身的情况下完全开发业务逻辑,并且只需构建或使用另一个视图并绑定到 ViewModel 对象的属性即可替换视图。例如,如果视图充满空文本字段,则文本字段的内容可以绑定到视图模型的不同属性。
There usually really should only be one view model. BUT if it's too complex, you can use subproperties of the bound objects like described in Binding to ViewModel.SubClass.Property (sub-property)
通常真的应该只有一个视图模型。但是,如果它太复杂,您可以使用绑定对象的子属性,如Binding to ViewModel.SubClass.Property (sub-property) 中所述
The ViewModel can get the data it returns to the view from a lot of different sources, business objects, databases, whatever.
ViewModel 可以从许多不同的来源、业务对象、数据库等获取它返回给视图的数据。
回答by blindmeis
a viewmodel contains the "view logic" - so all you wanna show on the view is exposed through the viewmodel. if you wanna show data from diffenrent "models" then your viewmodel agregate this and the view can bind to.
视图模型包含“视图逻辑” - 因此您想在视图上显示的所有内容都通过视图模型公开。如果您想显示来自不同“模型”的数据,那么您的视图模型会聚合它并且视图可以绑定到。
the main purpose from mvvm was btw unit test. this mean easy testing of view logic without UI.
mvvm 的主要目的是顺便说一下单元测试。这意味着无需 UI 即可轻松测试视图逻辑。
EDIT: why do you think:
编辑:你为什么认为:
ViewModel only has one single parameter for the View in its constructor
ViewModel 在其构造函数中只有一个 View 参数
EDIT2:
编辑2:
there btw two main approaches to work with mvvm, first is "View First" second is "Viewmodel First" you can of course mix up both and choose the best approach for you needs.
顺便说一句,使用 mvvm 有两种主要方法,第一种是“视图优先”,第二种是“视图模型优先”,您当然可以将两者混合并选择最适合您需要的方法。
回答by Monika
just use the User model in your view
只需在您的视图中使用 User 模型
public partial class User : Login
{
public string Password { get; set; }
public List<Customer> customer { get; set; }
}
in this the another model login inherited and the customer model also used in this model..
在这个模型中继承了另一个模型登录,并且在这个模型中也使用了客户模型。

