Dependency Injection Wiring
Prerequisites & Dependencies
- TypeScript 5+ (recommended) or Python 3.10+
- Understanding of interfaces and inversion of control principles
- Optional:
npm i inversify/pip i dependency-injectoror manual DI container
Execution Steps
- Define interfaces for all high-level dependencies (database, email service, external APIs)
- Create concrete implementations of those interfaces
- Build a DI container (or use an existing one) that maps interfaces → implementations
- Request dependencies via constructor injection or property injection, avoid
newor global singletons in business logic - Wire the container at the application bootstrap (e.g.,
index.ts,main.py) and verify that swapping implementations (mock vs real) is trivial for testing - Document the DI graph and ensure no circular dependencies exist
// InversifyJS example: interface → implementation wiring
import { injectable, inject, interfaces } from 'inversify';
import { TYPES } from './types';
@injectable()
class UserService {
constructor(@inject(TYPES.IUserRepository) private repo: IUserRepository) {}
async getById(id: string) { return this.repo.find(id); }
}
@injectable()
class UserRepository implements IUserRepository {
async find(id: string) { /* DB logic */ }
}
const container = new Container();
container.bind<IUserRepository>(TYPES.IUserRepository).to(UserRepository);
// Bootstrap app with resolved dependencies
const service = container.get(UserService);
service.getById('123');