Dependency injection (DI) เป็น pattern ที่อ็อบเจ็กต์รับ dependency จากภายนอก (ถูก inject เข้ามา) แทนที่จะสร้างมันเอง มันส่งเสริม loose coupling, testability และความยืดหยุ่น เป็น pattern ที่เป็นพื้นฐานและถูกใช้อย่างกว้างขวางในซอฟต์แวร์สมัยใหม่
Dependency injection คืออะไร
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
