diff --git a/source/poodinis/container.d b/source/poodinis/container.d index 82a80d8..d7e2a7a 100644 --- a/source/poodinis/container.d +++ b/source/poodinis/container.d @@ -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); } diff --git a/test/poodinis/containertest.d b/test/poodinis/containertest.d index b2a976a..3899a5a 100644 --- a/test/poodinis/containertest.d +++ b/test/poodinis/containertest.d @@ -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); + } }