如何设计Model-View-Controller中Model的界面?
我了解 MVC 模式的工作原理,但我一直有这个问题。如果您有一个大模型,其中许多函数委托给各个类,那么您是否必须定义一个大的整体接口,其中包含操作和查询模型的所有方法?
或者,该模型是否可以划分为许多模型,这些模型彼此通信,然后您可以使用各自的控制器来操纵它们?
谢谢
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
“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.
没有任何内容表明您的模型必须是单个类或实体。
例如,在 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.接口隔离是 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