简体   繁体   English

一些MVVM问题(WPF C#)

[英]Some MVVM questions (WPF C#)

I have been looking into MVVM recently and I seem to get the overall idea. 我最近一直在研究MVVM,我似乎得到了整体的想法。 There are a couple of niggly bits though that I do not fully understand and was hopping to get some answers here, cheers! 虽然有一些不起眼的东西,但我并不完全理解,并且正在跳跃,在这里得到一些答案,干杯!

  1. Is it incorrect to use one data model for the whole application. 将一个数据模型用于整个应用程序是不正确的。 Usually if I am creating a small utility I would have all of the logical data in one class. 通常,如果我创建一个小实用程序,我会在一个类中拥有所有逻辑数据。 This means i can have somethings like the following: 这意味着我可以拥有如下内容:

     DataStore myData = new DataStore; 
  2. If it is OK to have one data model is it ok to have more than one model view, say one representing each window or view (This is how I envision MVVM working). 如果可以拥有一个数据模型就可以拥有多个模型视图,比如说一个代表每个窗口或视图(这就是我设想的MVVM工作方式)。

  3. Given then above if one has multiple model views it would seem that the model would have to be declared before the first window (view), where should it be declared? 如果有一个具有多个模型视图,那么上面给出的模型似乎必须在第一个窗口(视图)之前声明,应该在哪里声明它? should the model be passed via a reference to subsequent model views? 模型是否应通过对后续模型视图的引用传递? Would this not be a source of coupling as the window or page (view) would need to know about the model to pass it to its model view since the view instantiates the model view. 由于视图实例化模型视图,因此窗口或页面(视图)需要了解模型以将其传递到模型视图,这不会成为耦合源。

Sorry if this is a lot of questions, I get the idea of MVVM in a single window or page sense but once I add multiple views my system breaks down. 很抱歉,如果这是一个很多问题,我会在一个窗口或页面感知中获得MVVM的想法,但是一旦我添加多个视图,我的系统就会崩溃。 I can get it to work with seperate models accessing an outside source to grab its data but if the data needs to persist between views I get lost. 我可以使用单独的模型访问外部源来获取其数据,但如果数据需要在视图之间持续存在,我就会迷失方向。

Thanks to all that take the time to respond! 感谢所有花时间回应!

Some thoughts: 一些想法:

  1. Simple applications don't necessarily require the complexity of MVVM - you may be able to get away with eliminating the ViewModel - and using the underlying model directly. 简单的应用程序不一定需要MVVM的复杂性 - 您可以通过消除ViewModel并直接使用底层模型来逃脱。 Just be careful that you don't stretch this approach to the breaking point - as WPF is very reliant on things like INotifyPropertyChanged, and DependencyProperties. 请注意,不要将此方法延伸到断点 - 因为WPF非常依赖于诸如INotifyPropertyChanged和DependencyProperties之类的东西。 You don't want to start merging these into your model classes unecessarily. 您不希望开始将这些合并到您的模型类中。

  2. Yes, it's ok. 好的,可以。 However, ... keep in mind it's simpler if there is only one Model instance - otherwise you need to deal with merging changes from multiple views when you are going to save (or losing one version's changes). 但是,请记住,如果只有一个Model实例,则更简单 - 否则,当您要保存(或丢失一个版本的更改)时,您需要处理来自多个视图的合并更改。 If the ViewModels refer to the same underlying model this can be workable. 如果ViewModels引用相同的底层模型,那么这是可行的。

  3. You cannot avoid a certain level of coupling in MVVM. 您无法避免MVVM中的某种级别的耦合。 However, (if I understand your question correctly) introducing coupling between ModelViews is probably a bad idea - since it is defeating the purpose of making each a discrete perspective on the model optimized for a particular view. 但是,(如果我正确理解你的问题)在ModelViews之间引入耦合可能是一个坏主意 - 因为它打破了为特定视图优化模型的每一个离散透视图的目的。

My own Experience: 我自己的经历:

  1. I do not see anything wrong with using one data model for your entire application, depending on what you are wanting to do. 我认为在整个应用程序中使用一个数据模型没有任何问题,具体取决于您想要做什么。 As long as you keep it separate from your View you are staying within the MVVM design pattern. 只要您将其与View分开,您就会保持在MVVM设计模式中。

  2. I personally like using a ModelView for each one of my Views. 我个人喜欢为我的每个视图使用ModelView。 I think there are different arguments on it, but I personally think everything stays more modular and unit testable by doing this. 我认为它有不同的论点,但我个人认为通过这样做,一切都更加模块化和单元可测试。

  3. I try to avoid coupling between ModelViews. 我试图避免ModelViews之间的耦合。 If i have a ModelView for each of my Views I declare each ModelView separately in each one of the Views. 如果我的每个视图都有一个ModelView,我会在每个视图中单独声明每个ModelView。 I then instantiate each View into my Main View through events. 然后,我通过事件将每个视图实例化到我的主视图中。 This stays with the MVVM pattern, and you can Unit Test each one of your ModelViews separately. 这与MVVM模式保持一致,您可以单独对每个模型视图进行单元测试。

I wouldn't be surprised if you have already read this article, but for those out there wanting to learn about the MVVM design here's a good Link . 如果您已经阅读过本文,我不会感到惊讶,但对于那些希望了解MVVM设计的人来说,这是一个很好的链接

Is it incorrect to use one data model for the whole application. 将一个数据模型用于整个应用程序是不正确的。 Usually if I am creating a small utility I would have all of the logical data in one class. 通常,如果我创建一个小实用程序,我会在一个类中拥有所有逻辑数据。 This means i can have somethings like the following: 这意味着我可以拥有如下内容:

 `DataStore myData = new DataStore; ` 

This is fine. 这可以。 Your data model is your data model and should represent the data in the best way possible, be it in one class or 1000. 您的数据模型是您的数据模型,应尽可能以最佳方式表示数据,无论是一个类还是1000个。

If it is OK to have one data model is it ok to have more than one model view, say one representing each window or view (This is how I envision MVVM working). 如果可以拥有一个数据模型就可以拥有多个模型视图,比如说一个代表每个窗口或视图(这就是我设想的MVVM工作方式)。

Absolutely, in my opinion that's exactly what you should do. 当然,在我看来,这正是你应该做的。 In general I would say you want one view model per view, whether a view is a control or a window. 一般来说,我会说每个视图都需要一个视图模型,无论视图是控件还是窗口。 There may be cases where you find that having more than one view model for a particular view would be helpful, but I would look first to make sure I didn't have a view that should be split up. 在某些情况下,您会发现为特定视图设置多个视图模型会有所帮助,但我首先要确保没有应该拆分的视图。

Given then above if one has multiple model views it would seem that the model would have to be declared before the first window (view), where should it be declared? 如果有一个具有多个模型视图,那么上面给出的模型似乎必须在第一个窗口(视图)之前声明,应该在哪里声明它? should the model be passed via a reference to subsequent model views? 模型是否应通过对后续模型视图的引用传递? Would this not be a source of coupling as the window or page (view) would need to know about the model to pass it to its model view since the view instantiates the model view. 由于视图实例化模型视图,因此窗口或页面(视图)需要了解模型以将其传递到模型视图,这不会成为耦合源。

Now you're entering religious territory. 现在你正在进入宗教领域。 I'm going to punt on this a little and say do what makes sense for your application. 我会稍微解决这个问题并说出对你的应用程序有意义的事情。 That said, if you want to reduce the coupling between your model and your views / view models, I would highly recommend extracting an interface from your model class(es). 也就是说,如果您想减少模型与视图/视图模型之间的耦合,我强烈建议从模型类中提取接口。 It would be fairly simple to create your model in your App.xaml.cs file and then pass it to the view (model) as an implementation of an interface. 在App.xaml.cs文件中创建模型然后将其作为接口的实现传递给视图(模型)将非常简单。

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

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