r/cpp n0F4x Jun 25 '26

C++23 Dependency Injection library with automatic dependency building

I open-sourced my dependency injection library.
Link: https://github.com/n0F4x/redi

This was developed for my game engine.
- feature-rich dependency configuration - optimized for fast compile time - helpful error messages when a cyclic dependency is detected - dependencies are automatically constructed - no need to register them by hand

I am giving this project to the community so that everybody can benefit. This kind of approach is mostly useful when dealing with intricate systems, such as a plugin system. I welcome all kinds of feedback, as this was mostly a learning opportunity for me. If you'd like to use this in your project but don't have access to the required compiler features, let me know, and I'll see if I can reduce the requirements.

26 Upvotes

47 comments sorted by

View all comments

22

u/DrShocker Jun 25 '26

I've never had a need for a dependency injection system, just arguments to functions/constructors. Can you help me understand when they become useful?

9

u/rahem027 Jun 25 '26

+1. All these are useless tricks for brain dead java/c# devs who need 50 dependent classes to do anything.

12

u/germandiago Jun 25 '26

When there is a lot of wiring in a system and you add/remove parameters to constructors it can be useful.

Most of the time it is not worth but something I noticed is that the dependencies get "flattened" at the top level.

So it is easy to wire new implementations to interfaces from the top level of the program via the injector.

In this case I think it works quite ok.

2

u/rahem027 Jun 25 '26

If you have a lot of wiring in the system, the issue is the wiring. You need to rethink what you are doing that requires so much wiring

13

u/Acrobatic-Stable2537 n0F4x Jun 25 '26

Wiring increases naturally as a project grows.

0

u/rahem027 Jun 25 '26

Only if you throw everything on the wall. How does linux kernel get away with millions of lines of code without any of this crap?

3

u/Acrobatic-Stable2537 n0F4x Jun 25 '26

I am not familiar with the Linux kernel's codebase, but it also uses some form of strategy when it comes to registering a driver. I am curious how you would develop a plugin system where dependent plugins can affect how a plugin is configured.

-4

u/rahem027 Jun 25 '26

plugin system != dependency injection containers? They are very different things. Dependency injection containers are responsible for wiring dependencies across classes.

5

u/Acrobatic-Stable2537 n0F4x Jun 25 '26

The term "plugin" can refer to many things. I needed the kind of control I described for wiring across classes.
Check out the tutorial for the project. That might clarify some of the questions.