Paul Jones: Configuration Values Are Dependencies, Too

In this post to his site Paul Jones suggests that dependencies aren’t limited to just the libraries and tools you use in your applications, it’s also the configuration values that define it’s environment.

As part of my consulting work, I get the opportunity to review lots of different codebases of varying modernity. One thing I’ve noticed with some otherwise-modern codebases is that they often “reach out” from inside a class to retrieve configuration values, instead of injecting those values into the class from the outside. That is, they use an equivalent of globals or service-location to read configuration, instead of using dependency injection.

[…] The effect, though, is global-ish or service-locator-ish in nature: the class is reaching outside its own scope to retrieve values it needs for its own operation. Likewise, one cannot tell from the outside the class what configuration values it depends on.

He gives code examples of this in action, showing pieces of code directly accessing the configuration structure from inside the class functionality. His examples show direct configuration access (not great), using a "Configuration" object (a bit better) and, finally, what he sees as a more correct way: injecting them like any other value. Code examples are provided for each step in this evolution, helping to illustrate his point.

PHPDeveloper.org

Schreibe einen Kommentar