mirror of
https://github.com/mbierlee/poodinis.git
synced 2024-11-15 12:14:01 +01:00
Add resolve options to resolveAll
This commit is contained in:
parent
21d727c500
commit
6746fd64a0
|
@ -74,7 +74,7 @@ public enum ResolveOption {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Does not throw a resolve exception when a type is not registered but will
|
* Does not throw a resolve exception when a type is not registered but will
|
||||||
* return null instead.
|
* return null instead. If the type is an array, an empty array is returned instead.
|
||||||
*/
|
*/
|
||||||
noResolveException
|
noResolveException
|
||||||
}
|
}
|
||||||
|
@ -348,12 +348,16 @@ synchronized class DependencyContainer {
|
||||||
* Animal[] animals = container.resolveAll!Animal;
|
* Animal[] animals = container.resolveAll!Animal;
|
||||||
* ---
|
* ---
|
||||||
*/
|
*/
|
||||||
public RegistrationType[] resolveAll(RegistrationType)() {
|
public RegistrationType[] resolveAll(RegistrationType)(ResolveOption[] resolveOptions = []) {
|
||||||
RegistrationType[] instances;
|
RegistrationType[] instances;
|
||||||
TypeInfo resolveType = typeid(RegistrationType);
|
TypeInfo resolveType = typeid(RegistrationType);
|
||||||
|
|
||||||
auto qualifiedRegistrations = resolveType in registrations;
|
auto qualifiedRegistrations = resolveType in registrations;
|
||||||
if (!qualifiedRegistrations) {
|
if (!qualifiedRegistrations) {
|
||||||
|
if (hasOption(resolveOptions, persistentResolveOptions, ResolveOption.noResolveException)) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
throw new ResolveException("Type not registered.", resolveType);
|
throw new ResolveException("Type not registered.", resolveType);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -662,4 +662,11 @@ version(unittest) {
|
||||||
auto instance = container.resolve!TestInterface([ResolveOption.noResolveException]);
|
auto instance = container.resolve!TestInterface([ResolveOption.noResolveException]);
|
||||||
assert(instance is null);
|
assert(instance is null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ResolveOption noResolveException does not throw for resolveAll
|
||||||
|
unittest {
|
||||||
|
shared(DependencyContainer) container = new DependencyContainer();
|
||||||
|
auto instances = container.resolveAll!TestInterface([ResolveOption.noResolveException]);
|
||||||
|
assert(instances.length == 0);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue