我可以只增强模型而不构建 ViewModel 吗?

发布于 2024-11-05 23:19:15 字数 228 浏览 0 评论 0原文

我知道 mvvm 要求有一个封装 Model 类的 ViewModel 类,但我想知道为什么这样做是更好的做法,而不是直接通过部分类来增强 Model 类。我知道您可能希望从 ORM 自动生成模型,但您仍然可以通过部分类将 ViewModel 内容放入另一个文件中,这样做可以避免为每个模型维护 ViewModel 的大量开销。所以我想问题是:让模型拥有面向 UI 的代码有什么不好,只要你在源代码中将其分离并且不不当使用模型的 UI 方面?

I know that mvvm calls for having a ViewModel class that wraps a Model class, but I'm wondering why it's a better practice to do this rather than just enhancing the Model class directly via partial classes. I get that you might want the Model to be auto-generated from an ORM, but you can still put the ViewModel stuff in another file via a partial class, and doing this avoids the considerable overhead of maintaining a ViewModel for every Model. So I guess the question is: what's so bad about letting the Model have UI oriented code, as long as you separate it in the source code and don't use the UI aspects of the Model inappropriately?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

陌上芳菲 2024-11-12 23:19:15

重点是关注点分离。您的 ViewModel 是专门为满足 UI 的需求而构建的;这意味着它包含的数据是专门针对 UI 进行格式化的,而您的模型是直接针对您的持久性或域逻辑进行格式化的。

在您的情况下,更改模型必然需要对视图模型进行更改;当分开时,每个都可以独立变化,从而减少意外的副作用。

The point is separation of concerns. Your ViewModel is built specifically for the needs of the UI; that means the data it contains is formatted specifically for the UI, whereas your Model is formatted directly for your persistence or domain logic.

In your situation, changing your model necesarily requires then that a change is also done to the view model; when separated, each can vary independently which reduces unintended side effects.

若能看破又如何 2024-11-12 23:19:15

让模型拥有面向 UI 的代码有什么不好,只要您将其在源代码中分离并且不不当使用模型的 UI 方面即可?

如果您拥有一套完整的单元测试,并且愿意在模型发生变化时更新所有正在使用您的模型的位置,或者您可以保证您的数据库永远不会改变,那么什么也没有。

what's so bad about letting the Model have UI oriented code, as long as you separate it in the source code and don't use the UI aspects of the Model inappropriately?

Nothing if you have a complete set of unit tests and are willing to update all locations that are using your model if it changes, or if you can guarantee that your database will never change.

春庭雪 2024-11-12 23:19:15

除了使用 viewModel 之外,您无法真正“增强”您的模型。理论上可以,但这将是非常糟糕的代码。
只是一个简单的例子:
假设您有一个只有 2 个字段的 User 类:用户名和密码。这个 User 类是您的模型。
但在您的页面上,您想再添加一个字段来编辑/添加数据:密码验证字段。
那么你打算将password2字段添加到你的User类中吗?
那就不好了!
相反,您最好创建一个绑定到视图的 ViewModel 并基于 ViewModel 的数据,然后创建模型的实例并在其他层中处理它

You can't really "enhance" your Model instead of using a viewModel. In theory you could, but it would be very bad code.
Just a simple example:
Imagine that you have a class User with only 2 fields: userName and password. This User class is your model.
But on your page you want to add one more field to edit/add the data: the password verification field.
So are you going to add password2 field into your User class?
That's bad!
Instead you better create a ViewModel binded to your View and based on the data of your ViewModel then you create the instance of your Model and process it in the other layers

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