A lightweight Dependency Injection (DI) framework with focus on simplicity.
- Inversion of Control (IoC).
- Convention over configuration.
- Injects dependencies via constructor parameters.
- Supports structs as well (… as classes and interfaces).
- No clutter – this library is a single, readily comprehensible file.
- No external dependencies. (*Only the D standard library is used.)
When using DUB, it’s as simple as running dub add oceandrift-di
.
See in-code documentation for details. Or check out the pre-rendered copy on dpldocs.
class Dependency {}
class Foo {
this(Dependency d) {
// …
}
}
// Bootstrap the DI framework.
// Then let it resolve the whole dependency tree of `Foo`
// and construct dependencies as needed.
auto di = new DI();
Foo foo = di.resolve!Foo();
class Foo {
// Mark fields as @dependency.
private @dependency Dependency1 d1;
private @dependency Dependency2 d2;
// Generate a constructor that assigns all dependencies.
mixin DIConstructor;
// The generated constructor corresponds roughly to this code:
/*
public this(
Dependency1 d1,
Dependency2 d2,
) {
this.d1 = d1;
this.d2 = d2;
}
*/
}