mirror of
https://github.com/mbierlee/poodinis.git
synced 2024-11-15 04:04:01 +01:00
Add scope to registration, check for unset scope
This commit is contained in:
parent
ac077c24d7
commit
0c084a43b9
|
@ -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;
|
||||||
}
|
}
|
||||||
|
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
15
test/poodinis/registrationtest.d
Normal file
15
test/poodinis/registrationtest.d
Normal 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());
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue