diff --git a/CHANGES.md b/CHANGES.md index 2922d34..208c8f5 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,5 +1,8 @@ Poodinis Changelog ================== +**Version NEXT** +* ADD setting persistent registration options + **Version 6.0.0** * CHANGE registration scopes are replaced by a single factory implementation. If you were not doing anything with the internal scope mechanism, you should not be affected by this change. diff --git a/TUTORIAL.md b/TUTORIAL.md index dc29180..246c3eb 100644 --- a/TUTORIAL.md +++ b/TUTORIAL.md @@ -196,4 +196,12 @@ class Context : ApplicationContext { return new SomeClass(); } } +``` + +Persistent Registration Options +------------------------------- +If you want registration options to be persistent (applicable for every call to register()), you can use the container method setPersistentRegistrationOptions(): +```d +dependencies.setPersistentRegistrationOptions(RegistrationOption.DO_NOT_ADD_CONCRETE_TYPE_REGISTRATION); // Sets the option +dependencies.unsetPersistentRegistrationOptions(); // Clears the persistentent options ``` \ No newline at end of file diff --git a/source/poodinis/container.d b/source/poodinis/container.d index 8c39736..19b47a3 100644 --- a/source/poodinis/container.d +++ b/source/poodinis/container.d @@ -69,6 +69,8 @@ synchronized class DependencyContainer { private Registration[] autowireStack; + private RegistrationOption[] persistentRegistrationOptions; + /** * Register a dependency by concrete class type. * @@ -126,7 +128,7 @@ synchronized class DependencyContainer { auto newRegistration = new AutowiredRegistration!ConcreteType(registeredType, this); newRegistration.singleInstance(); - if (!hasOption(options, RegistrationOption.DO_NOT_ADD_CONCRETE_TYPE_REGISTRATION)) { + if (!hasOption(options, persistentRegistrationOptions, RegistrationOption.DO_NOT_ADD_CONCRETE_TYPE_REGISTRATION)) { static if (!is(SuperType == ConcreteType)) { auto concreteTypeRegistration = register!ConcreteType; concreteTypeRegistration.linkTo(newRegistration); @@ -137,7 +139,13 @@ synchronized class DependencyContainer { return newRegistration; } - private bool hasOption(RegistrationOptionsTuple...)(RegistrationOptionsTuple options, RegistrationOption option) { + private bool hasOption(OptionType, OptionsTuple...)(OptionsTuple options, OptionType[] persistentOptions, OptionType option) { + foreach (presentOption; persistentOptions) { + if (presentOption == option) { + return true; + } + } + foreach(presentOption ; options) { if (presentOption == option) { return true; @@ -358,4 +366,22 @@ synchronized class DependencyContainer { } return instance; } + + /** + * Apply persistent registration options which will be used everytime register() is called. + */ + public void setPersistentRegistrationOptions(RegistrationOptionsTuple...)(RegistrationOptionsTuple registrationOptions) { + unsetPersistentRegistrationOptions(); + foreach (option; registrationOptions) { + persistentRegistrationOptions ~= option; + } + } + + /** + * Unsets all applied registration options + */ + public void unsetPersistentRegistrationOptions() { + persistentRegistrationOptions = []; + } + } diff --git a/test/poodinis/containertest.d b/test/poodinis/containertest.d index fca0e83..c5e7527 100644 --- a/test/poodinis/containertest.d +++ b/test/poodinis/containertest.d @@ -585,4 +585,21 @@ version(unittest) { container.registerContext!TestContext; container.resolve!ApplicationContext; } + + // Test set persistent registration options + unittest { + shared(DependencyContainer) container = new DependencyContainer(); + container.setPersistentRegistrationOptions(RegistrationOption.DO_NOT_ADD_CONCRETE_TYPE_REGISTRATION); + container.register!(TestInterface, TestClass); + assertThrown!ResolveException(container.resolve!TestClass); + } + + // Test unset persistent registration options + unittest { + shared(DependencyContainer) container = new DependencyContainer(); + container.setPersistentRegistrationOptions(RegistrationOption.DO_NOT_ADD_CONCRETE_TYPE_REGISTRATION); + container.unsetPersistentRegistrationOptions(); + container.register!(TestInterface, TestClass); + container.resolve!TestClass; + } }