mirror of
https://github.com/mbierlee/poodinis.git
synced 2024-11-15 04:04:01 +01:00
Add resolving all registered types of a certain super type
This commit is contained in:
parent
c75fc78900
commit
53fb0d8116
|
@ -265,8 +265,11 @@ synchronized class DependencyContainer {
|
||||||
}
|
}
|
||||||
|
|
||||||
Registration registration = getQualifiedRegistration(resolveType, qualifierType, cast(Registration[]) *candidates);
|
Registration registration = getQualifiedRegistration(resolveType, qualifierType, cast(Registration[]) *candidates);
|
||||||
QualifierType instance;
|
return resolveAutowiredInstance!QualifierType(registration);
|
||||||
|
}
|
||||||
|
|
||||||
|
private QualifierType resolveAutowiredInstance(QualifierType)(Registration registration) {
|
||||||
|
QualifierType instance;
|
||||||
if (!(cast(Registration[]) autowireStack).canFind(registration)) {
|
if (!(cast(Registration[]) autowireStack).canFind(registration)) {
|
||||||
autowireStack ~= cast(shared(Registration)) registration;
|
autowireStack ~= cast(shared(Registration)) registration;
|
||||||
instance = cast(QualifierType) registration.getInstance(new AutowireInstantiationContext());
|
instance = cast(QualifierType) registration.getInstance(new AutowireInstantiationContext());
|
||||||
|
@ -276,10 +279,42 @@ synchronized class DependencyContainer {
|
||||||
autowireContext.autowireInstance = false;
|
autowireContext.autowireInstance = false;
|
||||||
instance = cast(QualifierType) registration.getInstance(autowireContext);
|
instance = cast(QualifierType) registration.getInstance(autowireContext);
|
||||||
}
|
}
|
||||||
|
|
||||||
return instance;
|
return instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Resolve all dependencies registered to a super type.
|
||||||
|
*
|
||||||
|
* Returns:
|
||||||
|
* An array of autowired instances is returned. The order is undetermined.
|
||||||
|
*
|
||||||
|
* Examples:
|
||||||
|
* ---
|
||||||
|
* class Cat : Animal { ... }
|
||||||
|
* class Dog : Animal { ... }
|
||||||
|
*
|
||||||
|
* container.register!(Animal, Cat);
|
||||||
|
* container.register!(Animal, Dog);
|
||||||
|
*
|
||||||
|
* Animal[] animals = container.resolveAll!Animal;
|
||||||
|
* ---
|
||||||
|
*/
|
||||||
|
public RegistrationType[] resolveAll(RegistrationType)() {
|
||||||
|
RegistrationType[] instances;
|
||||||
|
TypeInfo resolveType = typeid(RegistrationType);
|
||||||
|
|
||||||
|
auto qualifiedRegistrations = resolveType in registrations;
|
||||||
|
if (!qualifiedRegistrations) {
|
||||||
|
throw new ResolveException("Type not registered.", resolveType);
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach(registration ; cast(Registration[]) *qualifiedRegistrations) {
|
||||||
|
instances ~= resolveAutowiredInstance!RegistrationType(registration);
|
||||||
|
}
|
||||||
|
|
||||||
|
return instances;
|
||||||
|
}
|
||||||
|
|
||||||
private Registration getQualifiedRegistration(TypeInfo resolveType, TypeInfo qualifierType, Registration[] candidates) {
|
private Registration getQualifiedRegistration(TypeInfo resolveType, TypeInfo qualifierType, Registration[] candidates) {
|
||||||
if (resolveType == qualifierType) {
|
if (resolveType == qualifierType) {
|
||||||
if (candidates.length > 1) {
|
if (candidates.length > 1) {
|
||||||
|
|
|
@ -443,4 +443,15 @@ version(unittest) {
|
||||||
shared(DependencyContainer) container = new DependencyContainer();
|
shared(DependencyContainer) container = new DependencyContainer();
|
||||||
assertThrown!RegistrationException(container.register!(TestClass, TestClass)(RegistrationOptions.ADD_CONCRETE_TYPE_REGISTRATION));
|
assertThrown!RegistrationException(container.register!(TestClass, TestClass)(RegistrationOptions.ADD_CONCRETE_TYPE_REGISTRATION));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Test resolving all registrations to an interface
|
||||||
|
unittest {
|
||||||
|
shared(DependencyContainer) container = new DependencyContainer();
|
||||||
|
container.register!(Color, Blue);
|
||||||
|
container.register!(Color, Red);
|
||||||
|
|
||||||
|
auto colors = container.resolveAll!Color;
|
||||||
|
|
||||||
|
assert(colors.length == 2, "resolveAll did not yield all instances of interface type");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue