Refactoring C++ Code for Unit testing with Dependency Injection
summary
Shows concrete DI techniques and their tradeoffs: link-time substitution works but is brittle and discouraged, inheritance and interfaces give rich mocks, templates shave virtual overhead but bloat legacy builds, and type erasure (std::function / invocable) is the most versatile. Demonstrates refactoring a “god” class into layered components, using constructor injection or dependency-provider std::function for lazy resources, replacing setters/method-injection where they break APIs, and a real example where a 165‑method interface gets tamed into testable units.
AI-written summary · about
chapters (14)
- 0:00Introduction
- 2:36Dependency injection basics
- 6:48Dependency injection methods
- 10:19Inheritance as a dependency injection mechanism
- 13:34Templates for dependency injection
- 16:26Type erasure for DI
- 19:59Null objects for testing
- 22:29Constructor vs method injection
- 26:46Real-world dependency injection roadblocks
- 33:22Refactoring legacy code
- 45:23Legacy code challenges
- 55:45Dependency injection truths in production systems
- 57:27Key takeaways
- 1:01:25Q&A
related talks
01h 01mDependency Injection in C++ - A Practical Guide
01h 29mDependency Injection in C++ - A Practical Guide
01h 03mHow to Use C++ Dependency Injection to Write Maintainable Software
58mA Practical Guide to Loose Coupling
38mSurgical Refactoring of Non-Tested Code – A Worked Example
46mTestability and C++ API Design
07mTDD/BDD and DI
01h 00mThe hidden rules of world-class C++ code
