Giter Club home page Giter Club logo

injector's Introduction

injector

Build Status
injector is a header-only, tiny and easy to use library for dependency injection written in C++17.

Code Examples

Basic Usage

#include <injector/injector.hpp>

#include <string>
#include <iostream>
#include <fstream>

struct logger {
    virtual ~logger() = default;
    virtual void log(const std::string& message) = 0;
};

struct stdout_logger : public logger {
    void log(const std::string& message) override {
        std::cout << message << std::endl;
    }
};

struct file_logger : public logger {
    void log(const std::string& message) override {
        std::ofstream("log.txt", std::ios::app) << message << std::endl;
    }
};

int main(int argc, char* argv[]) {
    di::dependency_container container;

    container.install<logger, stdout_logger>(di::dependency_lifetime::singleton);

    auto logger = container.get<::logger>();

    logger->log("Hello World!");
}

Dependency Injection

#include <injector/injector.hpp>

#include <string>
#include <iostream>
#include <fstream>

struct logger {
    void log(const std::string& message) {
        std::cout << message << std::endl;
    }
};

struct important_service  {
    important_service(std::shared_ptr<logger> logger)
        : _logger(logger) {
    }

    void do_something_important() {
        _logger->log("do_something_important()");
    }

    std::shared_ptr<logger> _logger;
};


int main(int argc, char* argv[]) {
    di::dependency_container container;

    container.install<logger>(di::dependency_lifetime::singleton);
    container.install<important_service>(di::dependency_lifetime::singleton);

    auto service = container.get<important_service>();

    service->do_something_important();
}

Resolver

#include <injector/injector.hpp>

#include <string>
#include <iostream>
#include <fstream>

struct logger {
    static std::shared_ptr<logger> resolver(di::dependency_container& container);
    virtual ~logger() = default;
    virtual void log(const std::string& message) = 0;
};

struct stdout_logger : public logger {
    void log(const std::string& message) override {
        std::cout << message << std::endl;
    }
};

std::shared_ptr<logger> logger::resolver(di::dependency_container& container) {
    return container.resolve<stdout_logger>();
}

int main(int argc, char* argv[]) {
    di::dependency_container container;

    container.install<logger>(di::dependency_lifetime::singleton, &logger::resolver);

    auto logger = container.get<::logger>();

    logger->log("Hello World!");
}

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.