(In what case) Can a ServiceManager Injection be warrantable?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

(In what case) Can a ServiceManager Injection be warrantable?

automatix
Hello Community! As is generally known injecting the ServiceManager as dependency is a bad practice. Nonetheless -- are there compromises/exceptions, where it would be acceptable in your opinion? Is a Factory for example such a case:
class FooFactory {
    public function __invoke(ServiceLocatorInterface $serviceManager) {
        $bar = $serviceManager->get('My\Bar');
        $baz = $serviceManager->get('My\Baz');
        $foo = new Foo($bar);
        $foo->setBaz($baz);
        ...
        return $foo;
    }
}
vs.
class FooFactory {
    public function __invoke(My\Bar $bar, My\Baz $baz) {
        $foo = new Foo($bar);
        $foo->setBaz($baz);
        ...
        return $foo;
    }
}
Thanks & regards, Ilya
Reply | Threaded
Open this post in threaded view
|

Re: (In what case) Can a ServiceManager Injection be warrantable?

automatix
The example was incorect, here is a patch:

use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;

class FooFactory implements FactoryInterface {
    public function createService(ServiceLocatorInterface $serviceManager) {
        $bar = $serviceManager->get('My\Bar');
        $baz = $serviceManager->get('My\Baz');
        $foo = new Foo($bar);
        $foo->setBaz($baz);
        ...
        return $foo;
    }
}

vs.

class FooFactory {
    private $bar;
    private $baz;
    public function __construct(\My\Bar $bar, \My\Baz $baz) {
        $this->bar = $bar;
        $this->baz = $baz;
    }
    public function createService() {
        $foo = new Foo($this->bar);
        $foo->setBaz($this->baz);
        ...
        return $foo;
    }
}
Reply | Threaded
Open this post in threaded view
|

Re: (In what case) Can a ServiceManager Injection be warrantable?

Marco Pivetta
In reply to this post by automatix
Hello Automatix,

Locations that I found to be legit use-case scenarios for the
ServiceLocator are:

 - factories
 - abstract factories
 - any kind of lookup map, such as:
    * a connection pool
    * map from name to service (such as in the DispatchListener in ZF2)

Basically, it drills down to either scrap code or code that is actually
doing some "location" job.

Marco Pivetta

http://twitter.com/Ocramius

http://ocramius.github.com/

On 5 August 2015 at 10:52, automatix <[hidden email]> wrote:

> Hello Community!As is generally known injecting the ServiceManager as
> dependency is a bad practice. Nonetheless -- are there
> compromises/exceptions, where it would be acceptable in your opinion? Is a
> Factory for example such a case:vs.Thanks & regards,Ilya
>
>
>
> --
> View this message in context:
> http://zend-framework-community.634137.n4.nabble.com/In-what-case-Can-a-ServiceManager-Injection-be-warrantable-tp4662642.html
> Sent from the Zend Framework mailing list archive at Nabble.com.