Circular dependency - Injecting objects that are directly depended on each other

这一生的挚爱 提交于 2019-12-30 08:14:41

问题


I have used Dice PHP DI container for quite a while and it seems the best in terms of simplicity of injecting dependencies.

From Dice Documentation:

class A {
    public $b;

    public function __construct(B $b) {
        $this->b = $b;
    }
}

class B {

}

$dice = new \Dice\Dice;    
$a = $dice->create('A');
var_dump($a->b); //B object

However, when you have to use objects that are directly dependent on each other, the finall result is server error, because of the infinite loop.

Example:

class A {
    public $b;

    public function __construct(B $b) {
        $this->b = $b;
    }
}

class B {
    public $a;

    public function __construct(A $a) {
        $this->a = $a;
    }
}

Author of Dice says that there is no way to construct an object from the A or B classes. As:

  • An 'A' object requires a 'B' object to exist before it can be created
  • But a 'B' object requires an 'A' object to exist before it can be created

Author says, that this limitation concerns all DI containers!


Question:

What would be the best solution for overcoming this problem nicely without changing initial code? Could anyone provide an example of using other DI containers, when it would be possible to run exampled code without bulky workarounds?


回答1:


As mentioned on your post on the Dice github ( https://github.com/TomBZombie/Dice/issues/7 ), the only way to resolve without removing the circular dependency is to refactor one of the classes to use setter injection:

class A {
    public $b;

    public function __construct(B $b) {
        $this->b = $b;
    }
}


class B {
    public $a;

    public function setA(A $a) {
        $this->a = $a;
    }
}

This allows the objects to be constructed:

$b = new B();
$a = new A($b);
$b->setA($a);

With the original code:

class A {
    public $b;

    public function __construct(B $b) {
        $this->b = $b;
    }
}

class B {
    public $a;

    public function __construct(A $a) {
        $this->a = $a;
    }
}

You cannot construct it and run into the same problem as the container:

$b = new B(new A(new B(new A(new B(.............))))

The problem with having a container work around this issue using a hack such as ReflectionClass::newInstanceWithoutConstructor is that your objects are now dependent on creation logic which uses this method. You essentially couple the code to the container which is a poor design as your code is now no longer portable and cannot be used without the container to perform the object construction.




回答2:


You have a circular dependency, which is very hard to solve. The first thing to do is to try to get rid of this circular dependency by refactoring your classes and how they interact.

If you really can't manage to do it, there are solutions. I'll copy-paste my answer from Self-referencing models cause Maximum function nesting level of x in Laravel 4:

  • Setter injection

Rather than injecting a dependency in the constructor, you can have it injected in a setter, which would be called after the object is constructed. In pseudo-code, that would look like that:

$userRepo = new UserRepository();
$cartRepo = new CartRepository($userRepo);
$userRepo->setCartRepo($userRepo);
  • Lazy injection

I don't know if Dice does support lazy injection, but that's also a solution: the container will inject a proxy object instead of the actual dependency. That proxy-object will load the dependency only when it is accessed, thus removing the need to build the dependency when the constructor is called.

Here is an explanation on how lazy injection works if you are interested: http://php-di.org/doc/lazy-injection.html



来源:https://stackoverflow.com/questions/24076299/circular-dependency-injecting-objects-that-are-directly-depended-on-each-other

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!