简体   繁体   English

mvvm confustion与模型和业务层连接

[英]mvvm confustion with the model and business layer connection

So I have been searching for a while and all my searches on the model portion are just confusing me. 所以我一直在寻找一段时间,我在模型部分的所有搜索都让我感到困惑。

I have been seeing many examples where the view model has a direct reference to the model and then sets the model's member variables. 我已经看到许多示例,其中视图模型直接引用模型,然后设置模型的成员变量。 However isn't the model supposed to be a business object so that the business layer can do calculations with that data? 但是,该模型不应该是业务对象,以便业务层可以使用该数据进行计算?

So... 所以...

1) Should I share the model between both UI and the Business layer so both are referencing the exact same objects (shouldn't each layer "hide" their contents from each other in which case this wouldnt be the best). 1)我应该在UI和Business层之间共享模型,因此两者都引用完全相同的对象(不应该每个层“隐藏”彼此的内容,在这种情况下这不是最好的)。

http://blog.trivadis.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-01-50/1856.distributed_5F00_domain.png http://blog.trivadis.com/cfs-file.ashx/__key/communityserver-blogs-components-weblogfiles/00-00-00-01-50/1856.distributed_5F00_domain.png

2) Or should the model be in the business layer. 2)或者模型应该在业务层中。 Then the UI makes calls from the view model using provided business interfaces to access model info? 然后,UI使用提供的业务接口从视图模型进行调用以访问模型信息? This way the model itself is hidden from the UI. 这样,模型本身就会隐藏在UI之外。 Similar to option 1's image except there would be some interface accessing the models and services. 与选项1的图像类似,只是会有一些接口访问模型和服务。

3) Or UI does have the model. 3)或UI确实有模型。 BUT I can see many occasions where the business layer would need this information resulting in multiple models which look relatively the exact same are made. 但我可以看到许多场合,业务层需要这些信息,导致多个模型看起来相对完全相同。 Imagine that the entire picture below resides in the UI. 想象一下,下面的整个图片都位于用户界面中。 Then there is a business layer below the model which the model accesses. 然后在模型下方有一个业务层,模型访问该层。 http://rarcher.azurewebsites.net/Images/mvvm00.png http://rarcher.azurewebsites.net/Images/mvvm00.png

The View never has direct access to the Model. View永远不能直接访问Model。 The View is connected to the ViewModel via its DataContext. View通过其DataContext连接到ViewModel。

The ViewModel has direct access to the Model and is able to recover values from the Model and set values into it as well as call methods so that actions occur. ViewModel可以直接访问模型,并且能够从模型中恢复值并将值设置到模型中以及调用方法以便执行操作。 But the ViewModel does not have direct access to the View, there could 0, 1 or many View instances all looking at the same ViewModel and it would never know. 但ViewModel无法直接访问View,可能有0,1个或多个View实例都在查看相同的ViewModel,而且它永远不会知道。

The Model does not know about the existence of the ViewModel or View. 模型不知道ViewModel或View的存在。 Therefore you could reuse the Model class in a different project without any dependency on WPF or even a user interface. 因此,您可以在不同的项目中重用Model类,而不依赖于WPF甚至是用户界面。

How you attach the Model to the ViewModel is up to you. 如何将Model附加到ViewModel取决于您。 You could pass a reference to the Model into the constructor of the ViewModel. 您可以将对Model的引用传递给ViewModel的构造函数。 Or provide a separate method or property that is called after the ViewModel is constructed. 或者提供在构造ViewModel之后调用的单独方法或属性。

For complete separation you would expose the same list of properties on the ViewModel as are present on the Model itself (or just the subset actually needed). 对于完全分离,您将在ViewModel上公开与Model本身(或实际需要的子集)相同的属性列表。 The ViewModel can hook into the PropertyChanged event of the INotifyPropertyChanged interface that should be exposed from the Model. ViewModel可以挂钩到应该从Model公开的INotifyPropertyChanged接口的PropertyChanged事件。 As it sees changes it can pass the change onto any watching View via its own properties and its own implementation of the INotifyPropertyChanged interface. 当它看到变化时,它可以通过它自己的属性和它自己的INotifyPropertyChanged接口实现将更改传递给任何观看的视图。

As @Phil Wright states in his answer, the Model should have no knowledge of the ViewModel or the View, so you are free to reuse your Models between different layers. 正如@Phil Wright在他的回答中所述,模型应该不了解ViewModel或View,因此您可以在不同层之间重复使用模型。 In my experience, Models are generally simple POCOs with no dependencies on other layers. 根据我的经验,模型通常是简单的POCO,不依赖于其他层。

Whether you reuse your Models between your layers is up to you. 是否在图层之间重复使用模型取决于您。 If you're using an ORM such as Entity Framework, your Models might be your entity classes. 如果您使用的是ORM,例如Entity Framework,那么您的模型可能是您的实体类。 Your business layer might decide to use these entity classes, or you may want to abstract them or aggregate them or do something else with them. 您的业​​务层可能决定使用这些实体类,或者您可能希望对它们进行抽象或聚合它们或对它们执行其他操作。 At the end of the day what goes in your Models is driven by what you want to store in them and how you want to use them, and SO can't really provide guidance on that because the question is so open-ended. 在一天结束时,您的模型中的内容将由您希望存储在其中的内容以及您希望如何使用它们来驱动,并且SO无法真正提供相关指导,因为问题是如此开放。

As far as WPF and MVVM are concerned, I like to think of the ViewModel as being an aggregator of sorts for one or more Models. 就WPF和MVVM而言,我喜欢将ViewModel视为一个或多个模型的聚合器。 You generally pass the Model(s) to the ViewModel somehow (through its constructor or whatever): the ViewModel then provides properties and commands that are used by the View (via binding) in order to read and/or change the state of the ViewModel. 您通常以某种方式(通过其构造函数或其他)将模型传递给ViewModel:ViewModel然后提供View(通过绑定)使用的属性和命令,以便读取和/或更改ViewModel的状态。 At some later point, the ViewModel may need to change the state of the Models that it was supplied with, to reflect the changes initiated by the View. 稍后,ViewModel可能需要更改其提供的模型的状态,以反映View启动的更改。

You may find that the Models needed by your business layer are slightly different to the Models used by your ViewModels, and unfortunately you can end-up writing a lot of very similar-looking Model code when this happens. 您可能会发现业务层所需的模型与ViewModel使用的模型略有不同,不幸的是,当发生这种情况时,您最终可能会编写许多非常相似的模型代码。 In such cases, Model interfaces may help: there's nothing to say that the Models used by a ViewModel have to be concrete classes. 在这种情况下,Model接口可能有所帮助:没有什么可以说ViewModel使用的模型必须是具体的类。 So you could have a single "sort of complex" Model that has all the properties needed by both your ViewModels and your business layer, then expose that Model through two different interfaces (one for each layer) so that properties irrelevant to a particular layer are not visible to it (if that makes sense!). 因此,您可以拥有一个“复杂”模型,它具有ViewModel和业务层所需的所有属性,然后通过两个不同的接口(每个层一个)公开该模型,以便与特定层无关的属性为它是不可见的(如果这是有道理的!)。

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

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