Skip to content

7bitcoder/7bitDI

Repository files navigation

DevCI Linux Windows MacOs Conan Center

logo

C++17 simple dependency injection library!

7bitDI is a simple C++ dependency injection library, designed to be as easy to use as possible, the main inspiration was the asp net core dependency injection system.


Main Features

  • Implementation separation
  • Multiple implementations
  • Keyed services
  • Service aliases
  • Thread safe
  • Strong destruction order

Supported Platforms

7bitDI requires client code and compiler compatible with the C++17 standard or newer.

The library is officially supported on the following platforms:

Operating systems:

  • Linux
  • macOS
  • Windows

Compilers:

  • gcc 7.0+
  • clang 6.0+
  • MSVC 2015+

If you notice any problems/bugs, please file an issue on the repository GitHub Issue Tracker. Pull requests containing fixes are welcome!

Documentation

https://7bitDI.readthedocs.io

Installation

There are a few ways of installation:

1. Using Cmake fetch content API - Recommended

Update CMakeLists.txt file with the following code

include(FetchContent)
FetchContent_Declare(
        7bitDI
        GIT_REPOSITORY https://github.com/7bitcoder/7bitDI.git
        GIT_TAG v3.3.0
)
FetchContent_MakeAvailable(7bitDI)

target_link_libraries(Target 7bitDI::7bitDI)

2. Using Conan.io package manager

Download and install A Conan, and create conanfile.txt in the root of your project for example:

[requires]
7bitdi/3.3.0

change the version to newer if available, then run the command:

conan install . --output-folder=build --build=missing

Follow in detailed instructions available at Conan Tutorial

3. Header only

Download the source code from the most recent release, copy include folder into your project location.

include_directories(/SevenBitDI/Include)

4. Header only - Single file

Download SevenBitDI.hpp header file from the most recent release, copy this file into your project location and include it.

5. Building library as Static/Shared

Download source code from the most recent release, build or install the project using Cmake, for more details see the Building Library guide in Documentation.

Injection Rules

The library relies on two core classes:

  • ServiceCollection: class is responsible for registering services and building service provider
  • ServiceProvider: class is responsible for delivering real services and managing its lifetime

The dependency injection mechanism relies heavily on template metaprogramming and it has some limitations.

General

  • Only one constructor should be defined for each instance implementation
  • If the service is registered with interface and implementation, the interface should have a virtual destructor
  • If multiple services are registered by the same interface, all should have the same lifetime (the build method will throw an exception)
  • Only one service implementation can be registered (the build method will throw an exception)

Service lifetime

Service can be registered as a singleton, scoped, or transient.

  • Singleton: service provider will create only one instance of this service (accessible via the getService method)
  • Scoped: instance provider will create only one instance of this instance for each scope (accessible via the getService method)
  • Transient: services are always unique, a new service is provided every time it is requested (accessible via createService or createInstanceInPlace method)

Injecting Services

  • Singleton/scoped services can be injected using one of:
    • References: (T&)
    • Pointers: (T*)
  • Transient services can be injected using one of:
    • std::unique_ptr: (unique_ptr< T>)
    • In place object if the type is movable or copyable: T
  • Multiple services implementing the same interface can be injected using std::vector:
    • Transient (std::vector<std::unique_ptr< T>>)
    • Singleton/scoped (std::vector<T*>)

Injection Table

Constructor param type ServiceProvider method used
T - if movable or copyable provider.createServiceInPlace< T>()
std::unique_ptr< T> provider.createService< T>()
T& provider.getService< T>()
T* provider.tryGetService< T>()
std::vector<T*> provider.getServices< T>()
std::vector<std::unique_ptr< T>> provider.createServices< T>()

Sample Usage

#include <SevenBit/DI.hpp>
#include <iostream>

using namespace sb::di;

struct IServiceA
{
    virtual std::string actionA() = 0;

    virtual ~IServiceA() = default;
};

struct IServiceB
{
    virtual std::string actionB() = 0;

    virtual ~IServiceB() = default;
};

struct ServiceA final : IServiceA
{
    std::string actionA() override { return "actionA"; }
};

struct ServiceB final : IServiceB
{
    std::string actionB() override { return "actionB"; }
};

class ServiceExecutor
{
    IServiceA &_serviceA;
    std::unique_ptr<IServiceB> _serviceB;

  public:
    ServiceExecutor(IServiceA &serviceA, std::unique_ptr<IServiceB> serviceB)
        : _serviceA(serviceA), _serviceB(std::move(serviceB))
    {
    }

    [[nodiscard]] std::string execute() const
    {
        return _serviceA.actionA() + ", " + _serviceB->actionB() + " executed.";
    }
};
int main()
{
    ServiceProvider provider = ServiceCollection{}
                                   .addSingleton<IServiceA, ServiceA>()
                                   .addTransient<IServiceB, ServiceB>()
                                   .addScoped<ServiceExecutor>()
                                   .buildServiceProvider();

    const auto &executor = provider.getService<ServiceExecutor>();

    std::cout << executor.execute();
    return 0;
}

Output

actionA, actionB executed.

More examples and tutorials are available on the Documentation & Examples page

Built With

@7bitcoder Sylwester Dawida 2023