WPF中MVVM下项目的业务逻辑?

发布于 2024-10-18 12:08:07 字数 196 浏览 8 评论 0原文

我正在使用 MVVM 模式在 WPF 中开发一个应用程序。例如,如果我有学生作为数据模型,我可以在哪里添加业务逻辑,例如添加新学生、删除学生和更新学生信息、计算分数等?

所有业务逻辑都位于模型视图模型下吗?

如果它在模型中我们该怎么做?请问有示例代码吗?我需要完整的代码作为示例!

I am developing an application in WPF using the MVVM pattern. If I have Student as a data model, for example, where could I add the business logic like adding new Students, deleting Students and updating Student info, calculating marks and so on?

Does all the business logics come under the model or the viewmodel?

If it is in the model how can we do that? Any example code please? I need full code for an example!

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

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

发布评论

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

评论(3

偏爱自由 2024-10-25 12:08:07

业务逻辑应该是模型的一部分,因为所有代码都应该可以在不同的技术平台上重用。例如,您可能希望在 ASP.NET MVC 中的网站和 WPF 中的桌面应用程序中重用模型,因此在视图模型中包含业务逻辑是没有意义的。

视图模型应包含特定于该特定应用程序实例的表示逻辑。

至于示例代码,这将特定于您正在建模的领域,因此您必须就您要建模的内容提出更具体的问题。

Business logic should be part of of your model, as all of that code should be reusable across different technology platforms. For example, you may want to reuse your models across a web site in ASP.NET MVC, and a desktop application in WPF, so it wouldn't make sense to have business logic in your view models.

View models should contain presentation logic which is specific to that particular application instance.

As for example code, this will be specific to the domain that you are modelling, so you would have to ask a more specific question about what you are trying to model.

烦人精 2024-10-25 12:08:07

我建议您阅读有关此主题的 PRISM 文档:第 5 章:实现 MVVM 模式

即使您不打算使用 PRISM,这篇文章也写得非常好,并用 Microsoft 的技术术语详细介绍了该模式。

I suggest you read PRISM documentation on this subject: Chapter 5: Implementing the MVVM Pattern.

Even if you don't plan to use PRISM, the article is very well written and details the pattern in Microsoft's technology terms.

逆流 2024-10-25 12:08:07

我不太相信微软关于模式的说法,因为他们的文章经常过时,或者修改以支持他们自己的专有框架或产品。此外,我还发现 MS 的想法/方法在他们的不同团队中以及在软件中普遍接受的方法中是矛盾的。

基本上,并不是所有的事情都是好的,也不是所有的事情都是坏的。

来回答你的问题。我会使用业务逻辑服务。我会在 ViewModel 中实例化对它的引用,并且不会在 ViewModel 或 Model 中放置与其角色无关的任何逻辑。也就是说,表示逻辑可以,具体取决于它是什么,它可能最好放置在 ViewModel 中,因为它与相关视图高度相关。模型也是如此。

记住这些角色,您将拥有一个可扩展且可测试的应用程序。

I would trust very little of what Microsoft has to say on Patterns as their articles are often dated, or modified to support their own proprietary frameworks or products. Also I've found MS thoughts/approaches contradictory within their different teams and also within the generally accepted approach in software.

Not all of it is good and not all of it is bad basically.

To answer your question. I'd use a Service for Business Logic. I'd instantiate a reference to it in the ViewModel and I would not put any logic in either the ViewModel or the Model that did not have anything to do with their role. ie Presentation logic is okay dependingon what it is, it might be best placed in the ViewModel as it is highly tied to the View in question. Same goes for the model.

Keeping in mind these roles you will have a scaleable and testable application.

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