From b18b6ed49c1c1cd7a004004a29239e949c093aff Mon Sep 17 00:00:00 2001 From: Mike Bierlee Date: Tue, 20 May 2014 00:26:53 +0200 Subject: [PATCH] Add global singleton for simplified use of container --- source/poodinis/container.d | 10 +++++++++- test/poodinis/containertest.d | 7 +++++++ 2 files changed, 16 insertions(+), 1 deletion(-) 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