Refactor getting existing registration to separate function

This commit is contained in:
Mike Bierlee 2015-02-22 19:11:38 +01:00
parent 1123aa0ef9
commit 89d82efb11
2 changed files with 32 additions and 6 deletions

View file

@ -106,12 +106,9 @@ class DependencyContainer {
writeln(format("DEBUG: Register type %s (as %s)", concreteType.toString(), registeredType.toString()));
}
auto existingCandidates = registeredType in registrations;
if (existingCandidates) {
auto existingRegistration = getRegistration(*existingCandidates, concreteType);
if (existingRegistration) {
return existingRegistration;
}
auto existingRegistration = getExistingRegistration(registeredType, concreteType);
if (existingRegistration) {
return existingRegistration;
}
AutowiredRegistration!ConcreteType newRegistration = new AutowiredRegistration!ConcreteType(registeredType, this);
@ -120,6 +117,15 @@ class DependencyContainer {
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) {
foreach(existingRegistration ; candidates) {
if (existingRegistration.instantiatableType == concreteType) {

View file

@ -333,4 +333,24 @@ version(unittest) {
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.");
}
}