mirror of
https://github.com/mbierlee/poodinis.git
synced 2024-11-15 04:04:01 +01:00
Refactor getting existing registration to separate function
This commit is contained in:
parent
1123aa0ef9
commit
89d82efb11
|
@ -106,12 +106,9 @@ class DependencyContainer {
|
||||||
writeln(format("DEBUG: Register type %s (as %s)", concreteType.toString(), registeredType.toString()));
|
writeln(format("DEBUG: Register type %s (as %s)", concreteType.toString(), registeredType.toString()));
|
||||||
}
|
}
|
||||||
|
|
||||||
auto existingCandidates = registeredType in registrations;
|
auto existingRegistration = getExistingRegistration(registeredType, concreteType);
|
||||||
if (existingCandidates) {
|
if (existingRegistration) {
|
||||||
auto existingRegistration = getRegistration(*existingCandidates, concreteType);
|
return existingRegistration;
|
||||||
if (existingRegistration) {
|
|
||||||
return existingRegistration;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
AutowiredRegistration!ConcreteType newRegistration = new AutowiredRegistration!ConcreteType(registeredType, this);
|
AutowiredRegistration!ConcreteType newRegistration = new AutowiredRegistration!ConcreteType(registeredType, this);
|
||||||
|
@ -120,6 +117,15 @@ class DependencyContainer {
|
||||||
return newRegistration;
|
return newRegistration;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private Registration getExistingRegistration(TypeInfo registrationType, TypeInfo qualifierType) {
|
||||||
|
auto existingCandidates = registrationType in registrations;
|
||||||
|
if (existingCandidates) {
|
||||||
|
return getRegistration(*existingCandidates, qualifierType);
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
private Registration getRegistration(Registration[] candidates, TypeInfo concreteType) {
|
private Registration getRegistration(Registration[] candidates, TypeInfo concreteType) {
|
||||||
foreach(existingRegistration ; candidates) {
|
foreach(existingRegistration ; candidates) {
|
||||||
if (existingRegistration.instantiatableType == concreteType) {
|
if (existingRegistration.instantiatableType == concreteType) {
|
||||||
|
|
|
@ -333,4 +333,24 @@ version(unittest) {
|
||||||
|
|
||||||
assert(!(instance is null), "Container failed to autowire member by interface");
|
assert(!(instance is null), "Container failed to autowire member by interface");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Register existing registration
|
||||||
|
unittest {
|
||||||
|
auto container = new DependencyContainer();
|
||||||
|
|
||||||
|
auto firstRegistration = container.register!TestClass;
|
||||||
|
auto secondRegistration = container.register!TestClass;
|
||||||
|
|
||||||
|
assert(firstRegistration is secondRegistration, "Registering the same registration twice registers the dependencies twice.");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Register existing registration by supertype
|
||||||
|
unittest {
|
||||||
|
auto container = new DependencyContainer();
|
||||||
|
|
||||||
|
auto firstRegistration = container.register!(TestInterface, TestClass);
|
||||||
|
auto secondRegistration = container.register!(TestInterface, TestClass);
|
||||||
|
|
||||||
|
assert(firstRegistration is secondRegistration, "Registering the same registration by super type twice registers the dependencies twice.");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue