High-level modules should not depend on low-level modules — both should depend on abstractions.
// dirty — UserService is hardcoded to MySQLUserRepo
class MySQLUserRepo {
constructor(private db: Record<string, unknown>) {}
findById(id: string): unknown { return this.db[id]; }
}
class UserService {
private repo = new MySQLUserRepo({}); // hardcoded!
getUser(id: string): unknown { return this.repo.findById(id); }
}Refactor `solve(db, id)`: instead of hardcoding the repo, create a simple in-memory repo from db (a plain object) and return db[id]. The point: solve receives its data source — it doesn't instantiate one.
solve({ u1: { id: 'u1', name: 'Alice' } }, 'u1') → { id: 'u1', name: 'Alice' }
Sample tests