From 5d9ac7341895b364d8b34a762e4bf15006314cc5 Mon Sep 17 00:00:00 2001 From: Mike Bierlee Date: Wed, 3 Feb 2016 23:13:36 +0100 Subject: [PATCH] Add setting persistent resolve options --- CHANGES.md | 2 +- source/poodinis/container.d | 19 ++++++++++++++++++- test/poodinis/containertest.d | 15 +++++++++++++++ 3 files changed, 34 insertions(+), 2 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 34b4f10..fdd3a7d 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,7 +1,7 @@ Poodinis Changelog ================== **Version NEXT** -* ADD setting persistent registration options +* ADD setting persistent registration and resolve options * DEPRECATE DO_NOT_ADD_CONCRETE_TYPE_REGISTRATION, use doNotAddConcreteTypeRegistration instead * DEPRECATE supplying register()'s registration options as variadic arguments. Use register(SuperType, ConcreteType)(RegistrationOption[]) instead. * ADD resolve options to container resolve() diff --git a/source/poodinis/container.d b/source/poodinis/container.d index 0cd2c5e..5bfdf23 100644 --- a/source/poodinis/container.d +++ b/source/poodinis/container.d @@ -417,7 +417,7 @@ synchronized class DependencyContainer { /** * Apply persistent registration options which will be used everytime register() is called. */ - public void setPersistentRegistrationOptions(RegistrationOptionsTuple...)(RegistrationOptionsTuple registrationOptions) { + public void setPersistentRegistrationOptions(OptionsTuple...)(OptionsTuple registrationOptions) { unsetPersistentRegistrationOptions(); foreach (option; registrationOptions) { persistentRegistrationOptions ~= option; @@ -431,4 +431,21 @@ synchronized class DependencyContainer { persistentRegistrationOptions = []; } + /** + * Apply persistent resolve options which will be used everytime resolve() is called. + */ + public void setPersistentResolveOptions(OptionsTuple...)(OptionsTuple resolveOptions) { + unsetPersistentResolveOptions(); + foreach (option; resolveOptions) { + persistentResolveOptions ~= option; + } + } + + /** + * Unsets all applied registration options + */ + public void unsetPersistentResolveOptions() { + persistentResolveOptions = []; + } + } diff --git a/test/poodinis/containertest.d b/test/poodinis/containertest.d index d45aa05..2080aa4 100644 --- a/test/poodinis/containertest.d +++ b/test/poodinis/containertest.d @@ -634,4 +634,19 @@ version(unittest) { container.resolve!(TestInterface, TestClass)([ResolveOption.registerBeforeResolving]); container.resolve!TestClass; } + + // Test set persistent resolve options + unittest { + shared(DependencyContainer) container = new DependencyContainer(); + container.setPersistentResolveOptions(ResolveOption.registerBeforeResolving); + container.resolve!TestClass; + } + + // Test unset persistent resolve options + unittest { + shared(DependencyContainer) container = new DependencyContainer(); + container.setPersistentResolveOptions(ResolveOption.registerBeforeResolving); + container.unsetPersistentResolveOptions(); + assertThrown!ResolveException(container.resolve!TestClass); + } }