How to Use C++ Dependency Injection to Write Maintainable Software
summary
Concrete rules for C++ dependency injection: prefer constructor or function parameters over setters, pass read‑only data as value or const reference and behaviors as std::function / std::function_ref or Concepts/abstract base classes, use factories to defer instantiation, avoid piping and forced lifetime choices, prefer references/unique_ptrs where ownership is clear, use aliasing constructor or holder pattern when factories must bundle lifetimes, and leverage testing with Google Test/Mock to inject mocks; expect tradeoffs in performance and header coupling.
AI-written summary · about
chapters (19)
- 0:00Intro
- 1:00Dependency Injection
- 5:36How to pass dependencies around
- 13:17Data which is constant
- 14:29Data which might change
- 23:31Passing the dependencies
- 28:19References are Universal
- 34:11Factories
- 35:31Unique Point
- 41:58The Solution
- 44:56Advantages and Disadvantages
- 46:08Testability
- 47:20Recap
- 48:09Conclusion
- 48:37Questions
- 49:16Can it work
- 52:46Passing as a parameter
- 55:04Multiple functions
- 57:59Piping
related talks
01h 01mDependency Injection in C++ - A Practical Guide
01h 29mDependency Injection in C++ - A Practical Guide
01h 02mRefactoring C++ Code for Unit testing with Dependency Injection
58mA Practical Guide to Loose Coupling
46mTestability and C++ API Design
01h 02mRetiring the Singleton Pattern: Concrete Suggestions for What to use Instead
01h 01mBack to Basics: Test-driven Development
59mThe Most Important C++ Design Guideline is Testability
