Dependency Injection (DI) is more than just passing objects into constructors. It’s a mindset that improves maintainability and testability.

Why DI matters

Hard-coded dependencies create tight coupling, making unit testing a nightmare. DI allows you to inject different implementations without changing the client.

Constructor vs Setter Injection

Constructor injection ensures that a class cannot exist in an invalid state. Setter injection is useful for optional dependencies.

Example: Logger in a mini-framework

Instead of calling new Logger() inside a controller, inject a LoggerInterface. The container can decide which concrete implementation to provide.

DI in your mini-framework

Your lightweight framework can implement DI without reflection magic. Just map interfaces to concrete classes in the container, and let the dispatcher inject them when resolving controllers.

Benefits

  • Better testability
  • Clear dependency contracts
  • Easier refactoring

Conclusion

DI is essential even in small frameworks. Embracing it early pays off when your application grows.