模型,视图,视图模型和演示者 [英] Models, Views, View Models and Presenters

查看:109
本文介绍了模型,视图,视图模型和演示者的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试掌握不同的模式(MVP,MVVM等),并找到适合我需求的模式.读完所有书后,我仍然不确定.希望有人能为我提供一些启示.

I'm trying to get to grips with different patterns (MVP, MVVM etc) and find one that suits my needs. After all my reading I'm still not sure. Hopefully someone can shed some light on this for me.

目前,我有一个实现接口ICustomView的WPF视图.该界面被注入到我的Presenter中.然后,主持人负责订阅数据,管理订阅等.当数据返回给主持人时,它将针对模型(CustomBusinessObjects的IObservable集合)调用各种方法.由于IObservable是Model的属性,因此它使用ICustomView接口进行此操作.

At the moment I have a WPF View which implements an interface ICustomView. This interface is injected into my Presenter. The presenter then is responsible for subscribing to data, managing subscriptions etc. When the data is returned to the Presenter it calls various methods against the Model (an IObservable collection of CustomBusinessObjects). It does this using the interface ICustomView since the IObservable is a property of the Model.

我看到的问题是模型与视图太耦合了.演示者还决定针对模型调用哪些方法.目前,视图由WinForms网格组成,并且ICustomView公开了该网格,从而允许Presenter针对该视图调用方法.但是,这增加了Presenter和View的耦合,这使得很难将该WinForms网格替换为WPF网格或图表等

The problem I see with this is the Model is too coupled with the View. Also the Presenter is deciding which methods to call against the Model. At the moment the View consists of a WinForms grid and this is exposed by the ICustomView allowing the Presenter to call methods against the View. However it adds to the coupling of Presenter and View which makes it difficult to swap out this WinForms grid for a WPF grid or chart etc

我正在考虑使Model成为一个完全独立的实体,可以说是使用单个方法ProcessUpdate(字符串主题,IMessage有效负载)来实现IModel.这会将逻辑从演示者转移到模型中.这也意味着一个以上的视图可以共享同一模型.定制模型可以具有用于特定定制的其他接口,但是Presenter仅需要了解IModel.

I am considering making the Model an entirely seperate entity lets say IModel with a single method ProcessUpdate(string topic, IMessage payload). This would move logic away from the presenter into the Model. It would also mean more than one view could share the same model. The custom model could have additional interfaces for specific customisations but the Presenter would only need to know about IModel.

这听起来像是一个合理的主意吗?我在这里想念什么吗?

Does this sound like a reasonable idea? Am I missing something here?

任何建议表示赞赏.

谢谢

推荐答案

我建议您从MVP切换到MVVM,因为您使用的是WPF.如果您使用的是ASP.Net或WinForms,我只会使用MVP.

I would recommend switching from MVP to MVVM because you are using WPF. I would only use MVP if you were using ASP.Net or WinForms.

话虽如此,您的MVVM对象将是:

That being said, your MVVM objects would be:

模型:简单数据对象.它不应包含任何功能(例如保存或编辑),但可以具有验证逻辑.

Model: Simple data object. It should not contain any functionality such as Save or Edit, but can have Validation logic.

查看:您的用户界面.我通常将自己作为ViewModel类类型的DataTemplate使用.它应该绑定到ViewModel的属性和命令.

View: Your UI. I usually do mine as a DataTemplate for the ViewModel class type. It should bind to your ViewModel's Properties and Commands.

ViewModel::将两者结合在一起的作品.视图中显示的任何数据都应绑定到ViewModel中的属性.视图中的任何命令(例如按钮单击")也应指向ViewModel中的方法.

ViewModel: The piece that combines the two. Any data displayed in the View should bind to a property in the ViewModel. Any commands in your View such as Button Clicks should also point to methods in the ViewModel.

例如,当用户点击视图上的GetCustomer按钮时,ViewModel应该接收命令,去获取CustomerModel,并公开它的属性以绑定到View.当用户单击保存"时,ViewModel应验证模型是否有效,然后使用其CustomerModel属性执行保存代码.

For example, when a user hits a GetCustomer button on the View, the ViewModel should receive the command, go and get the CustomerModel, and expose it's Properties for the View to bind to. When the user hits Save the ViewModel should validate that the Model is valid, and then execute the Save code using its CustomerModel property.

这篇关于模型,视图,视图模型和演示者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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