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
4
u/voidstarcpp Jun 10 '26
I don't understand what this is even attempting to do. Is it providing a global object registry with runtime composition or factory helpers?
If Window is a global, I don't see why I wouldn't just make it my app global. If it's owned by the Engine, it can be a
unique_ptr<Window>. And if you need to parameterize construction, you can have ag_window_factory.51
u/mungaihaha Jun 10 '26
How do people get anything done programming like this?
24
u/ozyx7 Jun 09 '26
As an aside, I really hate the term "dependency injection". "Injection" to me seems like something that is being done to some object without it being complicit. For example, contrast to DLL injection or other code injection attacks.
"Dependency injection" is a bad term for what, IMO should be called dependency parameterization. "Dependency inversion" is slightly better but still makes it seem fancier than it actually is.
1
u/Brodeon Jun 11 '26
DI is like injecting real dependencies. Once you taste it, you never want to live without this. In .NET it works though reflection, is the same mechanism used here? Unfortunately I don't have time to check your codebase
3
u/NoSpite4410 Jun 11 '26
Is this a way to make code that you can read but have no idea what it does?
1
u/Inujel Jun 10 '26
Thank you so much for this contribution. I love DI and always felt that the C++ ecosystem is lacking in that regard. I regularly use fruit even though it's hardly maintained anymore. How does your library compare to fruit?
1
u/gracicot Jun 10 '26
Interesting. I've been working on a C++20 rewrite of kangaru that completely remove the
xyServicetypes, and focuses on reflecting on constructor instead of listing dependencies, but it's nice to see a different take on the approach I choose at the time.There's one aspect that I like about the interface you're presenting to the user here, is that there seem to be one dipp::injected type that you configure so that services behave like you want, instead of the multiple types approach kangaru took for named (or keyed) services. I think this might be simpler than what I did at the time of kangaru 3.x and 4.x
-1
u/invisible-green-idea Jun 12 '26
Thanksfully, this is not idiomatic C++, this is an attempt to transplant idioms from the Java and C# world. No thanks.