模型、视图、视图模型和演示者
我正在尝试掌握不同的模式(MVP、MVVM 等)并找到适合我需求的模式。读完所有内容后,我仍然不确定。希望有人能为我阐明这一点。
目前我有一个 WPF 视图,它实现了 ICustomView 接口。这个接口被注入到我的Presenter中。然后,呈现器负责订阅数据、管理订阅等。当数据返回到呈现器时,它会针对模型(CustomBusinessObjects 的 IObservable 集合)调用各种方法。它使用接口 ICustomView 来完成此操作,因为 IObservable 是模型的属性。
我看到的问题是模型与视图的耦合度太高。演示者还决定对模型调用哪些方法。目前,视图由 WinForms 网格组成,并且由 ICustomView 公开,允许演示者针对视图调用方法。然而,它增加了 Presenter 和 View 的耦合,这使得很难将该 WinForms 网格替换为 WPF 网格或图表等
我正在考虑使模型成为一个完全独立的实体,让我们说 IModel 具有单个方法 ProcessUpdate(string topic, IMessage有效负载)。这会将逻辑从演示者转移到模型中。这也意味着多个视图可以共享同一模型。自定义模型可以具有用于特定自定义的附加接口,但 Presenter 只需要了解 IModel。
这听起来是一个合理的想法吗?我在这里错过了什么吗?
任何建议表示赞赏。
谢谢
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.
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.
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
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?
Any advice appreciated.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我建议从 MVP 切换到 MVVM,因为您正在使用 WPF。如果您使用 ASP.Net 或 WinForms,我只会使用 MVP。
也就是说,您的 MVVM 对象将是:
模型: 简单数据对象。它不应包含任何功能(例如保存或编辑),但可以具有验证逻辑。
查看:您的用户界面。我通常将我的数据模板作为 ViewModel 类类型的数据模板。它应该绑定到您的 ViewModel 的属性和命令。
ViewModel:将两者结合在一起的部分。 View 中显示的任何数据都应绑定到 ViewModel 中的属性。视图中的任何命令(例如按钮单击)也应该指向 ViewModel 中的方法。
例如,当用户点击视图上的 GetCustomer 按钮时,ViewModel 应该接收命令,获取 CustomerModel,并公开它的属性以供视图绑定。当用户点击“保存”时,ViewModel 应验证模型是否有效,然后使用其 CustomerModel 属性执行“保存”代码。
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.
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.
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: 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.
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.
就个人而言,在使用 WPF 时,我更喜欢使用 WPF 数据网格,并将其绑定到 MVVM 模式中的数据上下文。我认为您需要摆脱的第一件事是 WinForms 网格(只要您使用 WinForms 网格,就几乎不可能解耦您的模型/视图。
我会研究一些不同的事情。MVVM
一旦达到这一点,您需要做的就是更新数据上下文,并且您的视图也将随之更新。
Personally, when using WPF I prefer to use a WPF datagrid, and bind it to a datacontext in the MVVM pattern. I think the first thing you need to get rid of is the WinForms grid (it will be almost impossible to decouple your model/view as long as you are using a WinForms grid.
I would do research on a few different things.
Once you get to that point, all you will need to do is update your datacontext, and your view will update with it.