laravel 当服务类可以做同样的事情时,存储库的目的是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39945840/
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 14:35:00  来源:igfitidea点击:

What is the purpose of repository when service classes can do the same?

phplaravellaravel-5.2repository-pattern

提问by I'll-Be-Back

Normally I put the logic in the service classes without using repository, for example, something like this:

通常我将逻辑放在服务类中而不使用存储库,例如,像这样:

namespace App\ProjectName\Profile;

use App\User;

class AccountService
{
    private $userModel;

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

    public function detail()
    {
        $user = \Auth::User();

        return [
            'id'    => $user->id,
            'name'  => $user->name,
            'email' => $user->email,
            'keys'  => $user->keys,
        ];
    }

    public function addKey($name, $key)
    {
        return $this->userModel->keys()->create([
            'name' => $name,
            'key'  => $key
        ]);
    }
}

I have seen some example out there, refactored even further by creating repository classes. something like UserControllerinputs data, sends it to UserCreatorService, which gets the UserRepositorywhich in turn gets the UserModel. It seem UserCreatorServiceis a repeat of UserRepository?

我看过一些例子,通过创建存储库类进一步重构。诸如UserController输入数据之类的东西,将其发送到UserCreatorService,后者得到 ,UserRepository而后者又得到UserModel。好像UserCreatorService是重蹈覆辙UserRepository

回答by pid

From DDD (Domain Driven Design) the responsibility of a repository is to take care of loading, storing, modifying and deleting an entity on the designated data storage (which may or may not be a database -- it may even be a remote server or just a file).

从 DDD(域驱动设计)来看,存储库的职责是负责加载、存储、修改和删除指定数据存储(可能是也可能不是数据库——它甚至可能是远程服务器或只是一个文件)。

A service, on the other hand, has (or should have) a very narrow responsibility of performing some useful activity. Each service is instantiated separately and then injected into code in the application layer or above, which acts as a bridge (Bridge pattern). This approach has proven to be very advantageous because it allows to manage the dependencies between otherwise unrelated (uncoupled) code.

另一方面,服务具有(或应该具有)执行某些有用活动的非常狭窄的职责。每个服务单独实例化,然后注入到应用层或以上的代码中,充当桥梁(Bridge pattern)。这种方法已被证明是非常有利的,因为它允许管理其他不相关(解耦)代码之间的依赖关系。

Those two definitions and the origin of the concepts shows that they actually are very different things. By pure chanceyou noticed that a repository and a service have an apparent overlap, but that's due to implementation details or plain misuse. Their responsibilities may under circumstances go hand in hand (giving rise to a collaboration) but they really are orthogonal concepts.

这两个定义和概念的起源表明它们实际上是非常不同的东西。通过偶然的机会,你注意到一个存储库和服务有一个明显的重叠,但这是由于实施细则或纯滥用。在某些情况下,他们的职责可能齐头并进(导致合作),但他们确实是正交的概念。

Furthermore, Repositories should arise from a deep layer (Persistance or DAL, Data Access Layer). Services, on the other hand, often are vertical cross-cutting or arise on the application layer.

此外,存储库应该来自深层(持久性或 DAL,数据访问层)。另一方面,服务通常是垂直交叉的或出现在应用程序层。

Through proper layering the differences between repositories and services become even more apparent.

通过适当的分层,存储库和服务之间的差异变得更加明显。

Do not think about them as pure code artifacts you can move around. They are well-defined concepts useful to understand about and design the structure of a system. They decline into actual code only as a consequence of that design.

不要将它们视为可以随意移动的纯代码工件。它们是定义明确的概念,有助于理解和设计系统结构。它们仅作为该设计的结果才进入实际代码。

I hope I succeeded in writing something that clears up some ideas and is not confusing.

我希望我能成功地写出一些清除一些想法并且不会混淆的东西。

回答by Luís Cruz

There is no definitiveanswer for your question since the patterns you use highly depend on the project's complexity and needs.

您的问题没有明确的答案,因为您使用的模式在很大程度上取决于项目的复杂性和需求。

However, a Service and a Repository are two different things. The Repository is a common wrapper for the model and is where you write the queries to the database. IMO you shouldn't add logic here and the sole purpose of a repository is to grab os store data into the database. The advantage of Repositories is the "easiness" to switch to other database systems.

但是,服务和存储库是两种不同的东西。Repository 是模型的通用包装器,也是您将查询写入数据库的地方。IMO 你不应该在这里添加逻辑,存储库的唯一目的是将 os store 数据抓取到数据库中。Repositories 的优点是“容易”切换到其他数据库系统。

A Service, IMO, is where you add all the application's logic.

服务,IMO,是您添加所有应用程序逻辑的地方。

For additional information refer to this answer.

有关其他信息,请参阅此答案