diff --git a/source/poodinis/container.d b/source/poodinis/container.d index b6e57d8..de41255 100644 --- a/source/poodinis/container.d +++ b/source/poodinis/container.d @@ -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) { diff --git a/test/poodinis/containertest.d b/test/poodinis/containertest.d index ba0300e..8c7b966 100644 --- a/test/poodinis/containertest.d +++ b/test/poodinis/containertest.d @@ -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."); + } }