laravel中存储库模式相对于实例化控制器构造函数中的模型的优势 [英] advantage of repository pattern in laravel against instantiating a model in a controller constructor

查看:47
本文介绍了laravel中存储库模式相对于实例化控制器构造函数中的模型的优势的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们举两个例子.

示例1 (存储库模式)

接口

interface FooInterface {
    public function all();
}

模型(宽松使用)

class FooModel implements FooInterface {
    public function all()
    {
        return DB::('sometable')->get();
    }
}

服务提供商

  class FooServiceProvider extends ServiceProvider {

      public function register()
      {
        $this->app->bind(
          'Foo\FooInterface',
          'Foo\FooModel'
      );
   }

config/app.php

'providers' => array(
  // --
  'Foo\FooServiceProvider'
),

最后是控制器 :

use Foo\FooInterface as Model;

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

现在,我可以按$this->model->all()的方式访问方法.那太棒了!让我们看第二个例子.

示例2 :

Now i can access the methods as $this->model->all(). that's great! Let's look at the 2nd example.

Example 2:

控制器:

public function __construct()
{
    $this->model = new \Foo\FooModel();
}

现在我也可以使用与$this->model->all();



问题



Question

如我所读,将来使用存储库模式的优点是可以轻松配置/更改接口系统.例如

As i read, the advantage of using repository pattern is, in future, easily configurable/changeable interface system. e.g.

如果我更改数据库系统,只需更改服务提供商中的绑定.

if i change the db system, i just need to change, the bindings in the service provider.

但是,我也可以轻松地更改 controller构造中的 model 实例化以实现相同目的.例如,将$this->model = new \Foo\FooModel()更改为$this->model = new \Bar\BarModel();,其中BarModel将保存不同系统的方法.

But, i can also just easily change the model instaintiation in the controller construct to achieve the same. like, changing $this->model = new \Foo\FooModel() to $this->model = new \Bar\BarModel(); where BarModel will hold the methods of different system.

存储库模式的优势方面,我到底缺少什么?还是在这种特殊情况下,存储库模式没有提供太多优势,而在其他情况下却可以呢?如果是的话,那会是什么情况?

What exactly i am missing here in the aspect of advantages of a repository pattern.? or in this particular case, repository pattern doesn't give much advantage yet, in some other case it may? if that's a yes, what can be that situation?

p.s.术语 model 仅用于方便起见.

p.s. the term model is used just for the convenince.

推荐答案

这实际上取决于您如何设置代码.在您的特定情况下,似乎没有太大好处.

It really comes down to how you have set-up your code. In your particular case, there wouldn't appear to be much benefit.

但是,如果您的代码要求您在许多不同的控制器上具有多个模型实例化,该怎么办?例如,也许您有一个用户存储库模型.可能有很多控制器需要获取有关用户的信息.

But what if your code required you to have multiple instantiations of your model across many different controllers? For example, maybe you have a model to your user repository. There may be many controllers which need to get information about the user.

然后遍历所有控制器更改所有引用(即您的示例2)会很麻烦.最好只更改一次存储库(即您的示例1).

Then it would be a hassle to go through all your controllers changing all the references (i.e. your example 2). Much better to just change the repository once (i.e. your example 1).

在编码中从来没有一种适合所有大小.您所能做的最好的事情就是为您现在需要的代码编写代码,并意识到可能在将来有助于灵活性的任何潜在解决方案.我的观点是,存储库模式是有助于灵活性的解决方案之一.您可能永远不需要更改模型或移动到其他数据库,但是与如果您曾经想要更改数据库的麻烦相比,现在使用它进行编码的工作量很小.

There is never one size fits all in coding. The best you can do is code for what you need now, with an awareness of any potential solutions which may aid flexibility in the future. My view is that the repository pattern is one of those solutions which aids flexibility. You may never need to change the model, or move to a different database, but the effort in coding with it now is minimal compared to the hassle you will have if you ever did want to change your db.

这篇关于laravel中存储库模式相对于实例化控制器构造函数中的模型的优势的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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