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