Skip to content

Repository files navigation

Boost LicenceVersionLinuxCoverallsTry 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>namespacedi= boost::di;

Compile

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

Quick guide - Create object graph

classctor {
public:explicitctor(int i) : i(i) {}
int i;
};
structaggregate {
double d;
};
classexample {
public:example(aggregate a, const ctor& c) {
assert(87.0 == a.d);
assert(42 == c.i);
};
};
intmain() {
constauto 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.8GCC-6MSVC-2015
Compilation Time0.102s0.118s0.296s
Binary size (stripped)6.2kb6.2kb105kb
ASM x86-64

xor eax, eax
retq

Quick guide - Bind interfaces

structinterface {
virtual~interface() noexcept = default;
virtualintget() const = 0;
};
classimplementation : publicinterface {
public:intget() constoverride { return42; }
};
structexample {
example(std::shared_ptr<interface> i) {
assert(42 == i->get());
}
};
intmain() {
constauto injector = di::make_injector(
di::bind<interface>.to<implementation>()
);
injector.create<std::unique_ptr<example>>();
}

Run this example on Wandbox.

Clang-3.8GCC-6MSVC-2015
Compilation Time0.102s0.118s0.296s
Binary size (stripped)6.2kb6.2kb105kb
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<classErrorPolicy = classTErrorPolicy>
classsimple_updater {
public:voidupdate() const {
ErrorPolicy::on("update");
}
};
template<classUpdater = classTUpdater>
classexample {
public:explicitexample(const Updater& updater)
: updater(updater)
{ }
voidupdate() {
updater.update();
}
private:const Updater& updater;
};
intmain() {
structthrow_policy {
staticvoidon(const std::string& str) {
throwstd::runtime_error(str);
}
};
constauto injector = di::make_injector(
di::bind<classTErrorPolicy>.to<throw_policy>(),
di::bind<classTUpdater>.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.8GCC-6MSVC-2015
Compilation Time0.102s0.118s0.296s
Binary size (stripped)6.2kb6.2kb105kb
ASM x86-64

xor eax, eax
retq

Quick guide - Bind concepts

structStreamable {
template<classT>
autorequires(T&& t) -> decltype(
int( t.read() ),
t.write(int)
);
};
template<classExchange = Streamable(classExchangeStream)
class Engine = Streamable(classEngineStream)>
class example {
public:example(Exchange exchange, Engine engine)
: exchange(std::move(exchange)), engine(std::move(engine))
{ }
private:
Exchange exchange;
Engine engine;
};
intmain() {
constauto injector = di::make_injector(
di::bind<Streamable(classExchangeStream)>.to<exchange>(),
di::bind<Streamable(classEngineStream)>.to<engine>()
);
injector.create<example>();
}

Run this example on Wandbox.

Clang-3.8GCC-6MSVC-2015
Compilation Time0.102s0.118s0.296s
Binary size (stripped)6.2kb6.2kb105kb
ASM x86-64

xor eax, eax
retq


Documentation


Disclaimerboost-ext.di is not an official Boost library.

About

C++14 Dependency Injection library

Topics

Resources

Contributing

Stars

1.3k stars

Watchers

64 watching

Forks

Releases

Used by

Contributors

Languages