diff --git a/source/poodinis/container.d b/source/poodinis/container.d index c0ac08f..fb5622d 100644 --- a/source/poodinis/container.d +++ b/source/poodinis/container.d @@ -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; + } } diff --git a/test/poodinis/containertest.d b/test/poodinis/containertest.d index ced8bf6..d3a1569 100644 --- a/test/poodinis/containertest.d +++ b/test/poodinis/containertest.d @@ -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"); + } + } \ No newline at end of file