Add scope to registration, check for unset scope

This commit is contained in:
Mike Bierlee 2014-05-20 21:54:11 +02:00
parent ac077c24d7
commit 0c084a43b9
3 changed files with 37 additions and 1 deletions

View file

@ -44,7 +44,7 @@ class Container {
checkValidity!(InterfaceType)(registeredType, instantiatableType); checkValidity!(InterfaceType)(registeredType, instantiatableType);
} }
Registration newRegistration = { registeredType, instantiatableType }; Registration newRegistration = { registeredType, instantiatableType, new NullScope() };
registrations[newRegistration.registeredType] = newRegistration; registrations[newRegistration.registeredType] = newRegistration;
return newRegistration; return newRegistration;
} }

View file

@ -3,8 +3,29 @@ module poodinis.registration;
struct Registration { struct Registration {
TypeInfo registeredType = null; TypeInfo registeredType = null;
TypeInfo_Class instantiatableType = null; TypeInfo_Class instantiatableType = null;
RegistrationScope registationScope = null;
public Object getInstance() { public Object getInstance() {
if (registationScope is null) {
throw new NoScopeDefinedException(registeredType);
}
return instantiatableType.create(); 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;
}
}

View file

@ -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());
}
}