ViewModel 适合 Model View Presenter 模式吗?
在 MVC 中使用 ViewModel 后,我想知道将其应用于 MVP 模式是否实用。我只有几个考虑因素,其中之一是 MVP 已经相当难以实现(所有额外的编码,表面上的复杂性并不多),或者 ViewModel 已经有一种稍微相似的数据或实体建模方式。以 ViewModel 的形式添加另一层是否多余,或者它是我作为实现 MVP 模式的人应该遵守的逻辑抽象吗?
Having used ViewModels in MVC, I was wondering if applying the same to the MVP pattern is practical. I only have a few considerations, one being that MVP is already fairly hard to implement (with all the additional coding, not much on the seeming complexity) or that ViewModels already have a slightly similar way of modeling data or entities. Would adding another layer in the form of ViewModels be redundant or is it a logical abstraction that I, as one implementing the MVP pattern, should adhere to?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
ViewModel 在 MVP 中非常有用,我认为它带来的好处比额外编码的成本更多。
我认为指导原则是在需要的地方使用它们,而不是简单地为了它们本身而添加更多模式或架构。
我正在开发一个规模相当大的面向公众的 ASP.NET Web 应用程序,但以下内容也适用于 WinForms 中的 MVP。以下是我发现在 MVP 中使用 VM 的原因。
该站点聚合来自大量 LOB Web 服务的数据。这些服务由不同业务领域的不同开发小组维护。返回的数据在以下方面到处都是:
但最大的原因我发现使用它提供的模型与 MVC 中的相同:模型只是不适合视图的形状。我们正在组合模型类并添加额外的字段用于计算或聚合值等。
就我们所做的更改而言,我们必须:
其中唯一耗时的部分自然是模型到视图模型的映射。在我们的例子中,我们被迫在演示者中进行大量的处理来获取我们需要的数据,分配属性并不是什么大问题。对于更简单的需求,像 AutoMapper 这样的东西可以消除映射的痛苦。
ViewModels can be very useful in MVP, and I think add more benefit than the cost of extra coding.
I think the guiding rule is to use them where you need them, not simply to add more patterns or architecture for their own sake.
I work on a decent sized public facing asp.net web application, but the following applies to MVP in WinForms as well. Below are the reasons I found to use VM in MVP.
The site aggregates data from a boatload of LOB web services. The services are maintained by various development groups in different business verticals. The data coming back is all over the place in terms of:
But the biggest reason I found to use it was the model provided was the same as in MVC: the models just didn't fit the shape of the views. We are combining model classes and adding extra fields for calculations or aggregated values, etc.
In terms of changes we made we had to:
The only time consuming part of this is naturally the mapping of the model(s) to the view model. In our case we are forced to do a decent amount of processing in our presenters to get the data we need that assigning properties is not a big deal. For simpler needs something like AutoMapper would remove this pain for mappings.
找到了一个与我想要的类似的 MVPVM 实现做。 (虽然我实现的时候发现它非常复杂。大量重复工作。使用代码生成工具可能会更好)
Found a MVPVM implementation that is similar to what I wanted to do. (Found it to be very complex when I implemented it though. Lots of repetitive work. Probably will be better with a code generation tool)