在 MVC 应用程序中,控制器或模型应该处理数据访问吗? [英] In a MVC application, should the controller or the model handle data access?

查看:21
本文介绍了在 MVC 应用程序中,控制器或模型应该处理数据访问吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们公司正在进行一些哲学辩论,讨论应该在何处调用业务逻辑以执行 CRUD 操作.

We are having some philosopical debates in our company about where the calls to the Business Logic should be to perform CRUD operations.

我认为模型应该由您的数据结构组成,并且控制器应该负责填充数据.

I believe that the Model should consist of your data structure and that the controller should be responsible for populating the data.

我的同事认为所有填充都应该在模型类本身中完成,并且只需由控制器调用.这使控制器保持整洁(但在我看来,会使模型变得混乱).

My co-worker believes that all population should be done in the model class itself, and simply called by the controller. This keeps the controller neat and clean (but, in my opinion, clutters up the model).

他还认为任何返回 Json 对象的调用都应该发生在模型中,而不是在控制器中.模型会向控制器返回一个数组,控制器随后会将其作为 Json 对象返回.

He also believes that any call that returns a Json object should happen in the model, not in the controller. The model would return an array to the controller, which would then return this as a Json object.

每种方法有哪些不同的优点/缺点,是否有正确或错误的方法来做到这一点?

What are some different pros/cons to each and is there a right or wrong way to do this?

推荐答案

所有业务逻辑都应该在 MODEL 中.

All business logic should be in the MODEL.

记住,每一层的职责是这样的:

Remember, the responsibilities of each layer are thus:

  • 控制器 - 模型和视图之间的桥梁.决定下一步去哪里.
  • 查看 - 显示数据,收集用户输入
  • 模型 - 业务逻辑、数据存储接口.

最大的收获之一是维护和(后来的)扩展.一般来说:

One of the biggest gains is in maintenance and (later) expansion. In general:

  • 如果您需要更改业务逻辑,则不需要修改控制器或视图.
  • 如果您更改视觉显示,则无需修改模型或控制器.
  • 如果您更改工作流程,则无需修改视图或模型.

要做到以上几点,每一层都应该不知道其他层才能正常工作.例如,view 应该接收它的数据并且不需要知道它来自哪里才能正确显示它.控制器不需要知道模型的底层结构就可以与之交互.模型不应该知道如何显示数据(例如,格式化)或工作流.

To do the above, each layer should have no knowledge of the others in order to work properly. For example, the view should receive its data and not need to know anything about where it comes from in order to display it properly. The controller should not need to know anything about the underlying structure of the model in order to interact with it. The model should have no knowledge of how the data is to be displayed (e.g., formatting) or the workflow.

他还认为,任何返回 Json 对象的调用都应该发生在模型中,而不是在控制器中.模型会向控制器返回一个数组,然后控制器会将其作为 Json 对象返回."

"He also believes that any call that returns a Json object should happen in the model, not in the controller. The model would return an array to the controller, which would then return this as a Json object."

没有.模型永远不应该格式化数据.它也不应该读取格式化数据.这会污染模型并进入业务逻辑=显示逻辑的地狱级别.

NO. The Model should never format data. It also should not read formatted data. That is polluting the model and moving into the level of hell where business logic = display logic.

JSON(进来或出去)只是另一种观点.所以出门:

JSON (coming in or going out) is just another view. So going out:

Data Store -> Model -> Controller -> View

进来:

View -> Controller -> Model -> Data Store

这篇关于在 MVC 应用程序中,控制器或模型应该处理数据访问吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆