Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between Service and Manager classes in Zend Framework 2

I'm getting a little confused about the Manager and Service class name suffix.

As far as I understand the difference, Managers are responsible for handling (creating, retrieving, deleting, ...) certain types of entities. For instance, the ModuleManager is responsible for loading and returning Modules. In this case, you do care about the actual entity, the Module.

However, Services are classes that provide interfaces to execute cetain types of process logic. For example, a LogService sends a given log message to the defined log writer. You don't care where it goes and what it does, you just want the admin to be informed about what just happened.

Now, ZF2 provides a ServiceManager that creates and returns instances of a given Service. I accidentally got used to creating Managers and providing a factory to the ServiceManager so that you can access the Manager using $this->getServiceLocator()->get('managerName'); in the Controller context in order to keep controllers tiny and the real logic in testable classes. This is the part that is confusing me right now because obviously, it can't be recommended to retrieve Managers using a ServiceLocator. BUT: I'm not the only one doing this: The Doctrine ORM module is another example for this: It registers the EntityManager as a doctrine.entitymanager.orm_default Service by default.

Am I getting the true difference between Services and Managers wrong? Is there even a difference? Are Managers maybe even inherited from Services in the concept?

like image 844
Daniel M Avatar asked Sep 04 '12 10:09

Daniel M


1 Answers

I'll try to break down managers and services in ZF2 for you.

Managers

Unfortunately, the word 'manager' in reference to classes is incredibly ambiguous and within ZF2, there's a bit of inconsistency with how the word is used. Thus, there's truly no authoritative definition for what a 'manager' is in ZF2. Currently, the 'Managers' in ZF2 can best be broken down like this:

  • ServiceManager — An instance of Zend\ServiceManager\ServiceManager. There can be multiple ServiceManager instances -- by default, there's just the "main" service manager, which is configured with the 'services' config key or by the array(s) returned by Module's getServiceConfig() method.
  • Plugin Managers — These extend Zend\ServiceManager\PluginManager which extends Zend\ServiceManager\ServiceManager with some specialized functionality. These are spread throughout many components in the framework, and provide the functionality of what was previously known (in earlier versions of ZF2) as Plugin Loaders/Brokers. These plugin managers are what initialize things like view helpers, controller plugins, and controllers themselves.
  • Other 'Managers' — Things like Zend\ModuleManager\ModuleManager and Zend\Session\SessionManager. These have nothing to do with the ServiceManager, but just happen to have 'Manager' as a suffix to their name.

I think you may be getting confused trying to assign a definition to a term (Manager) that was not designed with any specific definition in mind. As the author of Zend\ModuleManager, I can tell you that I originally developed the component as Zend\Module (I really don't like Manager as a suffix). It was decided in one of our weekly IRC meetings that Zend\Module is ambiguous and that suffixing it with 'Manager' would somehow resolve that ambiguity. Obviously I was out-voted on the matter. My point is, Zend\ModuleManager was not developed to any specification of being a 'manager', by any definition.

Services

In ZF2, with regard to Zend\ServiceManager, 'services' are simply objects (and can technically also be arrays). The ServiceManager component can be thought of as a simple key-value registry for the various 'services' (objects) your application might need. These 'services' are typically things like a configured mailer, logger, database adapter, application config, etc. Of course the ServiceManager is not just a simple registry, and its primary function is to defer instantiation of the services (and their dependencies) until they're actually needed; aka lazy loading); I've written a blog post which explains the various features of the ServiceManager in depth.

I accidentally got used to creating Managers and providing a factory to the ServiceManager so that you can access the Manager using $this->getServiceLocator()->get('managerName)

I think in this case you may be confusing the term 'manager' with 'service' here. Things registered with the ServiceManager can simply be referred to as 'services'. This could be confusing because you could, indeed, register some sort of 'manager' as a service. For instance, you could have a 'session' service that's an instance of Zend\Session\SessionManager. Further confusion ensues, as the term 'service' generally refers to a class which makes up part of a service layer.

So, unfortunately, yes, the terminology in ZF2 can be a bit foggy, but once you understand a couple of relatively simple core concepts, the great design really opens up some amazing flexibility that is, in my opinion, unparalleled by most of the other existing frameworks out there.

Hope this helps.

like image 88
EvanDotPro Avatar answered Sep 18 '22 06:09

EvanDotPro