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 仓库
基础教程推荐
- XAMPP 服务器不加载 CSS 文件 2022-01-01
- 在 PHP 中强制下载文件 - 在 Joomla 框架内 2022-01-01
- 如何在 PHP 中的请求之间持久化对象 2022-01-01
- Libpuzzle 索引数百万张图片? 2022-01-01
- 在多维数组中查找最大值 2021-01-01
- 通过 PHP SoapClient 请求发送原始 XML 2021-01-01
- 在 Woocommerce 中根据运输方式和付款方式添加费用 2021-01-01
- mysqli_insert_id 是否有可能在高流量应用程序中返回 2021-01-01
- 超薄框架REST服务两次获得输出 2022-01-01
- WooCommerce 中选定产品类别的自定义产品价格后缀 2021-01-01