Giter VIP home page Giter VIP logo

7bitdi's Introduction

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.