Getting Started¶
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 7bitDI GitHub Issue Tracker. Pull requests containing fixes are welcome!
Installation¶
There are a few ways of installation:
- 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)
- Using Conan.io package manager
Download and install 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
- Header only
Download source code from the most recent release, copy include folder into your project location, for example copy into the ‘/SevenBitDI’ folder. Include this folder into the project, with CMake, u can use:
include_directories(/SevenBitDI/Include)
- Header only - Single file
Download SevenBitDI.hpp header file from the most recent release, copy this file into your project location and include it.
- 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.
Example Usage¶
The next chapters will in detail, step by step explain what is going on in this example
#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 IServiceExecutor
{
[[nodiscard]] virtual std::string execute() const = 0;
virtual ~IServiceExecutor() = default;
};
struct ServiceA final : IServiceA
{
std::string actionA() override { return "actionA"; }
};
struct ServiceB final : IServiceB
{
std::string actionB() override { return "actionB"; }
};
class ServiceExecutor final : public IServiceExecutor
{
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 override
{
return _serviceA.actionA() + ", " + _serviceB->actionB() + " executed.";
}
};
int main()
{
ServiceProvider provider = ServiceCollection{}
.addSingleton<IServiceA, ServiceA>()
.addTransient<IServiceB, ServiceB>()
.addScoped<IServiceExecutor, ServiceExecutor>()
.buildServiceProvider();
const auto &executor = provider.getService<IServiceExecutor>();
std::cout << executor.execute();
return 0;
}
actionA, actionB executed.