C++20/C++23 Dependency Injection
Dependency Injection (DI) is a technique for an object that's being created receive it's dependencies ready for use instead of creating them internally. more about it on the Wiki.
DIPP (Dependency Injection for C++) library aims to be as close to .NET's Microsoft DependencyInjection as possible.
Why is DIPP interesting:
- Non intrusive, you can use it with your existing classes.
- No auto-registration, you must register your services explicitly.
- All services are registered once (using
dipp::service_collection) with specified descriptor (scope lifetime (transient, scoped or singleton), object's backing memory and dependencies of the object) and will be later on consumed (usingdipp::service_provider). - Extensible and flexible to define your own service storage, (
dipp::service_provider,dipp::service_collection... are templated storage, defaults to std::map ofdipp::move_only_any).
DIPP supports two modes, error based return value using Boost.Leaf and exception throwing when attempting to fetch or add a service (check error_handling.cpp for examples).
Similar to .NET, DIPP supports keyed services, as in you can instantiate multiple services of the same type with different keys (check keys.cpp for more examples).
struct Engine
{
Window& window1;
Window& window2;
Engine(Window& window1, Window& window2) :
window1(window1), window2(window2)
{
}
};
// Declare our services
using WindowService1 = dipp::injected<Window, ...>;
using WindowService2 = dipp::injected<Window, ..., dipp::key("UNIQUE")>;
using EngineService = dipp::injected<Engine, ..., dipp::dependency<WindowService1, WindowService2>>;
// Create a collection to hold our services
dipp::service_collection collection;
// add the services to the collection
collection.add<WindowService>();
collection.add<EngineService>();
// create a service provider with the collection
dipp::service_provider services(std::move(collection));
// Fetch services
Engine& engine = services.get<EngineService>();
// both window services shouldn't be the same
assert(&engine.window1 != &engine.window2);Mode info:
- Codeberg: https://codeberg.org/JassJam/dipp.git
- Github: https://github.com/JassJam/dipp.git
- Basic samples in tests https://codeberg.org/JassJam/dipp/src/branch/main/tests
- Somewhat used through out my (incomplete) toy game engine: https://codeberg.org/JassJam/Neko.git
58
Upvotes
View all comments
Show parent comment
9
u/SirClueless Jun 10 '26
That situation adds complexity, but it's because the problem is more complex. You have to handle the late construction of the dependency, and the fact that it is optional whether it even can be constructed at all.
When solving this directly, you can use plain C++ features to store the dependency, like
std::optional<License>orstd::unique_ptr<License>that start out null -- both have simple lifetime requirements.When solving this with DI, you need additional complexity too, don't forget. A typical way is for DI frameworks to support assigning dependencies after construction with setters, which adds lifetime concerns too (what if a method that needs the dependency is called before the dependency is set?). Optional dependencies often are supported too, by taking a different type of argument. Both add the same kinds of complexity as just having an optional container to store the thing directly.
As always, the hard part is designing the rest of your application to work correctly around the fact that
services.get<LicenseService>()doesn't always work. Actually figuring out how to construct the dependency is in many ways the easy part, and doing it directly will always be more flexible because it's just written in normal code instead of requiring special features from the DI framework for every corner case.