Wstrzykiwanie zależności (DI) to wzorzec, w którym obiekt otrzymuje swoje zależności z zewnątrz (wstrzykiwane) zamiast tworzyć je sam. Promuje luźne powiązania, testowalność i elastyczność — fundamentalny, szeroko stosowany wzorzec w nowoczesnym oprogramowaniu.
Czym jest wstrzykiwanie zależności
WITHOUT DI → a class CREATES its own dependencies (tightly coupled):
class OrderService { constructor() { this.db = new Database(); } } // hardcoded dependency
WITH DI → dependencies are PROVIDED (injected) from outside:
class OrderService { constructor(db) { this.db = db; } } // db is injected
→ the object doesn't create/control its dependencies → they're given to it
