Lesson 20 / 35

What Dependency Injection Solves

See the tight coupling and untestable code that manual object wiring with new creates, and how injecting dependencies through constructors fixes both.

By · 15 years in web development · Updated 1 Oct 2026

Don't build your own tools

A carpenter doesn't forge their own hammer — they're handed one. A class shouldn't forge its own collaborators with new either; it should be handed (injected) whatever it depends on, and just describe what it needs, not how to build it.

Coupling and testability

class OrderService { private EmailSender s = new SmtpSender(); } hard-codes SmtpSender — you can never substitute a fake in a test, or swap providers without editing this class. Injecting an EmailSender through the constructor lets tests pass a stub and production wire a real one.