MCPcopy Create free account
hub / github.com/boost-ext/di

github.com/boost-ext/di @v1.3.2

Chat with this repo
repository ↗ · DeepWiki ↗ · release v1.3.2 ↗ · + Follow
12,973 symbols 118,820 edges 539 files 1,237 documented · 10% updated 3mo agov1.3.2 · 2025-04-02★ 1,26586 open issues

Browse by type

Functions 7,776 Types & classes 5,197
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

Boost Licence Version Linux Coveralls Try it online


boost-ext.di

Your C++14 one header only Dependency Injection library with no dependencies

Dependency Injection

https://www.youtube.com/watch?v=yVogS4NbL6U


Quick start

Download

boost-ext.di requires only one file. Get the latest header here!

Include

#include <boost/di.hpp>
namespace di = boost::di;

Compile

  • GCC/Clang sh $CXX -std=c++14 -O2 -fno-exceptions -fno-rtti -Wall -Werror -pedantic-errors file.cpp
  • MSVC sh cl /std:c++14 /Ox /W3 file.cpp

Quick guide - Create object graph

class ctor {
public:
  explicit ctor(int i) : i(i) {}
  int i;
};

struct aggregate {
  double d;
};

class example {
 public:
  example(aggregate a, const ctor& c) {
    assert(87.0 == a.d);
    assert(42 == c.i);
  };
};

int main() {
  const auto injector = di::make_injector(
    di::bind<int>.to(42),
    di::bind<double>.to(87.0)
  );

  injector.create<example>();
}

Run this example on Wandbox.

Clang-3.8 GCC-6 MSVC-2015
Compilation Time 0.102s 0.118s 0.296s
Binary size (stripped) 6.2kb 6.2kb 105kb
ASM x86-64

xor eax, eax
retq
      

Quick guide - Bind interfaces

struct interface {
  virtual ~interface() noexcept = default;
  virtual int get() const = 0;
};

class implementation : public interface {
public:
  int get() const override { return 42; }
};

struct example {
  example(std::shared_ptr<interface> i) {
    assert(42 == i->get());
  }
};

int main() {
  const auto injector = di::make_injector(
    di::bind<interface>.to<implementation>()
  );

  injector.create<std::unique_ptr<example>>();
}

Run this example on Wandbox.

Clang-3.8 GCC-6 MSVC-2015
Compilation Time 0.102s 0.118s 0.296s
Binary size (stripped) 6.2kb 6.2kb 105kb
ASM x86-64 (same as `make_unique`)

push   %rbx
mov    %rdi,%rbx
mov    $0x8,%edi
callq  0x4008e0 <_Znwm@plt>
movq   $0x400c78,(%rax)
mov    %rax,(%rbx)
mov    %rbx,%rax
pop    %rbx
retq
      

Quick guide - Bind templates

template<class ErrorPolicy = class TErrorPolicy>
class simple_updater {
public:
  void update() const {
    ErrorPolicy::on("update");
  }
};

template<class Updater = class TUpdater>
class example {
public:
  explicit example(const Updater& updater)
    : updater(updater)
  { }

  void update() {
    updater.update();
  }

private:
  const Updater& updater;
};

int main() {
  struct throw_policy {
    static void on(const std::string& str) {
      throw std::runtime_error(str);
    }
  };

  const auto injector = di::make_injector(
    di::bind<class TErrorPolicy>.to<throw_policy>(),
    di::bind<class TUpdater>.to<simple_updater>()
  );

  injector.create<example>().update();
  // Terminates with an uncaught exception because of our bound error policy
}

Run this example on Wandbox.

Clang-3.8 GCC-6 MSVC-2015
Compilation Time 0.102s 0.118s 0.296s
Binary size (stripped) 6.2kb 6.2kb 105kb
ASM x86-64

xor eax, eax
retq
      

Quick guide - Bind concepts

struct Streamable {
 template<class T>
 auto requires(T&& t) -> decltype(
   int( t.read() ),
   t.write(int)
 );
};

template<class Exchange = Streamable(class ExchangeStream)
         class Engine   = Streamable(class EngineStream)>
class example {
public:
  example(Exchange exchange, Engine engine)
    : exchange(std::move(exchange)), engine(std::move(engine))
  { }

private:
  Exchange exchange;
  Engine engine;
};

int main() {
  const auto injector = di::make_injector(
    di::bind<Streamable(class ExchangeStream)>.to<exchange>(),
    di::bind<Streamable(class EngineStream)>.to<engine>()
  );

  injector.create<example>();
}

Run this example on Wandbox.

Clang-3.8 GCC-6 MSVC-2015
Compilation Time 0.102s 0.118s 0.296s
Binary size (stripped) 6.2kb 6.2kb 105kb
ASM x86-64

xor eax, eax
retq
      

Documentation

Extension points exported contracts — how you extend this code

Core symbols most depended-on inside this repo

Shape

Class 4,881
Method 4,798
Function 2,978
Interface 311
Enum 5

Languages

C++57%
TypeScript20%
Java16%
C#7%
Python1%

Modules by API surface

benchmark/performance/di.cpp917 symbols
benchmark/create_complex/dagger2.java827 symbols
benchmark/create_complex/ninject.cs726 symbols
benchmark/create_complex/guice.java726 symbols
benchmark/create_complex/fruit.cpp724 symbols
benchmark/create_complex/dicpp.cpp724 symbols
benchmark/create_complex/di.cpp724 symbols
include/boost/di.hpp499 symbols
doc/cppnow-2016/example/typename_erasure.cpp405 symbols
doc/themes/boost-modern/js/codemirror/codemirror.js374 symbols
doc/cppnow-2016/example/resolve.cpp268 symbols
benchmark/create_simple/dagger2.java207 symbols

For agents

$ claude mcp add di \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact

Ask about this repo answers extend the page