WPF 的复合指南:MVVM 与 MVP [英] Composite Guidance for WPF : MVVM vs MVP

查看:17
本文介绍了WPF 的复合指南:MVVM 与 MVP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很困惑.也许你可以帮助我:)

I am confused. Maybe you can help me :)

我一直在遵循 CAG 的指导,发现 MVP 模式对我来说非常自然.假设我有一个 UI 就绪模型(例如:实现 INotifyPropertyChanged),我使用演示者将此模型绑定到视图(演示者知道视图的接口),使我的代码隐藏尽可能小,仅处理绑定(模型和命令)属性(或方法)或没有 ICommand 的控件的事件,在这种情况下会立即委派给演示者.

I have been following the guidance of CAG and found the MVP pattern very natural to me. Suppose I have a UI-ready Model (for example : implements INotifyPropertyChanged), I use the presenter to bind this Model to a view (presenter knows an interface of the view), keeping my Code-Behind as small as possible handling only Bindings (Model and Commands) properties (or methods) or events for controls that don't have ICommand and in this case immediately being delegated to the presenter.

  1. 一段时间后,我发现了 MVVM 模式,但到目前为止我还没有发现它.据我所知,只有当我的模型不是 UI-ready 时,我才会使用 MVVM.但是保留演示者并仅使用新模型是否更合理,我无法理解这种用法​​会丢失什么.我知道我错过了一些东西,但它是什么:)

  1. After a while I've discovered the MVVM pattern, and so far it eludes me. As far as I can tell in my approach I would use MVVM only when my Model is not UI-ready. But would it be more reasonable to keep the presenter and just use a new Model, I fail to understand what do I lose with this kind of usage. I know I am missing something, but what is it :).

此外,当您的视图是通用的并且可以处理多种模型时(例如在 PropertyGrid 中).建议 ViewModel 与 DataTemplate 一起使用,但在这种情况下,您无法为 Model 中的每个实体创建一个模板,只需要在运行时进行调查,您有什么建议?

Also when your View is generic and can handle many kinds of Models (such as in a PropertyGrid). ViewModel is recommended to be used with a DataTemplate, but in this case you just can't create a Template for each entity in your Model, it is just need to be investigated in runtime, what would you recommend?

在观看 Josh Smith 在 截屏,感觉ViewModel中Model的重新暴露违反了DRY(不要重复),真的是不可避免的吗?与火焰相比,他的争论让我感到惊讶 ADO.Net 动态数据元数据类现在越来越多.

While watching Josh Smith talking about MVVM in a screencast, I got a feeling that the re exposing of the Model in the ViewModel is violating DRY (do not repeat yourself), is it really unavoidable? it surprises me nobody his arguing about it in comparison for the flames ADO.Net Dynamic Data metadata classes are getting nowadays.

希望它足够清楚

谢谢

爱丽儿

推荐答案

关于#3,很多人会使用另一层间接"的说法,说模型的变化不会影响视图.虽然这在技术上是正确的,但这并不是做这样的事情的真正原因.

Regarding #3, a lot of people will use the "another layer of indirection" argument, saying that changes in the model won't affect the view. While this is technically correct, it's not the real reason to do something like this.

如果您将模型视为从数据访问层或服务(通常被认为是这些)返回的实体,您就会开始明白为什么需要 ViewModel.ViewModel 旨在使用 View 需要的行为扩展模型.

If you consider the Model as the entities you get back from, say, a data access layer or a service (which is what these are generally considered) you begin to see why you need a ViewModel. A ViewModel is designed to extend the model with behaviors the View needs.

例如.如果您希望能够更改属性并通过绑定将更改通知 View,则该属性需要引发某种形式的 NotifyPropertyChanged,以便视图可以做出反应.这是您的典型模型所不具备的行为.

For example. If you want to be able to change a property and have the View notified of this change through binding, the property needs to raise some form of NotifyPropertyChanged so that the view can react. This is behavior that your typical model won't have.

在另一个示例中,假设您有一个集合,并且当用户单击视图中该项目旁边的复选标记时,您希望使用布尔值标记该集合中的每个项目.您可能需要一个IsSelected"属性.这是模型不应该提供的行为.

In another example, let's say you have a collection and you'd like to flag each item in the collection with a boolean value when a user clicks a checkmark next to that item in the view. You'd need an "IsSelected" property, probably. This is a behavior that the Model shouldn't have to provide.

但是我知道你来自哪里......我一开始肯定有问题.我第一次将模型的内容复制并粘贴到我的视图模型中时,我的胃转了转,但是您必须接受这样一个事实,即为了让您的视图工作,它需要模型应该具有的这种额外的行为不提供.

However I see where you are coming from... I definitely had a problem with this at first. The first time I copy and pasted the contents of a model into my viewmodel, my stomach turned, but you just have to make peace with the fact that for your View to work, it's going to need this extra bit of behavior that a Model shouldn't provide.

无论这多么不干燥,强制您的 WCF 类型或 LINQ to SQL 类型(或任何您最喜欢的 ORM 类型)实现 INotifyProperyChanged 都更糟.

No matter how un-DRY this is, forcing your WCF types or LINQ to SQL types (or whatever your favorite ORM is) to implement INotifyProperyChanged is worse.

这篇关于WPF 的复合指南:MVVM 与 MVP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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