Service locator

from Wikipedia, the free encyclopedia

A service locator ( English service locator ), or Resource Locator ( English resource locator ) is a by Martin Fowler imagined architectural pattern of software development . The service locator there is a central object , the registration ( English registry ) that returns to query for a specific object type a corresponding object.

Service locators are used

  • to bootstrap the dependency injection , d. H. as an intermediate step in refactoring to convert an application from fixed dependencies to dependency injection, or
  • to offer dynamic reconfiguration of the objects to be loaded during runtime,
  • if they are enforced by a used framework .

Service locator as an anti-pattern

Often times, a service locator is used by inexperienced developers rather than dependency injection. The reason for this is that dependency injection frameworks are also suitable for implementing service locators. However, a service locator has the disadvantage that it hides dependencies. The hidden dependencies make it harder to test the code and perform refactorings. In addition, errors in the configuration only occur at runtime. In this context, the service locator pattern is therefore considered an anti -pattern .

Individual evidence

  1. a b Daniel Baharestani: Mastering Ninject for dependency injection . Packt Publishing, 2013, ISBN 978-1-78216-620-7 (English, 142 pages).
  2. ^ Martin Fowler: Inversion of Control Containers and the Dependency Injection pattern. January 23, 2004, accessed March 25, 2017 .