Dependency Injection

What Is a Dependency Injection Container?

A dependency injection container builds objects and their dependencies from registered rules. Learn what it does and when a small program can skip one.

On this page
  1. What a container does
  2. Wiring by hand
  3. Containers in C++
  4. Trade-offs
  5. When you can skip one
  6. Key takeaways

What is a dependency injection container? It is a tool that knows how to build objects and the dependencies they need. You register rules, such as “when a Logger is needed, create a ConsoleLogger”, and then ask the container for a UserService. It builds the logger, passes it in, and returns a ready object. Containers are optional. Small programs often do the same job with a few lines in main.

What a container does

Most containers handle three jobs:

  • Registration. You tell the container which implementation to use for each interface.
  • Resolution. You ask for an object, and the container builds it, including its dependencies, in the right order.
  • Lifetime. You decide how long an object lives. Many containers offer transient objects, which are created each time they are requested, and singletons, which are created once and shared.

Wiring by hand

Before reaching for a container, look at the manual version. A factory function does the same wiring a container would. Assume UserService takes a std::unique_ptr<Logger> and that ConsoleLogger derives from Logger:

#include <memory>

std::unique_ptr<UserService> make_user_service() {
    return std::make_unique<UserService>(std::make_unique<ConsoleLogger>());
}

This function is a small, hand-written container. Its rule is fixed in code. A container stores rules like this one, so you can change them in one place and build complex object graphs without writing each factory yourself.

Containers in C++

The C++ standard library does not include a dependency injection container. Several open-source libraries provide one, including Boost.DI and Google’s Fruit. They differ in how they register types, how much of the checking happens at compile time, and which C++ standard they require. Check each project’s documentation and supported compilers before you adopt one.

Trade-offs

A container can help in a few ways:

  • Wiring lives in one place, which helps large programs.
  • Swapping implementations for tests or different environments becomes a change to registration rather than to every class.

It also brings costs:

  • The flow of control is less obvious, so it can be harder to follow in a debugger.
  • The library has its own learning curve.
  • Compile-time errors from template-heavy libraries can be long and hard to read.
  • Builds can take longer in some setups.

When you can skip one

A small program with a handful of classes rarely needs a container. Manual wiring in main or a factory function is shorter, easier to debug, and clear to readers. Consider a container when the object graph is large and repeated across the program, or when different builds need different wiring.

Key takeaways

  • A container builds objects and their dependencies from rules you register.
  • It is optional, and plain C++ can do the same wiring by hand.
  • Registration, resolution and lifetime are the core jobs.
  • Weigh the added indirection and learning curve against the benefit for your project.

The complete guide to dependency injection

Dependency Injection in C++
Read the guide

Keep exploring

Guides from other topics

All topics →