douke1954 2018-06-09 11:13
浏览 80
已采纳

Symfony从扩展类设置依赖注入

I would like to inject to the abstract class like this:

services:
    App\Infrastructure\Persistence\BaseDoctrineRepository:
        arguments:
          $eventStore: '@broadway.event_store'
          $registry: '@doctrine'
          $eventBus: '@broadway.event_handling.event_bus'

, but if do that then I get:

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". 

So I need to duplicate code for every repository like this and I would like to avoid it.

services:
    App\Infrastructure\Persistence\User\DoctrineUserRepository:
        arguments:
          $eventStore: '@broadway.event_store'
          $registry: '@doctrine'
          $eventBus: '@broadway.event_handling.event_bus'

Abstract class:

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);
    }

Class that extends from the abastract (I would like to avoid the constructor):

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);
    }
  • 写回答

2条回答 默认 最新

  • dongxian2863 2018-06-09 20:29
    关注

    Following the note indication

    If you have a _defaults section in your file, all child services are required to explicitly override those values to avoid ambiguity. You will see a clear error message about this.

    of @brucie-alpha link reference, I could manage common dependencies with parent Services. Here is the solution that worked for me since I'm using _defaults section in my services.yaml file

    App\Infrastructure\Persistence\BaseDoctrineRepository:
        abstract: true
        public: false
        autowire: false
        autoconfigure: false
        arguments:
          $eventStore: '@broadway.event_store'
          $registry: '@doctrine'
          $eventBus: '@broadway.event_handling.event_bus'
    
    App\Infrastructure\Persistence\User\DoctrineUserRepository:
        parent: 'App\Infrastructure\Persistence\BaseDoctrineRepository'
        public: true
        autowire: false
        autoconfigure: false
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程
  • ¥20 模型在y分布之外的数据上预测能力不好如何解决