mirror of
https://github.com/mbierlee/poodinis.git
synced 2025-02-23 02:22:41 +01:00

Some checks failed
CI / dmd-2.086.1 on ubuntu-latest (push) Failing after 4s
CI / dmd-2.087.1 on ubuntu-latest (push) Failing after 3s
CI / dmd-2.088.1 on ubuntu-latest (push) Failing after 4s
CI / dmd-2.089.1 on ubuntu-latest (push) Failing after 3s
CI / dmd-2.090.1 on ubuntu-latest (push) Failing after 3s
CI / dmd-2.091.1 on ubuntu-latest (push) Failing after 3s
CI / dmd-2.092.1 on ubuntu-latest (push) Failing after 3s
CI / dmd-2.093.1 on ubuntu-latest (push) Failing after 3s
CI / dmd-2.094.2 on ubuntu-latest (push) Failing after 3s
CI / dmd-2.095.1 on ubuntu-latest (push) Failing after 3s
CI / dmd-2.096.1 on ubuntu-latest (push) Failing after 3s
CI / dmd-2.097.2 on ubuntu-latest (push) Failing after 3s
CI / dmd-2.098.1 on ubuntu-latest (push) Failing after 3s
CI / dmd-2.099.1 on ubuntu-latest (push) Failing after 3s
CI / dmd-2.100.2 on ubuntu-latest (push) Failing after 3s
CI / dmd-2.101.2 on ubuntu-latest (push) Failing after 3s
CI / dmd-2.102.2 on ubuntu-latest (push) Failing after 3s
CI / dmd-latest on ubuntu-latest (push) Failing after 3s
CI / ldc-1.16.0 on ubuntu-latest (push) Failing after 3s
CI / ldc-1.17.0 on ubuntu-latest (push) Failing after 3s
CI / ldc-1.18.0 on ubuntu-latest (push) Failing after 3s
CI / ldc-1.19.0 on ubuntu-latest (push) Failing after 3s
CI / ldc-1.20.1 on ubuntu-latest (push) Failing after 3s
CI / ldc-1.21.0 on ubuntu-latest (push) Failing after 3s
CI / ldc-1.22.0 on ubuntu-latest (push) Failing after 2s
CI / ldc-1.23.0 on ubuntu-latest (push) Failing after 3s
CI / ldc-1.24.0 on ubuntu-latest (push) Failing after 3s
CI / ldc-1.26.0 on ubuntu-latest (push) Failing after 2s
CI / ldc-1.27.1 on ubuntu-latest (push) Failing after 3s
CI / ldc-1.28.1 on ubuntu-latest (push) Failing after 3s
CI / ldc-1.29.0 on ubuntu-latest (push) Failing after 3s
CI / ldc-1.30.0 on ubuntu-latest (push) Failing after 3s
CI / ldc-1.31.0 on ubuntu-latest (push) Failing after 4s
CI / ldc-1.32.0 on ubuntu-latest (push) Failing after 3s
CI / ldc-latest on ubuntu-latest (push) Failing after 3s
CI / GDC on Ubuntu (push) Failing after 3s
CI / dmd-latest on windows-latest (push) Has been cancelled
CI / ldc-latest on windows-latest (push) Has been cancelled
55 lines
1.5 KiB
D
55 lines
1.5 KiB
D
/**
|
|
* Poodinis Dependency Injection Framework
|
|
* Copyright 2014-2025 Mike Bierlee
|
|
* This software is licensed under the terms of the MIT license.
|
|
* The full terms of the license can be found in the LICENSE file.
|
|
*/
|
|
|
|
import poodinis;
|
|
|
|
import std.stdio;
|
|
|
|
class ADependency {
|
|
@PostConstruct void postConstructor() {
|
|
writeln("The dependency is created.");
|
|
}
|
|
|
|
void callMe() {
|
|
writeln("The dependency was called.");
|
|
}
|
|
}
|
|
|
|
class AClass {
|
|
@Inject ADependency dependency; // Dependencies are autowired before the post-constructor is called.
|
|
|
|
@PostConstruct void postConstructor() {
|
|
writeln("The class is created.");
|
|
if (dependency !is null) {
|
|
writeln("The dependency is autowired.");
|
|
} else {
|
|
writeln("The dependency was NOT autowired.");
|
|
}
|
|
}
|
|
|
|
@PreDestroy void preDestructor() {
|
|
writeln("The class is no longer registered with the container.");
|
|
}
|
|
}
|
|
|
|
void main() {
|
|
auto container = new shared DependencyContainer();
|
|
container.register!(ADependency).onConstructed((Object obj) {
|
|
writeln("ADependency constructed");
|
|
});
|
|
|
|
container.register!(AClass).onConstructed((Object obj) {
|
|
writeln("AClass constructed");
|
|
});
|
|
|
|
auto instance = container.resolve!AClass; // Will cause the post constructor to be called.
|
|
container.removeRegistration!AClass; // Will cause the pre destructor to be called.
|
|
|
|
// The instance won't be destroyed by the container and as long as there are references to it,
|
|
// it will not be collected by the garbage collector either.
|
|
instance.dependency.callMe();
|
|
}
|