Skip to content
Niels Braczek edited this page Dec 3, 2015 · 2 revisions

Dependency Injection

Decision

Use Dependency Injection (Containers) wherever possible. Avoid Service Locators, if possible. Global services are requested by raising an event (e.g., ‘requestLogger’), which returns a logger.

Reason

With Dependency Injection, the provided services and objects are controlled by the calling instance. Dependency Injection Containers may help to keep the signature of constructor short, if several dependencies are required.

Service Locators are appealing on first sight, but tend to turn into an anti­pattern, as they provide global references and cannot easily be tailored for special use cases (HMVC, for example, may need different services for subsequent requests). Requesting services using events decreases coupling. The event dispatcher is responsible for caching the result of those events, so there is no performance impact.

Clone this wiki locally