简体   繁体   English

使用1个模型的多个viewmodel?

[英]Multiple viewmodels using 1 model?

I have viewmodel1 and viewmodel2. 我有viewmodel1和viewmodel2。

Viewmodel1 needs to show data from a list of some sort contained in model. Viewmodel1需要显示模型中包含的某种列表中的数据。 Viewmodel2 needs to input data to the list contained in model. Viewmodel2需要将数据输入到模型中包含的列表中。

So both Viewmodel1 and Viewmodel2 need to "know" about model. 因此,Viewmodel1和Viewmodel2都需要“了解”模型。

What is the proper way to do this in MVVM? 在MVVM中执行此操作的正确方法是什么?

Should I create model in app and give viewmodel1 and viewmodel2 a reference to it or? 我应该在应用程序中创建模型并给viewmodel1和viewmodel2引用吗?

I find it helps if I think of a ViewModel as being a Model, translated just for the View. 如果将ViewModel视为仅针对View进行翻译的模型,我发现它会有所帮助。

You have a few choices: 您有几种选择:

  • Create a controller which sets up the ViewModel. 创建一个设置ViewModel的控制器 This is usually a good choice if the ViewModel needs information from more than one place. 如果ViewModel需要来自多个地方的信息,通常这是一个不错的选择。 You can either give the ViewModel a reference to all the information it needs, or make it a Plain Old .NET Object (PONO) and have the controller set it up for you. 您可以给ViewModel引用它需要的所有信息,也可以使其成为一个普通的.NET对象(PO​​NO)并让控制器为您设置它。

  • Just pass the reference to the ViewModel as you suggest. 只需按照您的建议将引用传递给ViewModel。 Useful if no interaction is required between different classes 如果不同类之间不需要交互,则很有用

  • Wire up the EventAggregator , which can be used to publish a notification when models change, and pass a Repository to the ViewModels so that each of them can go and get / store the model when they need to. 连接EventAggregator ,该控件可用于在模型更改时发布通知,并将存储库传递给ViewModel,以便它们中的每个都可以在需要时去获取/存储模型。

I really like the last pattern as it's easily scalable if you find you need more presenters or controllers with access to these kinds of models. 我非常喜欢最后一种模式,因为如果您发现需要更多的演示者或控制器可以访问这些类型的模型,那么它很容易扩展。 It also lets you inject a repository, which means you can more easily move to a nice RESTful service-oriented architecture later. 它还允许您注入存储库,这意味着您以后可以更轻松地迁移到一个不错的RESTful面向服务的体系结构。

If you haven't done much dependency injection before then please consider doing it through the constructor. 如果您之前没有做过很多依赖注入,请考虑通过构造函数进行。 This will help you avoid situations where things are trying to use your ViewModels before they're ready, and lets the ViewModels do their own work. 这将帮助您避免事情在准备就绪之前尝试使用ViewModel的情况,并使ViewModel自己完成工作。 You might also want to look at frameworks like Unity or Castle Windsor which can help you do this kind of wiring, but that's really only for big Enterprise projects (and not even all of them). 您可能还希望查看诸如UnityCastle Windsor之类的框架,它们可以帮助您进行这种连接,但这实际上仅适用于大型Enterprise项目(甚至不是全部)。

That's perfectly fine. 很好 Models are meant to be just what they sound like: data models. Models就是听起来像的:数据模型。 They're meant to be dummy objects that hold data, which the rest of the application can use as needed. 它们本应是保存数据的伪对象,应用程序的其余部分可以根据需要使用它们。

ViewModels are models that reflect the View. ViewModels是反映View的模型。 For example, suppose you had a LoginViewModel and an ManageUsersViewModel . 例如,假设您有一个LoginViewModelManageUsersViewModel Both ViewModels would work with the UserModel data object, however they are entirely different ViewModels for entirely different things. 两种ViewModel都可以与UserModel数据对象一起使用,但是对于完全不同的事物,它们是完全不同的ViewModel。

In most cases, I would leave the responsibility of loading Models up to the ViewModel . 在大多数情况下,我将负责将Models加载到ViewModel的责任。 For example, you normally wouldn't pre-load the Users list prior to the User logging in so you'd have their User object available. 例如,您通常在用户登录之前不会预先加载“用户”列表,因此您可以使用其“用户”对象。 Insetad, your LoginViewModel would make it's own database call to get the User model of the user logging in, while the ManagerUsersViewModel would make its own database call to get the list of users that can be modified. 在Insetad中,您的LoginViewModel将进行其自身的数据库调用以获取登录用户的User模型,而ManagerUsersViewModel将进行其自身的数据库调用以获取可以修改的用户列表。

You must be thinking in a wrong way.. in MVVM your model are contained in ViewModels all the way to the views.. lets say if there is a ObservableCollection then there will be a property in you viewmodel for this and you should be initializing this collection in constructor/some method of the ViewModel.. both viewmodels will initialize ProductTypes like this.. In my opinion you should try create Classed For Model,ViewModel,Repository and use IoC in it.. 您必须以错误的方式进行思考..在MVVM中,您的模型一直包含在ViewModels的视图中。.假设如果存在ObservableCollection,则viewmodel中将有一个属性,您应该对此进行初始化在构造函数/ ViewModel的某些方法中集合。两个视图模型都将像这样初始化ProductTypes。我认为您应该尝试创建Classed For Model,ViewModel,Repository并在其中使用IoC。

here is a really good video on mvvm you should try this video and have a look at the code as well. 这是mvvm上的一个非常不错的视频,您应该尝试该视频并同时看一下代码。

http://blog.lab49.com/archives/2650 http://blog.lab49.com/archives/2650

If the List you mentioned is something that will never change you should try creating a singleton ViewModel for this and reference that viewmodel in other viewmodels. 如果您提到的列表永远不变,则应尝试为此创建一个单例ViewModel并在其他视图模型中引用该视图模型。

Regards. 问候。

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

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