Skip to content
/ di Public
forked from boost-ext/di

C++ Dependency Injection

Notifications You must be signed in to change notification settings

retf/di

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

C++ Dependency Injection

"Don't call us, we'll call you", Hollywood principle

Disclaimers

This is not an official Boost library yet and there is no guarantee it will ever be!

Introduction

Boost.DI is C++03/C++11/C++14 header only library providing type safe, compile time, macro free constructor dependency injection.

Hello World

#include <boost/di.hpp>

namespace di = boost::di;

struct hello {
    hello(const std::shared_ptr<i>& sp
        , std::unique_ptr<int> up
        , boost::function<int()> f)
        : sp(sp)
    {
        assert(dynamic_cast<impl*>(sp.get()));
        assert(*up == 0);
        assert(f.empty());
    }

    std::shared_ptr<int> sp;
};

struct world {
    world(hello copy
        , boost::shared_ptr<i> sp
        , const std::string& str
        , int i)
    {
        assert(dynamic_cast<impl*>(sp.get()));
        assert(copy.sp.get() == sp.get());
        assert(str == "");
        assert(i == 0);
    }
};

struct app {
    app(hello, world);
    int run();
};

int main() {
    auto injector = di::make_injector(
        di::bind<i, impl>() // scope deduction -> di::shared<di::bind<i, impl>>
    );

    return injector.create<app>().run();
}

Key features

  • Type safe
  • Compile time approach - 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
  • Compile time policies - ex. to detect circular dependencies or limit supported types only to specified
  • Supports C++03/C++11/C++14 standards

Other features

  • Header only library
  • Architecture independent (tested on x86/x86-64)
  • Supports all POSIX and Windows operating systems (tested on Linux 3.8/Windows 7/8)
  • Exception safe guaranty
  • Thread safe guaranty
  • Constructor injection (macro free)
  • Compile time creation guaranty
  • Dependencies life time management (scopes: deduce, external, unique, shared, session + custom scopes)
  • Scope deduction (shared_ptr -> shared, unique_ptr, lvalue -> unique, ...)
  • Supports copies, references, pointers, boost and std smart pointers / rvalue references(C++11)
  • Named parameters (named<int, my_int>)
  • Runtime visitor throughout created objects (useful for generation dependency diagrams)

More information

Tested compilers (100% test code coverage)

  • Linux (x86/x86-64)

    • Clang 3.2/3.3/3.4.2+ (clean with Clang Static Analyzer and Valgrind)
    • GCC 4.7.3/4.8.2/4.9.0+ (clean with Valgrind)
    • Intel C++ 14.0.3+ (clean with Valgrind)
  • Windows (x86/x86-64)

    • MinGW 4.7.3/4.8.2+
    • Visual Studio 2013+ (clean with DrMemory)
  • Darwin/Mac OS (x86-64)

    • Clang 503.0.40+ (Apple LLVM version 5.1)

Continuous integration

Build Status Build Status Coverage Status

Other Dependency Injection Frameworks for C++:

License

Distributed under the Boost Software License, Version 1.0.

About

C++ Dependency Injection

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published