mirror of
https://github.com/mbierlee/poodinis.git
synced 2024-11-15 04:04:01 +01:00
Add registration linking
This commit is contained in:
parent
fe6a62104e
commit
eb05f7702f
|
@ -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 {
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue