ViewModel 适合 Model View Presenter 模式吗?

发布于 2024-09-06 00:27:15 字数 186 浏览 5 评论 0原文

在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

陪你搞怪i 2024-09-13 00:27:15

ViewModel 在 MVP 中非常有用,我认为它带来的好处比额外编码的成本更多。

我认为指导原则是在需要的地方使用它们,而不是简单地为了它们本身而添加更多模式或架构。

我正在开发一个规模相当大的面向公众的 ASP.NET Web 应用程序,但以下内容也适用于 WinForms 中的 MVP。以下是我发现在 MVP 中使用 VM 的原因。

该站点聚合来自大量 LOB Web 服务的数据。这些服务由不同业务领域的不同开发小组维护。返回的数据在以下方面到处都是:

  • Type Soup - 将 GUID 存储为字符串,返回双精度数而不是小数,日期作为字符串等
  • 疯狂的命名约定 - 驼峰式大小写属性,名称中的下划线,缩写混乱

但最大的原因我发现使用它提供的模型与 MVC 中的相同:模型只是不适合视图的形状。我们正在组合模型类并添加额外的字段用于计算或聚合值等。

就我们所做的更改而言,我们必须:

  • 在 Views 和 Presenters (Controllers) 文件夹旁边创建一个新的 ViewModels 文件夹
  • 将模型值映射到视图模型
  • 将视图接口上的属性从模型类型更改为视图模型类型
  • 根据新对象实现视图

其中唯一耗时的部分自然是模型到视图模型的映射。在我们的例子中,我们被迫在演示者中进行大量的处理来获取我们需要的数据,分配属性并不是什么大问题。对于更简单的需求,像 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:

  • Type Soup - Storing GUIDs as strings, returning doubles instead of decimals, dates as strings, etc
  • Crazy Naming Conventions - Camel case properties, underscores in names, abbreviation jumbles

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:

  • Create a new ViewModels folder along side the Views and Presenters (Controllers) folder
  • Map the model values to the view model
  • Change the property on the view interface from a model type to a view model type
  • Implement the view according to the new object

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.

打小就很酷 2024-09-13 00:27:15

找到了一个与我想要的类似的 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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文