Why we don't use a Dependency Injection Container

Why we don't use a Dependency Injection Container

March 21, 2013 - by Pim Elshof

Tags:

dependency-injection-container

Dependency Injection Containers are great for managing small, known numbers of services. For managing a larger set of services that needs to be discoverable, another solution is necessary.

Dependency Injection is a really good thing. By now, most developers will agree with that. It makes unit testing possible and it's a vital aspect of good, solid design. Dependencies Injection Containers play a big role in many frameworks and applications, taking care of passing on dependencies across modules, or from framework to module.

At Procurios, we have about four hundred modules, most of which expose some services (we call those API's). These services are often retrieved by other modules to work with. For example, many modules use the framework's StringApi to convert the character set of inbound data.

Most, if not all, DIC solutions boil down to a container class that acts as a key-value store. In our case you could imagine something like

$Container->get('lib', 'StringApi'); 

This poses a significant problem, though: how do you know which services are exposed?

To make development easy enough, we need a way to auto-complete which services a module exposes. So we opted for a static ApiFactory in every module, which only shows the API's it provides:

$StringApi = LibApiFactory::getStringApi(); 

Controllers can pick up these services, destroying dependency injection in the process.

Testing becomes very difficult with controllers pulling in their dependencies themselves. Integration testing is still possible, but unit testing is not. So in order to allow for unit testing, we null-pass the dependencies to the constructor of the controller, creating them if we did not receive them. This way we salvage auto-completion, strong typing (over string typing) and DI!

public function __construct(StringApi $StringApi = null)
{
    $this->StringApi = $StringApi ?: LibApiFactory::getStringApi();
}

Share this post!

Comments

Leave a comment!

Italic and bold

*This is italic*, and _so is this_.
**This is bold**, and __so is this__.

Links

This is a link to [Procurios](http://www.procurios.nl).

Lists

A bulleted list can be made with:
- Minus-signs,
+ Add-signs,
* Or an asterisk.

A numbered list can be made with:
1. List item number 1.
2. List item number 2.

Quote

The text below creates a quote:
> This is the first line.
> This is the second line.

Code

A text block with code can be created. Prefix a line with four spaces and a code-block will be made.