From 6746fd64a0b917e876f1305b8305e5f1a611337d Mon Sep 17 00:00:00 2001 From: Mike Bierlee Date: Mon, 15 Feb 2016 21:59:35 +0100 Subject: [PATCH] Add resolve options to resolveAll --- source/poodinis/container.d | 8 ++++++-- test/poodinis/containertest.d | 7 +++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/source/poodinis/container.d b/source/poodinis/container.d index d7e2a7a..784d87b 100644 --- a/source/poodinis/container.d +++ b/source/poodinis/container.d @@ -74,7 +74,7 @@ public enum ResolveOption { /** * 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 } @@ -348,12 +348,16 @@ synchronized class DependencyContainer { * Animal[] animals = container.resolveAll!Animal; * --- */ - public RegistrationType[] resolveAll(RegistrationType)() { + public RegistrationType[] resolveAll(RegistrationType)(ResolveOption[] resolveOptions = []) { RegistrationType[] instances; TypeInfo resolveType = typeid(RegistrationType); auto qualifiedRegistrations = resolveType in registrations; if (!qualifiedRegistrations) { + if (hasOption(resolveOptions, persistentResolveOptions, ResolveOption.noResolveException)) { + return []; + } + throw new ResolveException("Type not registered.", resolveType); } diff --git a/test/poodinis/containertest.d b/test/poodinis/containertest.d index 3899a5a..b2d9b0f 100644 --- a/test/poodinis/containertest.d +++ b/test/poodinis/containertest.d @@ -662,4 +662,11 @@ version(unittest) { auto instance = container.resolve!TestInterface([ResolveOption.noResolveException]); 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); + } }