首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >来自扩展类的Symfony设置依赖注入

来自扩展类的Symfony设置依赖注入
EN

Stack Overflow用户
提问于 2018-06-09 19:13:05
回答 1查看 875关注 0票数 1

我想像这样注入到抽象类中:

代码语言:javascript
复制
services:
    App\Infrastructure\Persistence\BaseDoctrineRepository:
        arguments:
          $eventStore: '@broadway.event_store'
          $registry: '@doctrine'
          $eventBus: '@broadway.event_handling.event_bus'

,但如果这样做,我会得到:

代码语言:javascript
复制
Cannot autowire service "App\Infrastructure\Persistence\User\DoctrineUserRepository": argument "$eventStore" of method "__construct()" references interface "Broadway\EventStore\EventStore" but no such service exists. You should maybe alias this interface to one of these existing services: "broadway.event_store.dbal", "broadway.event_store.in_memory". 

因此,我需要为每个这样的存储库复制代码,我希望避免这种情况。

代码语言:javascript
复制
services:
    App\Infrastructure\Persistence\User\DoctrineUserRepository:
        arguments:
          $eventStore: '@broadway.event_store'
          $registry: '@doctrine'
          $eventBus: '@broadway.event_handling.event_bus'

抽象类:

代码语言:javascript
复制
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;

abstract class BaseDoctrineRepository extends ServiceEntityRepository
{
    public function __construct(ManagerRegistry $registry, EventStore $eventStore, EventBus $eventBus)
    {
        $this->eventStore = $eventStore;
        $this->eventBus = $eventBus;
        parent::__construct($registry, static::REPOSITORY_CLASS);
    }

从abastract扩展的类(我想避免使用构造函数):

代码语言:javascript
复制
class DoctrineUserRepository extends BaseDoctrineRepository implements UserRepository
{
    const REPOSITORY_CLASS = User::class;

    public function __construct(ManagerRegistry $registry, EventStore $eventStore, EventBus $eventBus)
    {
        parent::__construct($registry, $eventStore, $eventBus);
    }
EN

回答 1

Stack Overflow用户

发布于 2018-06-10 02:52:55

你试过这个https://symfony.com/doc/current/service_container/parent_services.html吗?

所以基本上

代码语言:javascript
复制
services:
    App\Infrastructure\Persistence\BaseDoctrineRepository:
            abstract: true
            arguments:
              $eventStore: '@broadway.event_store'
              $registry: '@doctrine'
              $eventBus: '@broadway.event_handling.event_bus'
    App\Infrastructure\Persistence\User\DoctrineUserRepository:
        parent: App\Infrastructure\Persistence\BaseDoctrineRepository
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50773651

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档