Add resolve options to resolveAll

This commit is contained in:
Mike Bierlee 2016-02-15 21:59:35 +01:00
parent 21d727c500
commit 6746fd64a0
2 changed files with 13 additions and 2 deletions

View file

@ -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);
} }

View file

@ -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);
}
} }