A dependency is an object another object needs to work. In layered systems, dependency arrows point downward: the UI commands the business layer, while returned data can travel upward without reversing source-code references. section 4.2.1 section 2.4.3
Hard-coding a database inside a report generator makes tests depend on that connection. Constructor injection supplies the dependency from outside, so a fake database can return controlled data. section 2.4.3
Listing 2.5 "After": Loosely Coupled (reportGenerator.js)
class ReportGeneratorAfter { #A
constructor(dbConnection) { #A
// The dependency is "injected" into the constructor. #A
this.dbConnection = dbConnection; #A
} #A
generate(reportName) {
const data = this.dbConnection.getData(reportName);
return `Report '${reportName}' generated with ${data.length} rows.`;
}
}See the report generator accept its database connection through the constructor instead of creating one itself. section 2.4.3


































