Dependency and service injection

From semantic-mediawiki.org

Semantic MediaWiki uses a minimalistic dependency and service injection mechanism to ensure that recurring class invocations are managed in a central place to avoid complex interface maintenance and support for advanced service factoring.

SharedCallbackContainer hosts the definitions (internally managed by onoi/callback-container) and is made available through the ApplicationFactory as a so called ServiceLocator.

Direct wp:dependency injection is always preferred but sometimes it is unavoidable to rely on an indirect service access therefore some guidelines should be considered when deciding for or against using a service definition:

  • When a service is expected to be used more than once and/or requested in different places
  • When an interface is not easily extensible and testing is paramount, providing access via ApplicationFactory (ApplicationFactory::registerObject to override an instance during testing)

Furthermore, for a complex service, using a dedicated factory method (which can involve a service locator) is preferred over a simple new MyClass().

See also[edit]