mirror of
https://github.com/mbierlee/poodinis.git
synced 2024-11-15 04:04:01 +01:00
Add global singleton for simplified use of container
This commit is contained in:
parent
27a50a53cf
commit
b18b6ed49c
|
@ -26,8 +26,9 @@ class ResolveException : Exception {
|
||||||
|
|
||||||
class Container {
|
class Container {
|
||||||
|
|
||||||
private Registration[TypeInfo] registrations;
|
private static Container instance;
|
||||||
|
|
||||||
|
private Registration[TypeInfo] registrations;
|
||||||
private bool _typeValidityCheckEnabled = true;
|
private bool _typeValidityCheckEnabled = true;
|
||||||
|
|
||||||
@property public void typeValidityCheckEnabled(bool enabled) {
|
@property public void typeValidityCheckEnabled(bool enabled) {
|
||||||
|
@ -75,4 +76,11 @@ class Container {
|
||||||
public void clearRegistrations() {
|
public void clearRegistrations() {
|
||||||
registrations.clear();
|
registrations.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static Container getInstance() {
|
||||||
|
if (instance is null) {
|
||||||
|
instance = new Container();
|
||||||
|
}
|
||||||
|
return instance;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -70,4 +70,11 @@ version(unittest) {
|
||||||
assertThrown!ResolveException(container.resolve!(TestClass)(), "Resolving cleared type does not fail");
|
assertThrown!ResolveException(container.resolve!(TestClass)(), "Resolving cleared type does not fail");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
unittest {
|
||||||
|
// Test get singleton of container
|
||||||
|
auto instance1 = Container.getInstance();
|
||||||
|
auto instance2 = Container.getInstance();
|
||||||
|
assert(instance1 is instance2, "getInstance does not return the same instance");
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
Loading…
Reference in a new issue