理解MVC:“胖"是什么概念?在模特身上,“瘦"在控制器上? [英] Understanding MVC: Whats the concept of "Fat" on models, "Skinny" on controllers?

查看:22
本文介绍了理解MVC:“胖"是什么概念?在模特身上,“瘦"在控制器上?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图理解模型上的胖"与控制器上的瘦"的概念,并根据我一直在讨论的内容,我有以下示例(摘自 freenode 讨论):

I'm trying to understand the concept of "Fat" on models vs "skinny" on controllers and from what I've been discussing I have the following example (this is taken from a freenode discussion):

Q:在 MVC 范式上,它说的是胖模型,瘦控制器.我在这里想,如果我有很多方法(在控制器上)只使用一些抽象方法来 CRUD(在模型上),我是在创建一个胖控制器而不是模型吗?或者他们说,胖模型,在返回什么而不是输入什么?这是我从未理解的事情 =) 感谢任何评论!非常感谢

Q: On MVC paradigm, its said Fat models, skinny controllers. I'm here thinking, If I have lots of methods (on controller) that uses just a few abstract methods to CRUD (on model), am I creating a fat controller instead of a model ? Or they say, fat model, refearing in what is returned and not typed ? that's something I've never understood =) Any comments are appreciated! Thanks a lot

OBS1:我不是在做模型的事情,在控制器中,我只有控制模型的方法

OBS1: I'm not doing whats ment by the model, in the controller, I just have methods that control whats going to the model

OBS2:假设checkIfEmailExists()",有john@hotmail.com"作为参数.此方法将从查询此参数是否存在于表中的模型方法中获取返回值,返回布尔值.如果是0,checkIFemailExists()"会调用一个不同的模型方法,这个,他只是另一个抽象方法,执行更新操作.

OBS2: let's say "checkIfEmailExists()", has "john@hotmail.com", as a parameters. This method will, get the return from the model method that querys if this param exist in table, return boolean. If is 0, "checkIFemailExists()" will call a diferent model method, this one, he's just another abstract method, that performs Update operation.

OBS3:checkIfEmailExists()",不只是一个控制器吗?他实际上并没有执行任何 CRUD,他只是在比较值等.这让我感到困惑,因为在我看来,这是一个控制器:S

OBS3: The "checkIfEmailExists()", isnt just a controller ? He's not actually performing any CRUD, he's just comparing values etc. That's whats confusing me, because in my head this is a controller :S

注意:我想这不是最好的例子,因为说检查是否存在某些东西",听起来像是查询我的表操作

Notes: I guess this is not the best example, since saying "check if something exists",sounds like a query my table operation

Q2:还有一个问题,所以,假设我有一个视图表单,从那里发送电子邮件地址参数.你是说视图直接进入模型?

Q2:just one more question, so, let's say I've got a view form, from where that email address parameter is sent from. Are you saying the view goes directly to the model ?

Q3:控制器不应该在它们之间起作用吗?这就是范式

Q3:Shouldn't the controller act between them ? thats the paradigm

最后说明:讨论结束,说我错了,希望没问题(我正在学习).但是,那么,Q2 和 Q3 的正确答案是什么?

FINAL NOTE: The discussion ended, saying that I'm wrong, wish is ok (i'm learning). But, so, whats the right answers for Q2 and Q3 ?

感谢您的关注

推荐答案

您的应用程序是 M.它应该能够独立于 V 和 C.V 和 C 构成了您的应用程序的用户界面.无论是 Web 界面还是命令行界面,对于应用程序的核心业务逻辑的运行来说都无关紧要.您希望模型包含业务逻辑.

Your application is the M. It should be able to stand independent from V and C. V and C form the User Interface to your application. Whether this is a web interface or a command line interface shouldn't matter for the core business logic of your application to run. You want the model to be fat with business logic.

如果你有一个胖控制器,例如充满了业务逻辑,你没有坚持MVC的目的.控制器的唯一职责是处理 UI 请求并将其委托给模型.这就是为什么它应该是瘦的.它应该只包含它负责的工作所必需的代码.

If you have a fat controller instead, e.g. full with business logic, you are not adhering to the purpose of MVC. A controller's sole responsibility is handling and delegating UI requests to the Model. That's why it should be skinny. It should only contain code necessary for what it's responsible for.

public function fooAction()
{
    if(isset($_POST['bar'])) {
        $bar = Sanitizer::sanitize($_POST['bar']);
        $rows = $this->database->query('SELECT * from table');
        try {
            foreach($rows as $row) {
                $row->foo = $bar;
                $row->save();
            }
        } catch (Exception $e) {
            $this->render('errorPage');
            exit;
        }
        $this->render('successPage');
    } else {
        $this->render('fooPage');
    }
}

什么时候该

public function fooAction()
{
    if(isset($_POST['bar'])) {
        $success = $this->tableGateway->updateFoo($_GET['bar']);
        $page    = $success ? 'successPage' : 'errorPage';
        $this->render($page);
    } else {
        $this->render('fooPage');
    }
}

因为这就是控制器需要知道的全部内容.它不应该更新行.它应该只是告诉模型有人要求进行此更改.更新是管理行的类的责任.此外,控制器不一定要清理该值.

because that's all the controller needs to know. It should not update the rows. It should just tell the model that someone requested this change. Updating is the responsibility of the class managing the rows. Also, the controller does not necessarily have to sanitize the value.

至于 Q2 和 Q3,请看我对 我可以从视图中调用模型吗.

As for Q2 and Q3, please see my answer to Can I call a Model from the View.

这篇关于理解MVC:“胖"是什么概念?在模特身上,“瘦"在控制器上?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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