Laravel Repositories(Laravel 仓库)
问题描述
Laravel 中的存储库有什么优势?它似乎是从应用程序的业务逻辑中抽象出模型层.尽管它实际上似乎只是让整个请求生命周期变得更加复杂,但收效甚微.
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?
现在使用存储库一段时间后,我将添加以下内容:
After now using repositories for some time I would add the following:
- 存储库执行单一职责
- 存储库应该只返回一个实体集合
- 虽然与依赖注入不同,但概念是兄弟
- 实际存储实现的存储抽象(例如 MySQL)
- 更轻松的测试
推荐答案
存储库,就像在提供的教程中一样,不是 Laravel 概念所必需的.相反,它们是 Laravel 可以实现的一种 IoC 注入形式.任何可能同样被注入的对象并不意味着它是一个存储库.有关 Taylor Otwell 的一个很好的示例,请参阅视频,它恰好也使用了存储库":http://vimeo.com/53029232.
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.
在此示例中,存储库抽象出数据的来源,并将其传递给控制器.只要传递的数据实现了指定的接口,控制器就可以愉快地"使用接口定义的方法,而不必担心数据最初来自哪里.这允许在不破坏控制器的情况下切换数据的初始源.您可以从文件、数据库、外部 API、模拟对象或任意数组中提取数据.基本上,控制器不需要收集存储库表示的数据.它可以接收和使用.
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.
这篇关于Laravel 仓库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Laravel 仓库
- 带有通配符的 Laravel 验证器 2021-01-01
- Laravel 仓库 2022-01-01
- 正确分离 PHP 中的逻辑/样式 2021-01-01
- PHP Count 布尔数组中真值的数量 2021-01-01
- 如何定位 php.ini 文件 (xampp) 2022-01-01
- 没有作曲家的 PSR4 自动加载 2022-01-01
- SoapClient 设置自定义 HTTP Header 2021-01-01
- Mod使用GET变量将子域重写为PHP 2021-01-01
- 从 PHP 中的输入表单获取日期 2022-01-01
- Oracle 即时客户端 DYLD_LIBRARY_PATH 错误 2022-01-01