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::serviceprovider 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