Quantcast
Channel: Recent posts across whole site
Viewing all articles
Browse latest Browse all 49197

Butler object = Service Locator ?

$
0
0

In his article about dependency injection, Martin Fowler makes a distinction between "dependency injection" and a "Service Locator".
http://martinfowler.com/articles/injection.html#ServiceLocatorVsDependen...

Dpi in a very general sense just means that consumer components (plugins, page callbacks, etc) get their dependencies (information about current user, current language, gateways to load nodes and other pieces of content) injected as parameters (constructor, setter, or just function parameters), so they do not have to pull this information from global state (global vars, singletons, functions with global state dependencies). Something that should be a policy in any sane project, but is desperately missing in Drupal.

In Fowler's article, dpi is something more specific:
- The consumer component does not get one know-it-all gateway object, but rather a bunch of tiny objects, representing different pieces of the information that is to be injected.
- There is a system that looks at the signature of the class to instantiate (constructor signature, existing setters, implemented interfaces), and that can automatically choose which objects need to be injected into the to-be-created object. To generalize this concept, we could replace the signature with a definition array, and put a factory in between the injector system and the class to instantiate.
- This also implies that objects are never constructed by the consumer component, but by the injector system.

The alternative he mentions is the "Service Locator":
- Instead of a bunch of tiny objects, every consumer component gets the same "service locator" object injected.
- The service locator knows how to respond to requests for specific pieces of information.
- Usually it will delegate these requests to registered or lazy-loaded sub-services ("plugins" ?).

Interesting observation: The service locator can be regarded as a special case of the dependency injection, where every constructor or factory wants nothing but a service locator injected. This also means that we could mix both concepts, if we really want to.

I had the impression that this service locator is very close to the butler concept.
Is this observation correct?
What do we gain, having found this existing term?
Why have I not seen the term in previous discussions?


Viewing all articles
Browse latest Browse all 49197

Trending Articles