Laravel 存储库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17367159/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 08:02:52  来源:igfitidea点击:

Laravel Repositories

phplaravellaravel-4

提问by AndrewMcLagan

What are the advantages of Repositories in Laravel? It seems to be abstracting the Model layer from the business logic of the application. Although it really just seems to make the whole request life cycle just that much more complicated for little gain.

Laravel 中存储库的优点是什么?它似乎是从应用程序的业务逻辑中抽象出模型层。尽管它似乎真的只是让整个请求生命周期变得更加复杂,但收益甚微。

Can someone shed light on the advantage of Laravel repositories?

有人可以阐明 Laravel 存储库的优势吗?



Edit

编辑

After now using repositories for some time I would add the following:

现在使用存储库一段时间后,我会添加以下内容:

  • Repositories enforce single responsibility
  • Repositories should only return one collection of entities
  • Although separate from dependancy injection the concepts are brothers
  • Storage abstraction for the actual storage implementation (e.g. MySQL)
  • Easier testing
  • 存储库强制执行单一职责
  • 存储库应该只返回一个实体集合
  • 虽然与依赖注入不同,但概念是兄弟
  • 实际存储实现的存储抽象(例如 MySQL)
  • 更容易测试

回答by kfriend

Repositories, like in the provided tutorial, aren't necessary a Laravel concept. Rather, they're a form of IoC injection that is possible with Laravel. Any object that might similarly be injected doesn't mean it's a repository. See the video for a good example from Taylor Otwell, which happens to use a "repository" as well: http://vimeo.com/53029232.

存储库,就像在提供的教程中一样,不是 Laravel 概念所必需的。相反,它们是 Laravel 可以实现的一种 IoC 注入形式。任何可能被类似注入的对象并不意味着它是一个存储库。请参阅视频以获取 Taylor Otwell 的一个很好的示例,该示例恰好也使用了“存储库”:http: //vimeo.com/53029232

In this example, the repository abstracts where the data came from that is passed to the controller. As long as the data passed implements the specified interface, the controller can "blissfully" make use of the interface's defined methods without worry about where the data initially came from. This allows switching the initial source of the data without breaking your controller. You could pull the data from a file, a database, an outside API, a mock object, or just some arbitrary array. Basically, the controller doesn't need to gather the data represented by the repository. It can just receive and use.

在这个例子中,存储库抽象了数据的来源,并将其传递给控制器​​。只要传递的数据实现了指定的接口,控制器就可以“幸福地”使用接口定义的方法,而不必担心数据最初来自哪里。这允许在不破坏控制器的情况下切换数据的初始源。您可以从文件、数据库、外部 API、模拟对象或一些任意数组中提取数据。基本上,控制器不需要收集存储库表示的数据。它可以只是接收和使用。

回答by James Flight

In addition to the other answers here, it's worth pointing out that repositories used when used in Laravel can add an extra level of expressiveness. Take for example the following:

除了这里的其他答案之外,值得指出的是,在 Laravel 中使用的存储库可以增加额外的表现力。以以下为例:

$users = User::whereHas("role", function($q) {
    $q->where('name', 'moderator');
}, '<', 1)->get();

The code is difficult to read and awkward to look at. It can be encapsulated in a repository method, and demonstrate much clearer code intent:

该代码难以阅读且难以查看。它可以封装在一个存储库方法中,并展示更清晰的代码意图:

$users = $userRepository->getUsersWhoAreNotModerators();

This is also achievable using eloquent 'query scopes' but I think using a repository is superior, as is adheres much better to the single responsibility principal, and is doable regardless of whether you are using Eloquent.

这也可以使用 eloquent 的“查询范围”来实现,但我认为使用存储库更好,因为它更好地遵守单一职责原则,并且无论您是否使用 Eloquent 都是可行的。

回答by Ashwini G.

Repositories helps to keep your controller clean and make reusable code. functions in repositories can be accessed in one or more controllers, repositories etc. Also all your backend related logic (like fetching data from database or external calls) can be added in repositories to make logical separation.

存储库有助于保持控制器清洁并制作可重用的代码。存储库中的功能可以在一个或多个控制器、存储库等中访问。此外,所有后端相关逻辑(如从数据库或外部调用获取数据)都可以添加到存储库中以进行逻辑分离。

One of the main use of repositories is to create different binding (using interfaces to define your functions and with the help of app.bind bind different implementation of the function as per need). for example: two separate repositories (implementing the parent repository/ interface ) handling database and files for backend data.

存储库的主要用途之一是创建不同的绑定(使用接口来定义你的函数,并在 app.bind 的帮助下根据需要绑定不同的函数实现)。例如:两个独立的存储库(实现父存储库/接口)处理后端数据的数据库和文件。

Hope this will help. Thanks!

希望这会有所帮助。谢谢!

回答by Alexandru Muresan

The main reason you use repository pattern is to be able to easily change your data source. For example, in my experience the most common change is adding a caching layer, because the repository implements an interface all you need to do is build the new object implementing the same interface with the new methods handling cache and change the binding.

您使用存储库模式的主要原因是能够轻松更改您的数据源。例如,根据我的经验,最常见的更改是添加缓存层,因为存储库实现了一个接口,您需要做的就是使用处理缓存的新方法构建实现相同接口的新对象并更改绑定。