From 0c084a43b9c22e22e49f0913eed9db086dcd20d1 Mon Sep 17 00:00:00 2001 From: Mike Bierlee Date: Tue, 20 May 2014 21:54:11 +0200 Subject: [PATCH] Add scope to registration, check for unset scope --- source/poodinis/container.d | 2 +- source/poodinis/registration.d | 21 +++++++++++++++++++++ test/poodinis/registrationtest.d | 15 +++++++++++++++ 3 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 test/poodinis/registrationtest.d diff --git a/source/poodinis/container.d b/source/poodinis/container.d index 3e343d9..c9f61dc 100644 --- a/source/poodinis/container.d +++ b/source/poodinis/container.d @@ -44,7 +44,7 @@ class Container { checkValidity!(InterfaceType)(registeredType, instantiatableType); } - Registration newRegistration = { registeredType, instantiatableType }; + Registration newRegistration = { registeredType, instantiatableType, new NullScope() }; registrations[newRegistration.registeredType] = newRegistration; return newRegistration; } diff --git a/source/poodinis/registration.d b/source/poodinis/registration.d index dc90077..ce14803 100644 --- a/source/poodinis/registration.d +++ b/source/poodinis/registration.d @@ -3,8 +3,29 @@ module poodinis.registration; struct Registration { TypeInfo registeredType = null; TypeInfo_Class instantiatableType = null; + RegistrationScope registationScope = null; public Object getInstance() { + if (registationScope is null) { + throw new NoScopeDefinedException(registeredType); + } + return instantiatableType.create(); } } + +class NoScopeDefinedException : Exception { + this(TypeInfo type) { + super("No scope defined for registration of type " ~ type.toString()); + } +} + +interface RegistrationScope { + public Object getInstance(); +} + +class NullScope : RegistrationScope { + public Object getInstance() { + return null; + } +} diff --git a/test/poodinis/registrationtest.d b/test/poodinis/registrationtest.d new file mode 100644 index 0000000..2ef5e69 --- /dev/null +++ b/test/poodinis/registrationtest.d @@ -0,0 +1,15 @@ +import poodinis.registration; + +import std.exception; + +version(unittest) { + class TestType { + } + + // Test getting instance without scope defined throws exception + unittest { + Registration registration = Registration(); + registration.registeredType = typeid(TestType); + assertThrown!(NoScopeDefinedException)(registration.getInstance()); + } +} \ No newline at end of file