Add global singleton for simplified use of container

This commit is contained in:
Mike Bierlee 2014-05-20 00:26:53 +02:00
parent 27a50a53cf
commit b18b6ed49c
2 changed files with 16 additions and 1 deletions

View file

@ -25,9 +25,10 @@ class ResolveException : Exception {
}
class Container {
private static Container instance;
private Registration[TypeInfo] registrations;
private bool _typeValidityCheckEnabled = true;
@property public void typeValidityCheckEnabled(bool enabled) {
@ -75,4 +76,11 @@ class Container {
public void clearRegistrations() {
registrations.clear();
}
public static Container getInstance() {
if (instance is null) {
instance = new Container();
}
return instance;
}
}

View file

@ -70,4 +70,11 @@ version(unittest) {
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");
}
}