dongting3135 2015-10-09 21:39
浏览 41

向控制器注入数据与使用模型注入控制器相比,是否有具体的好处?

My goal of asking this question is to ferret out whether there are benefits to injecting Controller directly with the data it needs (more specific approach) opposed to injecting a Model into a Controller (more generic approach). Or to establish whether or not it is just a matter of preference.

Injecting Controller with Model:

Model can be used to run all kinds of queries to retrieve various bits of data, but it is a heavier-weight construct than the data itself. Model essentially contains data, or at least it can access all the data you may need. Example:

class CategoryControllerWithModel
{
    private $model;

    public function __construct($model)
    {
        $this->model = $model;
    }

    // generates HTML for input form
    public function genHtml()
    {
        /* retrieve data */
        $categories = $this->model->getCategories();

        //...
    }
}

//instantiation within Factory Method:
class Factory
{
    $model = new CategoryModel();
    $controller = new CategoryControllerWithModel($model);
    return $controller;
}

Injecting Controller with Data:

Here we do a bit more upfront with in the Factory method but we get a leaner Controller that only receives exactly the data it needs and is so completely separated from the Model that it is not even aware of its existence.

class CategoryControllerWithData
{
    private $categories;

    public function __construct($categories)
    {
        $this->categories = $categories;
    }

    public function genHtml()
    {
        $categories = $this->categories;
    }
}

//instantiation within Factory Method:
class Factory
{
    $model = new CategoryModel();

    //a bit more work to get the data Controller needs
    //benefit:  Controller not tied to the Model
    $categories = $model->getCategories():

    $controller = new CategoryControllerWithData($categories);
    return $controller;
}

Question:

I suppose MVC stands for exactly that -- Model, View, Controller, so injecting Model is probably considered to be an "okay" thing to do. If so, am I taking this too far by trying to remove Controller dependency on Model?

Suppose I insist that I want to inject Data into my Controllers rather than the Model. Is this a purely preferential issue do you see any concrete benefits of doing so?

  • 写回答

2条回答 默认 最新

  • dsfsd43523 2015-10-09 21:50
    关注

    From my point of view, Factory shouldn't be responsible for domain logic. It should only be responsible for building things up.

    In this case, where you are injecting data, Factory has to know what categories controller is searching for, are there any filtering and so on.

    So I think for controller you should only inject model, keep Factory single responsibility only for building things and controller should be responsible for it's data.

    评论

报告相同问题?

悬赏问题

  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来