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()));
|
||||
}
|
||||
|
||||
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) {
|
||||
|
|
|
@ -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.");
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue