ServiceManager Advice

Tag: zend-framework2 Author: fanye86 Date: 2013-01-13

I'm simply looking for advice on the best way I should handle this situation.

Right now I've got several files in a folder called Service. The files contact several functions which do random things of course. Each of these files needs access to the SM Adapter.

My question is, should I implement the ServiceManagerAwareInterface in each of these files OR should I just make a new class which implements the ServiceManagerAwareInterface and just extend my classes on the new class which implements this service?

Both ways work as they should, just not sure which way would be more proper.

Something else I thought of too, maybe just inject the SM into each of the service files from the Module.php file as a factory and create a __construct(ServiceManager $serviceManager) Right now I have all my service files setup as invokables.
Here is what my examples look like right now... Maybe someone can point out if my logic is wrong or if I'm even doing things right... gist.github.com/4667356

Best Answer

If you think that your system will always rely on ZF2, both approaches are equivalent.

Now from an OO design perspective, personally I have a preference for the approach in which you extend your service then implement the ServiceManagerAwareInterface. I would even use an interface for the dependency over the ServiceLocator to protect even more my classes. Why? Extending your classes does not cost you a lot, same for making your class depending on interfaces.

Let's take this example, Imagine you did not use this approach during a ZF1 project, during which you had probably resolved your dependencies with the Zend_Registry. Now, let's assume you moved to a ZF2 implementation, how much time you think you'll spend refactoring your code from something like Zend_Registry::get($serviceX) to $this->getServiceManager()->get($serviceX) on your Service layer?

Now Assume you had made the choice of protecting your classes, first by creating your own Service locator interface, as simple as:

public interface MyOwnServiceLocatorInterface{
    public function get($service);
}

Under ZF1 you had created an adapter class using the Zend_Registry:

public class MyZF1ServiceLocator implements MyOwnServiceLocatorInterface{
    public function get($service){
        Zend_Registry::get($service);
    }
}

Your Service classes are not coupled to the Zend_Registry, which make the refactoring much more easier.

Now, You decide to move to ZF2 so you'll logically use the ServiceManger. You create then this new Adapter class:

public class MyZF2ServiceLocator implements 
     ServiceManagerAwareInterface,MyOwnServiceLocatorInterface
{

    private $_sm;

    public function get($service){
        $this->_sm->get($service);
    }

    public function setServiceManager($serviceManager){
         $this->_sm = $serviceManager;
    }
}

Again, your Service classes are not coupled to the ZF2 ServiceManger.

Now, how would look like the configuration/registration of you Service layer on the ServiceManager. Well, you'll use your Module::getServiceConfig class for that:

//Module.php

public function getServiceConfig()
{
     return array(
           'factories'=>array(
                'My\ServiceA'=>function($sm){
                        return new My\ServiceA($sm->get('My\Service\Name\Space\MyZF2ServiceLocator'));
                    }

            //Some other config

            )
}

As you can see, no refactoring is needed within your Service classes as we protected them by relying on interface and using adapters. As we used a closure factory, we don't even need to extend our Service classes and implement the ServiceLocatorAwareInterface.

Now, before concluding in my previous example i have to note that I did not treat the case in which my classes are constructed via factories, however, you can check one of my previous answers that address the factory topic but also the importance of loose coupling among an application layers.

Other Answer1

you can add initializers to do that. It can reduce repetitive injection in getting the service that pass db adapter. OR, you can set abstract_factories, it will reduce repetitive SM registration. I just posted SM Cheatsheet here, Hope helpful :)

https://samsonasik.wordpress.com/2013/01/02/zend-framework-2-cheat-sheet-service-manager/