Boost logo

Boost :

Subject: [boost] Is there any interest in a dependency injection library?
From: Krzysztof Jusiak (krzysztof_at_[hidden])
Date: 2014-07-24 15:47:27


Hi,

Is there any interest in a C++03/C++11/C++14 header only, type safe,
library providing compile time, macro free *constructor dependency
injection <http://en.wikipedia.org/wiki/Dependency_injection>*?

*Key features:*
* *Compile time *- 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* - example to detect circular dependencies or
limit supported types only to specified

*Hello World*
---------------------------------------------------------------------------------------------------------->
#include <memory>
#include <boost/shared_ptr.hpp>
*#include <boost/di.hpp>*

*namespace di = boost::di;*

struct interface { virtual ~interface() { } };
struct implementation : interface { };

class hello_world {
public:
    hello_world(std::unique_ptr<interface> up
                   , const std::shared_ptr<interface>& sp1
                   , boost::shared_ptr<interface> sp2
                   , int i)
        : i(i)
    {
        assert(dynamic_cast<implementation*>(up.get()));
        assert(dynamic_cast<implementation*>(sp1.get()));
        assert(dynamic_cast<implementation*>(sp2.get()));
        assert(sp1.get() == sp2.get());
        assert(i == 0);
    }

    int run() { return i; }

private:
    int i = 0;
};

int main() {
    // create injector and configure bindings
    auto injector = *di::make_injector*(
        *di::bind*<interface, implementation>() // scope will be deduced
    );

    // create and run hello_world
    return injector.*create*<hello_world>().run();
}
<----------------------------------------------------------------------------------------------------------

*Overview*:
* boost library incubator:
http://rrsd.com/blincubator.com/bi_library/di-dependency-injection-2
* repository: https://github.com/krzysztof-jusiak/di
* documentation: http://krzysztof-jusiak.github.io/di/boost/libs/di/doc/html
* issues: https://github.com/krzysztof-jusiak/di/issues
* download: https://github.com/krzysztof-jusiak/di/archive/master.zip

*Supported Compilers* - 100% test coverage:
* Linux (x86/x86-64)
    * Clang 3.2/3.3/3.4/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*:
* Linux, Darwin/Mac OS: https://travis-ci.org/krzysztof-jusiak/di
* Windows: https://ci.appveyor.com/project/krzysztof-jusiak/di
* Coverage: https://coveralls.io/r/krzysztof-jusiak/di

*Similar libraries*:
* https://bitbucket.org/cheez/dicpp
* 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 (proposal for boost library
in 2009: http://lists.boost.org/Archives/boost/2009/10/157477.php)
* 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

Java: https://github.com/google/guice
C#: http://www.ninject.org/

Source code was internally reviewed. DI was successfully used in a
commercial project as well.

Looking forward to hear from you. Any feedback is more than welcome.

Best regards, Kris


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk