TGViewer
C++ - Reddit C++ - Reddit @r_cpp · 230 subscribers
Post #25397 17
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 (using `dipp::service_provider`).
Extensible and flexible to define your own service storage, (dipp::service_provider, dipp::service_collection ... are templated storage, defaults to std::map of dipp::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::servicecollection 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](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](https://codeberg.org/JassJam/dipp/src/branch/main/tests)
Somewhat used through out my (incomplete) toy game engine: https://codeberg.org/JassJam/Neko.git

https://redd.it/1u1dyrg
@r_cpp
Wikipedia Dependency injection technique in software engineering
More from @r_cpp
  1. Sep 29, 2026Boost.Graph 1.95 will be C++17 Dear Boost.Graph community, In two release cycles (Boost re…
  2. Sep 26, 2026Token Sequence Injection & Modern Macros: The Most Game-Changing Compile-Time Feature in C…
  3. Sep 25, 2026myStringStream.str("") Considered Harmful Under C++20 I was recently looking at some (rath…
  4. Sep 20, 2026A clever branch free optimization I'm the developer of memlz which is an extremely fast co…
  5. Sep 15, 2026Inside Boost.PolyCollection https://bannalia.blogspot.com/2026/09/inside-boostpolycollecti…
  6. Sep 11, 2026C++26: Standard Library Hardening Experiments https://www.cppstories.com/2026/hardening-ex…
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook →Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 →