Add resolve option for not throwing a resolveException

This commit is contained in:
Mike Bierlee 2016-02-15 21:43:10 +01:00
parent fcf021cf74
commit 21d727c500
2 changed files with 18 additions and 1 deletions

View file

@ -70,7 +70,13 @@ public enum ResolveOption {
* This essentially makes registration optional for resolving by concerete types.
* Resolinvg will still fail when trying to resolve a dependency by supertype.
*/
registerBeforeResolving
registerBeforeResolving,
/**
* Does not throw a resolve exception when a type is not registered but will
* return null instead.
*/
noResolveException
}
/**
@ -300,6 +306,10 @@ synchronized class DependencyContainer {
auto candidates = resolveType in registrations;
if (!candidates) {
if (hasOption(resolveOptions, persistentResolveOptions, ResolveOption.noResolveException)) {
return null;
}
throw new ResolveException("Type not registered.", resolveType);
}

View file

@ -655,4 +655,11 @@ version(unittest) {
shared(DependencyContainer) container = new DependencyContainer();
assertThrown!ResolveException(container.resolve!TestInterface([ResolveOption.registerBeforeResolving]));
}
// Test ResolveOption noResolveException does not throw
unittest {
shared(DependencyContainer) container = new DependencyContainer();
auto instance = container.resolve!TestInterface([ResolveOption.noResolveException]);
assert(instance is null);
}
}