From 21d727c50083ffea4b4dc3e3db4e8d8e97c094d1 Mon Sep 17 00:00:00 2001 From: Mike Bierlee Date: Mon, 15 Feb 2016 21:43:10 +0100 Subject: [PATCH] Add resolve option for not throwing a resolveException --- source/poodinis/container.d | 12 +++++++++++- test/poodinis/containertest.d | 7 +++++++ 2 files changed, 18 insertions(+), 1 deletion(-) 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); + } }