డిపెండెన్సీ ఇంజెక్షన్ (DI) ఒక ప్యాటర్న్, ఇక్కడ ఒక ఆబ్జెక్ట్ దాని డిపెండెన్సీలను బాహ్యం నుండి (ఇంజెక్ట్ చేయబడిన) పొందుతుంది, కాకుండా వాటిని స్వయంగా సృష్టించదు. ఇది సడిగిన కపలింగ్, టెస్టేబిలిటీ, మరియు సరళతను ప్రోత్సహిస్తుంది — ఆధുনిక సాఫ్টువేర్లో ఒక ప్రాథమిక, విస్తృతంగా ఉపయోగించిన ప్యాటర్న్.
డిపెండెన్సీ ఇంజెక్షన్ అంటే ఏమిటి
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
