C++03/11 | C++14
"Don't call us, we'll call you", Hollywood principle
This is not an official Boost library yet and there is no guarantee it will ever be!
Boost.DI is C++03/11 header only library providing type safe, compile time, macro free constructor dependency injection.
- Documentation
- [Todo/Issues] (https://github.com/krzysztof-jusiak/di/issues?state=open)
- Boost Library Incubator
- Boost Library Proposal
Hello World
#include <memory>
#include <boost/di.hpp>
namespace di = boost::di;
class hello_world {
public:
hello_world(std::shared_ptr<ilogger> logger
, std::shared_ptr<ilogic> logic
, bool value)
: logger_(logger)
, logic_(logic)
, value_(value)
{ }
int run() const {
if (value_) {
logger_->log("hello world");
return logic_->do_it();
}
}
std::shared_ptr<ilogger> logger_;
std::shared_ptr<ilogic> logic_;
bool value_ = false;
};
int main() {
// dependency injection configuration
auto injector = di::make_injector(
di::bind<ilogger, logger>()
, di::bind<ilogic, logic>()
, di::bind<bool>::to(true)
);
// composition root
return injector.create<hello_world>().run();
}Tests with Automatic Mocks Injector
#include <mocks_injector.hpp>
void test_hello_world_when_run_with_value_false_then_nothing() {
auto mi = di::make_mocks_injector(di::bind<bool>::to(false));
mi.create<hello_world>()->run();
}
void test_hello_world_when_run_with_value_true_then_log_and_do_it() {
auto mi = di::make_mocks_injector(di::bind<bool>::to(true));
EXPECT_CALL(mi, ilogic::do_it);
EXPECT_CALL(mi, ilogger::log).With("hello world");
mi.create<hello_world>()->run();
}- Type safe
- Header only
- Supports C++03/C++11/C++14 standards
- Exception safe guaranty
- Thread safe guaranty
- Compile time creation guaranty - no exceptions - if application compiles all dependencies will be be created accurately
- Macro free - by default no need to specify constructor traits or register anything (less intrusive)
- Scopes deduction - scopes are deduced based on type semantic
- Automatic conversion between std/boost smart pointers
- Dependencies life time management (scopes: deduce, external, unique, shared, session + custom scopes)
- Compile time policies - ex. to detect circular dependencies or limit supported types only to specified
- Supports copies, references, pointers, boost and std smart pointers / rvalue references(C++11)
- Supports annotations (di::named<int, my_int>)
- Runtime visitor throughout created objects (useful for generation dependency diagrams)
-
Linux (x86/x86-64)
- Clang 3.2/3.3/3.4/3.5/3.6+ (clean with Clang Static Analyzer and Valgrind)
- GCC 4.7/4.8/4.9+ (clean with Valgrind)
- Intel C++ 14.0+ (clean with Valgrind)
-
Windows (x86/x86-64)
- MinGW 4.7/4.8/4.9+
- Visual Studio 2013+ (clean with DrMemory)
-
Darwin/Mac OS (x86-64)
- Clang 503+
- https://bitbucket.org/cheez/dicpp
- https://github.com/google/fruit
- https://code.google.com/p/infectorpp
- https://github.com/phs/sauce
- https://code.google.com/p/wallaroo
- https://code.google.com/p/hypodermic
- https://github.com/admiyo/CppInject
- http://qtioccontainer.sourceforge.net
- https://code.google.com/p/autumnframework
- http://sourceforge.net/projects/cpp-resolver
- https://code.google.com/p/pococapsule
- https://code.google.com/p/spring-cpp
- http://www.cs.utah.edu/~arichard/inject.html
- http://bobah.net/d4d/source-code/misc/cpp-dependency-injection
- https://code.google.com/p/ffead-cpp
- http://codebros.github.io/DiLite
- https://code.google.com/p/kindi
- https://www.leapmotion.com/blog/hello-world-meet-autowiring-c
Distributed under the Boost Software License, Version 1.0.

