diff --git a/CHANGES.md b/CHANGES.md index 208c8f5..8b2fc16 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -2,6 +2,7 @@ Poodinis Changelog ================== **Version NEXT** * ADD setting persistent registration options +* DEPRECATE DO_NOT_ADD_CONCRETE_TYPE_REGISTRATION, use doNotAddConcreteTypeRegistration instead **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 diff --git a/source/poodinis/container.d b/source/poodinis/container.d index 19b47a3..d7ed7d6 100644 --- a/source/poodinis/container.d +++ b/source/poodinis/container.d @@ -51,6 +51,13 @@ public enum RegistrationOption { * Prevent a concrete type being registered on itself. With this option you will always need * to use the supertype as the type of the dependency. */ + doNotAddConcreteTypeRegistration, + + /** + * Prevent a concrete type being registered on itself. With this option you will always need + * to use the supertype as the type of the dependency. + * @deprecated use doNotAddConcreteTypeRegistration instead + */ DO_NOT_ADD_CONCRETE_TYPE_REGISTRATION } @@ -128,7 +135,7 @@ synchronized class DependencyContainer { auto newRegistration = new AutowiredRegistration!ConcreteType(registeredType, this); newRegistration.singleInstance(); - if (!hasOption(options, persistentRegistrationOptions, RegistrationOption.DO_NOT_ADD_CONCRETE_TYPE_REGISTRATION)) { + if (!hasOption(options, persistentRegistrationOptions, RegistrationOption.doNotAddConcreteTypeRegistration)) { static if (!is(SuperType == ConcreteType)) { auto concreteTypeRegistration = register!ConcreteType; concreteTypeRegistration.linkTo(newRegistration); @@ -141,12 +148,22 @@ synchronized class DependencyContainer { private bool hasOption(OptionType, OptionsTuple...)(OptionsTuple options, OptionType[] persistentOptions, OptionType option) { foreach (presentOption; persistentOptions) { + // DEPRECATED LEGACY COMPATIBILITY - REMOVE REMOVE REMOVE REMOVE REMOVE REMOVE (SOON) + if (presentOption == RegistrationOption.DO_NOT_ADD_CONCRETE_TYPE_REGISTRATION) { + presentOption = RegistrationOption.doNotAddConcreteTypeRegistration; + } + if (presentOption == option) { return true; } } foreach(presentOption ; options) { + // DEPRECATED LEGACY COMPATIBILITY - REMOVE REMOVE REMOVE REMOVE REMOVE REMOVE (SOON) + if (presentOption == RegistrationOption.DO_NOT_ADD_CONCRETE_TYPE_REGISTRATION) { + presentOption = RegistrationOption.doNotAddConcreteTypeRegistration; + } + if (presentOption == option) { return true; } diff --git a/test/poodinis/containertest.d b/test/poodinis/containertest.d index c5e7527..ddfde4a 100644 --- a/test/poodinis/containertest.d +++ b/test/poodinis/containertest.d @@ -494,7 +494,16 @@ version(unittest) { assert(expectedTestClass is actualTestClass, "Instance resolved in main thread is not the one resolved in thread"); } - // Test registering type with option DO_NOT_ADD_CONCRETE_TYPE_REGISTRATION + // Test registering type with option doNotAddConcreteTypeRegistration + unittest { + shared(DependencyContainer) container = new DependencyContainer(); + container.register!(TestInterface, TestClass)(RegistrationOption.doNotAddConcreteTypeRegistration); + + auto firstInstance = container.resolve!TestInterface; + assertThrown!ResolveException(container.resolve!TestClass); + } + + // Test registering type with option DO_NOT_ADD_CONCRETE_TYPE_REGISTRATION (DEPRECATED) unittest { shared(DependencyContainer) container = new DependencyContainer(); container.register!(TestInterface, TestClass)(RegistrationOption.DO_NOT_ADD_CONCRETE_TYPE_REGISTRATION); @@ -589,7 +598,7 @@ version(unittest) { // Test set persistent registration options unittest { shared(DependencyContainer) container = new DependencyContainer(); - container.setPersistentRegistrationOptions(RegistrationOption.DO_NOT_ADD_CONCRETE_TYPE_REGISTRATION); + container.setPersistentRegistrationOptions(RegistrationOption.doNotAddConcreteTypeRegistration); container.register!(TestInterface, TestClass); assertThrown!ResolveException(container.resolve!TestClass); } @@ -597,7 +606,7 @@ version(unittest) { // Test unset persistent registration options unittest { shared(DependencyContainer) container = new DependencyContainer(); - container.setPersistentRegistrationOptions(RegistrationOption.DO_NOT_ADD_CONCRETE_TYPE_REGISTRATION); + container.setPersistentRegistrationOptions(RegistrationOption.doNotAddConcreteTypeRegistration); container.unsetPersistentRegistrationOptions(); container.register!(TestInterface, TestClass); container.resolve!TestClass;