如何设计Model-View-Controller中Model的界面?

发布于 2024-12-08 22:56:37 字数 157 浏览 2 评论 0 原文

我了解 MVC 模式的工作原理,但我一直有这个问题。如果您有一个大模型,其中许多函数委托给各个类,那么您是否必须定义一个大的整体接口,其中包含操作和查询模型的所有方法?

或者,该模型是否可以划分为许多模型,这些模型彼此通信,然后您可以使用各自的控制器来操纵它们?

谢谢

I understand how the MVC pattern works, but I've always had this question. If you have a big Model with many functions delegated to various classes, do you have to define a big monolithic interface with all the methods to manipulate and query the Model?

Or, can that model be partitioned to many models, that talk to each other, and then you can manipulate them with their respective controllers?

Thanks

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

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

发布评论

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

评论(3

土豪 2024-12-15 22:56:37

“ViewModel”概念在这里可能会有所帮助 - Phil Haack 在“专业 ASP.NET MVC3”。

许多自动工具或“脚手架”生成器都希望创建单个页面/界面,但没有什么可以阻止您为大型模型进行多阶段过程。

一种选择是为流程的每个阶段创建视图模型(即 BigProcessPartAViewModel、BigProcessPartBViewModel 等),然后生成一个控制器,用单独的视图处理每个阶段。显然,您需要跨多个阶段管理状态,可能需要使用数据库或会话。

此外,您的模型就是您的模型......它不是数据访问。因此,您可能需要一个额外的模型来处理进程内状态以及允许多阶段事务的数据访问设计。

The "ViewModel" concept may be helpful here -- it's also referred to as "view specific model" by Phil Haack in the book "Professional ASP.NET MVC3".

A lot of auto-tooling or "scaffolding" generators look to create a single page/interface but there is nothing preventing you from making a multi-stage process for a large model.

One option would be to create View Models for each stage of the process (ie. BigProcessPartAViewModel, BigProcessPartBViewModel, etc) and then generate a controller that process each of these with separate views. Obviously you'll need to manage state across multiple stages, perhaps with a database or session.

Additionally, your model is your model... it is NOT data access. So you may need to have an additional model that handles in-process state as well as a data access design that allows for a multi-phase transaction.

撩发小公举 2024-12-15 22:56:37

没有任何内容表明您的模型必须是单个类或实体。

例如,在 Spring MVC 中,模型基本上是您在控制器中构造的带有键和值的 Map

There is nothing that says that your Model needs to be a single class or entity.

For example, in Spring MVC, the model is basically a Map that you construct in the Controller, with keys and values.

小…红帽 2024-12-15 22:56:37

接口隔离是 SOLID 之一,它代表对象的 5 个基本原则面向编程和设计。它表示,如果您的界面变得太“胖”,则需要将其拆分为更小、更具体的界面。有关接口隔离 wiki 页面的更多详细信息

Interface segregation is one of the SOLID which stands for 5 basic principles of object-oriented programming and design. It says that if your interface becomes too 'fat' it needs to be split into smaller and more specific interfaces. More details on Interface segregation wiki page

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