All Projects → boost-ext → Di

boost-ext / Di

DI: C++14 Dependency Injection Library

Programming Languages

metaprogramming
66 projects

Projects that are alternatives of or similar to Di

Lld Parking Lot
Repository for low level design solution of parking lot
Stars: ✭ 27 (-96.43%)
Mutual labels:  dependency-injection, design-patterns
concurrent-resource
A header-only C++ library that allows easily creating thread-safe, concurrency friendly resources.
Stars: ✭ 17 (-97.75%)
Mutual labels:  boost, design-patterns
Python Dependency Injector
Dependency injection framework for Python
Stars: ✭ 1,203 (+59.13%)
Mutual labels:  dependency-injection, design-patterns
Service Pattern Go
Simple clean Go REST API architecture with dependency injection and mocking example, following SOLID principles.
Stars: ✭ 449 (-40.61%)
Mutual labels:  dependency-injection, design-patterns
ufw
A minimalist framework for rapid server side applications prototyping in C++ with dependency injection support.
Stars: ✭ 19 (-97.49%)
Mutual labels:  dependency-injection, boost
Sml
SML: C++14 State Machine Library
Stars: ✭ 540 (-28.57%)
Mutual labels:  boost, design-patterns
Scabbard
🗡 A tool to visualize Dagger 2 dependency graphs
Stars: ✭ 615 (-18.65%)
Mutual labels:  dependency-injection
Injector
Python dependency injection framework, inspired by Guice
Stars: ✭ 651 (-13.89%)
Mutual labels:  dependency-injection
Rude Java
Java Practice Projects. 以Java语言为主的各种项目实践,涵盖各个业务、各个功能,并附上高质量文章讲解,其中一些甚至可以单开一个仓库。让你再也不用寻找各种框架demo、项目脚手架。
Stars: ✭ 583 (-22.88%)
Mutual labels:  design-patterns
Cp Ddd Framework
A lightweight flexible development framework for complex business architecture with full ecosystem!轻量级业务中台开发框架,中台架构的顶层设计和完整解决方案!
Stars: ✭ 566 (-25.13%)
Mutual labels:  design-patterns
Rails Handbook
Describing the development process used by the Infinum Rails Team.
Stars: ✭ 738 (-2.38%)
Mutual labels:  design-patterns
Opulence
A simple, secure, and scalable PHP application framework
Stars: ✭ 723 (-4.37%)
Mutual labels:  dependency-injection
Rest Api Design Guide
NBB's REST-ish API Design Guide
Stars: ✭ 643 (-14.95%)
Mutual labels:  design-patterns
Coordinator
Implementation of Coordinators app design pattern.
Stars: ✭ 616 (-18.52%)
Mutual labels:  design-patterns
Container
Small but powerful dependency injection container
Stars: ✭ 674 (-10.85%)
Mutual labels:  dependency-injection
Frint
Modular JavaScript framework for building scalable and reactive applications
Stars: ✭ 608 (-19.58%)
Mutual labels:  dependency-injection
Ltupatternfactory
Lambda the ultimate Pattern Factory: FP, Haskell, Typeclassopedia vs Software Design Patterns
Stars: ✭ 735 (-2.78%)
Mutual labels:  design-patterns
Javamtp
《Java多线程编程实战指南(设计模式篇)》源码
Stars: ✭ 575 (-23.94%)
Mutual labels:  design-patterns
Wire
Compile-time Dependency Injection for Go
Stars: ✭ 7,091 (+837.96%)
Mutual labels:  dependency-injection
Design Patterns Php
Design Pattern Examples in PHP
Stars: ✭ 682 (-9.79%)
Mutual labels:  design-patterns

Boost Licence Version Build Status Build Status Coveralls Github Issues 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
    $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

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 
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


Disclaimer [Boost::ext].DI is not an official Boost library.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].