Add registration linking

This commit is contained in:
Mike Bierlee 2015-05-03 00:46:51 +02:00
parent fe6a62104e
commit eb05f7702f
2 changed files with 254 additions and 229 deletions

View file

@ -22,6 +22,7 @@ class Registration {
TypeInfo registeredType = null;
TypeInfo_Class instantiatableType = null;
CreationScope registationScope = null;
private Registration linkedRegistration;
this(TypeInfo registeredType, TypeInfo_Class instantiatableType) {
this.registeredType = registeredType;
@ -29,12 +30,22 @@ class Registration {
}
public Object getInstance(InstantiationContext context = new InstantiationContext()) {
if (linkedRegistration !is null) {
return linkedRegistration.getInstance(context);
}
if (registationScope is null) {
throw new NoScopeDefinedException(registeredType);
}
return registationScope.getInstance();
}
public Registration linkTo(Registration registration) {
this.linkedRegistration = registration;
return this;
}
}
class NoScopeDefinedException : Exception {

View file

@ -10,8 +10,11 @@ import poodinis.registration;
import std.exception;
version(unittest) {
class TestType {
}
class TestType {}
interface TestInterface {}
class TestImplementation : TestInterface {}
// Test getting instance without scope defined throws exception
unittest {
@ -77,4 +80,15 @@ version(unittest) {
assert(registration is chainedRegistration, "Registration returned by scope setting is not the same as the registration being set");
}
// Test linking registrations
unittest {
Registration firstRegistration = new Registration(typeid(TestInterface), typeid(TestImplementation)).singleInstance();
Registration secondRegistration = new Registration(typeid(TestImplementation), typeid(TestImplementation)).singleInstance().linkTo(firstRegistration);
auto firstInstance = firstRegistration.getInstance();
auto secondInstance = secondRegistration.getInstance();
assert(firstInstance is secondInstance);
}
}