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

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

问题描述

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

问:在MVC范例中,其所说的Fat模型是瘦控制器.我在这里思考的是,如果我有很多方法(在控制器上)仅对CRUD使用了一些抽象方法(在模型上),那么我是在创建胖控制器而不是模型吗?还是他们说,胖模子,退回了什么却没有输入?这是我从未理解过的东西=)任何评论表示赞赏!非常感谢

OBS1:我没有执行模型的操作,在控制器中,我只是拥有控制模型操作的方法

OBS2:假设"checkIfEmailExists()"具有"john@hotmail.com"作为参数.该方法将从查询该参数是否存在于表中的模型方法的返回值中返回boolean.如果为0,则"checkIFemailExists()"将调用不同的模型方法,该方法只是另一个执行Update操作的抽象方法.

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

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

Q2:还有一个问题,所以,我有一个查看表单,从该表单发送电子邮件地址参数.您是说视图直接进入模型吗?

Q3:控制器在它们之间不动作吗?那就是范式

最后的提示:讨论结束,说我错了,愿望还可以(我正在学习).但是,那么,第二季度和第三季度的正确答案是什么?

感谢您的关注

解决方案

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

如果您有胖控制器,例如充满业务逻辑,您就没有遵循MVC的目的.控制器的唯一责任是处理UI请求并将其委托给模型.这就是为什么它应该很瘦.它应该只包含负责其工作的代码.

简化示例

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');
    }
}

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

关于第二季度和第三季度,请参阅我对

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: I'm not doing whats ment by the model, in the controller, I just have methods that control whats going to the model

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: 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: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:Shouldn't the controller act between them ? thats the paradigm

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 ?

Thanks for your atention

解决方案

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.

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.

Simplified Example

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');
    }
}

When it should be

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.

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

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

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