|
21 | 21 |
|
22 | 22 | <br />
|
23 | 23 |
|
24 |
| -### Built With |
| 24 | +## Built With |
25 | 25 |
|
26 | 26 | - [Google Test](https://github.com/google/googletest)
|
27 | 27 |
|
| 28 | +## Supported Platforms |
| 29 | + |
| 30 | +7bitDI requires client code and compiler compatible with the C++17 standard or newer. |
| 31 | + |
| 32 | +The library is officially supported on the following platforms: |
| 33 | + |
| 34 | +**Operating systems:** |
| 35 | + |
| 36 | +* Linux |
| 37 | +* macOS |
| 38 | +* Windows |
| 39 | + |
| 40 | +**Compilers:** |
| 41 | + |
| 42 | +* gcc 7.0+ |
| 43 | +* clang 6.0+ |
| 44 | +* MSVC 2015+ |
| 45 | + |
| 46 | +If you notice any problems/bugs, please file an issue on the repository GitHub Issue Tracker. Pull requests containing |
| 47 | +fixes are welcome! |
| 48 | + |
| 49 | +## Installation |
| 50 | + |
| 51 | +**There are a few ways of installation:** |
| 52 | + |
| 53 | +### 1. Using Cmake fetch content api - Recommended |
| 54 | + |
| 55 | +Update CMakeLists.txt file with following code |
| 56 | + |
| 57 | +```cmake |
| 58 | +include(FetchContent) |
| 59 | +FetchContent_Declare( |
| 60 | + 7bitDI |
| 61 | + GIT_REPOSITORY https://github.com/7bitcoder/7bitDI.git |
| 62 | + GIT_TAG 86228173f14f449dde88a84c549474ba43c2fd25 # proper release tag for example 1.0.0 |
| 63 | +) |
| 64 | +FetchContent_MakeAvailable(7bitDI) |
| 65 | +``` |
| 66 | + |
| 67 | +### 2. Using Conan.io package manager |
| 68 | + |
| 69 | +Download and install A [Conan](https://conan.io/), and create conanfile.txt in the root of your project for example: |
| 70 | + |
| 71 | + ```txt |
| 72 | + [requires] |
| 73 | + 7bitdi/2.0.0 |
| 74 | + ``` |
| 75 | + |
| 76 | +change the version to newer if available, then run the command: |
| 77 | + |
| 78 | + ```sh |
| 79 | + conan install . --output-folder=build --build=missing |
| 80 | + ``` |
| 81 | + |
| 82 | +### 3. Header only |
| 83 | + |
| 84 | +Download source code from the most recent release, |
| 85 | +copy include folder into your project location, |
| 86 | +for example copy into the '/SevenBitDI' folder. |
| 87 | +Include this folder into the project, with [Cmake](https://cmake.org/), u can use: |
| 88 | + |
| 89 | + ```cmake |
| 90 | + include_directories(/SevenBitDI/Include) |
| 91 | + ``` |
| 92 | + |
| 93 | +### 4. Header only - Single file |
| 94 | + |
| 95 | +Download SevenBitDI.hpp header file from the most recent release, |
| 96 | +copy this file into your project location and include it. |
| 97 | + |
| 98 | +### 5. Building library as Static/Shared |
| 99 | + |
| 100 | +Download source code from the most recent release, build or install the project using [Cmake](https://cmake.org/)_, |
| 101 | +for more details see the Building Library guide |
| 102 | +in [Documentation](https://7bitdi.readthedocs.io/en/latest/getting-started.html). |
| 103 | + |
| 104 | +### The library relies on two core classes: |
| 105 | + |
| 106 | +* ServiceCollection: class is responsible for registering services and building service provider |
| 107 | +* ServiceProvider: class is responsible for delivering real services and managing its lifetime |
| 108 | + |
| 109 | +## Injection Rules |
| 110 | + |
| 111 | +The dependency injection mechanism relies heavily on template metaprogramming and it has some limitations. |
| 112 | + |
| 113 | +### General |
| 114 | + |
| 115 | +* Only one constructor should be defined for each instance implementation |
| 116 | +* If the service is registered with interface and implementation, the interface should have a virtual destructor |
| 117 | +* If multiple services are registered by the same interface, all should have the same lifetime (the build method will |
| 118 | + throw an exception) |
| 119 | +* Only one service implementation can be registered (the build method will throw an exception) |
| 120 | + |
| 121 | +### Injecting Services |
| 122 | + |
| 123 | +* Services cannot be injected by value: (T) |
| 124 | +* Singleton/scoped services can be injected using one of: |
| 125 | + * References: (T&) |
| 126 | + * Const references: (const T&) |
| 127 | + * Pointers: (T*) |
| 128 | + * Const pointer: (T* const) |
| 129 | + * Pointer to const object: (const T*) |
| 130 | + * Const pointer to const object: (const T* const) |
| 131 | +* Transient services can be injected using std::unique_ptr: (unique_ptr<T>) or directly T if object is movable or |
| 132 | + copyable |
| 133 | +* Multiple services implementing specified interface can be injected using std::vector: |
| 134 | + * Transient (std::vector<std::unique_ptr<T>>) |
| 135 | + * Singleton/scoped (std::vector<T*>) |
| 136 | + |
| 137 | +### Injection Table |
| 138 | + |
| 139 | +| Constructor param type | ServiceProvider method used | |
| 140 | +|---------------------------------|------------------------------------| |
| 141 | +| T - if movable or copyable | provider.createServiceInPlace<T>() | |
| 142 | +| std::unique_ptr<T> | provider.createService<T>() | |
| 143 | +| T& | provider.getService<T>() | |
| 144 | +| T* | provider.tryGetService<T>() | |
| 145 | +| std::vector<T*> | provider.getServices<T>() | |
| 146 | +| std::vector<std::unique_ptr<T>> | provider.createServices<T>() | |
| 147 | + |
28 | 148 | ### Sample Usage
|
29 | 149 |
|
30 | 150 | ```cpp
|
|
0 commit comments