1

我正在尝试在一个小型学校项目中实现 MVC,但我有一些小疑问,希望有人可以帮助我:

1.- 如果我有 5 个视图,我想我需要 5 个模型,每个视图一个,但我需要 5 个控制器吗?还是只需要一个控制器来与所有这些交互。

2.-我需要从数据库中添加数据,在哪里进行选择查询,在模型中还是在控制器中?

4

4 回答 4

2

1.- if I'm having 5 views, I think I need 5 models, one for each view, but do I need 5 controllers?, or just one controller that will interact with all of them.

No, that's the beauty of MVC. One model can be used by many different views. The model should be written so that it has no knowledge of the views and no need for this knowledge. You may need 5 controllers, but that's not a given. It's possible (though unlikely) that you will only need one control.

Also note that you may in fact need several models if you require several different data sources and "business rules" for the program.

2.-I need to add data from a DB, where do I make the select query, in the model, or in the controller?

That sounds like a control issue.

Per comments, note MVC was not created as an answer to limited resources, but as a way to allow for the creation of modular program code to allow for the reduction of coupling and an increase in cohesion. This should make it much easier to upgrade, enhance and debug code.

于 2013-07-07T17:59:05.677 回答
2
  1. 这取决于这 5 个视图代表什么领域概念。如果它们代表关于同一领域概念的不同视图,例如学生列表、学生详细信息等,那么您将拥有 1 个模型和 5 个视图。每个域实体有一个控制器也是一个好主意。

  2. 您将使用称为 DAO(数据访问对象)的专用类与数据库交互,DAO 的方法通常是 CRUD 操作(创建、读取、更新和删除)。您将从控制器调用 DAO。

于 2013-07-07T18:02:46.200 回答
1
  1. Models, views, and controllers need not map to one another 1:1.
  2. I would normally consider this to be controller code, but it may somewhat depend on your particular situation.
于 2013-07-07T17:59:12.150 回答
0

1)取决于你想要做什么。理想情况下,视图和控制器之间应该存在一对一的映射。但是,您可以只为多个模型创建一个控制器并尝试使用视图 ID 来操作视图。

2)选择查询通常应该在控制器中

于 2013-07-07T18:02:24.630 回答