mirror of
https://github.com/mbierlee/poodinis.git
synced 2024-11-15 04:04:01 +01:00
Add resolve option for not throwing a resolveException
This commit is contained in:
parent
fcf021cf74
commit
21d727c500
|
@ -70,7 +70,13 @@ public enum ResolveOption {
|
||||||
* This essentially makes registration optional for resolving by concerete types.
|
* This essentially makes registration optional for resolving by concerete types.
|
||||||
* Resolinvg will still fail when trying to resolve a dependency by supertype.
|
* 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;
|
auto candidates = resolveType in registrations;
|
||||||
if (!candidates) {
|
if (!candidates) {
|
||||||
|
if (hasOption(resolveOptions, persistentResolveOptions, ResolveOption.noResolveException)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
throw new ResolveException("Type not registered.", resolveType);
|
throw new ResolveException("Type not registered.", resolveType);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -655,4 +655,11 @@ version(unittest) {
|
||||||
shared(DependencyContainer) container = new DependencyContainer();
|
shared(DependencyContainer) container = new DependencyContainer();
|
||||||
assertThrown!ResolveException(container.resolve!TestInterface([ResolveOption.registerBeforeResolving]));
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue