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

查看:63
本文介绍了在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:

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

要执行上述操作,每一层都应该不了解其他各层,以便正常工作.例如,视图应该可以接收其数据,而无需知道有关其来源的任何信息即可正确显示它. controller 不需要与模型的基础结构有关的任何知识即可与之交互. 模型应该不知道如何显示数据(例如格式)或工作流.

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."

否.该模型永远不要格式化数据.它还不应读取格式化的数据.那会污染模型,并进入business logic = display logic的地狱级别.

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天全站免登陆