18.8 Improving Your Code’s Design and Testability with PicoContainer.NET

The Singleton design pattern is one of the most easily understandable patterns, which also makes it one of the most likely to be abused. While singletons let you ensure that a class will be instantiated only once, they can also make for brittle and less-testable software by creating unnecessary static dependencies. The same goes for any number of other useful patterns that, when abused, create even more static dependencies.

Tip

See the seminal work Design Patterns: Elements of Reusable Object-Oriented Software, by Gamma et al. (Addison-Wesley), more commonly known as the “Gang of Four book,” for more information on patterns.

Static dependencies exist when a specific something has to know implementation details of something else, such as a database connection. You’re reliant on those implementation details never changing. In the case of a database connection, what happens if you move your software to a different database platform? You’ll be forced to go back and change that static dependency.

Following the principles of Inversion of Control (IoC) or Dependency Injection (DI) while designing your applications will lead to cleaner, simpler, and better-architected applications by reducing or eliminating static dependencies.

Tip

For a thorough description of the details of IoC and DI, see Martin Fowler’s article “Inversion of Control Containers and the Dependency Injection Pattern” at http://www.martinfowler.com/articles/injection.html ...

Get Windows Developer Power Tools now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.