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 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化