From da16513a8de234ad2d1f3acedde6060d6cf129e0 Mon Sep 17 00:00:00 2001 From: Mike Bierlee Date: Mon, 17 Aug 2015 22:25:57 +0200 Subject: [PATCH] Always register concrete type when registering type by supertype by default. This deprecates ADD_CONCRETE_TYPE_REGISTRATION (see DO_NOT_ADD_CONCRETE_TYPE_REGISTRATION for reverse configuration). This also means you do not need to use qualifiers when registering single type of super type by super type. --- CHANGES.md | 4 ++++ source/poodinis/container.d | 15 +++++++++++---- test/poodinis/containertest.d | 18 ++++++++++++++++-- 3 files changed, 31 insertions(+), 6 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index a7c177d..5976a98 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,5 +1,9 @@ Poodinis Changelog ================== +** Next version** +* DEPRECATE ADD_CONCRETE_TYPE_REGISTRATION registration option. It basically does nothing anymore. See next point. +* CHANGE adding registrations by super type always registers them by concrete type as well now. (Previously done with ADD_CONCRETE_TYPE_REGISTRATION). See DO_NOT_ADD_CONCRETE_TYPE_REGISTRATION for the reverse behaviour. + **Version 4.0.0** * REMOVE deprecated module "dependency.d" diff --git a/source/poodinis/container.d b/source/poodinis/container.d index cd7a14a..e761ae4 100644 --- a/source/poodinis/container.d +++ b/source/poodinis/container.d @@ -64,8 +64,17 @@ public enum RegistrationOptions { * * assert(firstCat is secondCat); * --- + * + * Deprecated: ADD_CONCRETE_TYPE_REGISTRATION behaviour is default now. If you want to reverse to happen, use DO_NOT_ADD_CONCRETE_TYPE_REGISTRATION. */ - ADD_CONCRETE_TYPE_REGISTRATION + + ADD_CONCRETE_TYPE_REGISTRATION, + + /** + * 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. + */ + DO_NOT_ADD_CONCRETE_TYPE_REGISTRATION } /** @@ -140,12 +149,10 @@ synchronized class DependencyContainer { auto newRegistration = new AutowiredRegistration!ConcreteType(registeredType, this); newRegistration.singleInstance(); - if (hasOption(options, RegistrationOptions.ADD_CONCRETE_TYPE_REGISTRATION)) { + if (!hasOption(options, RegistrationOptions.DO_NOT_ADD_CONCRETE_TYPE_REGISTRATION)) { static if (!is(SuperType == ConcreteType)) { auto concreteTypeRegistration = register!ConcreteType; concreteTypeRegistration.linkTo(newRegistration); - } else { - throw new RegistrationException("Option ADD_CONCRETE_TYPE_REGISTRATION cannot be used when registering a concrete type registration", concreteType); } } diff --git a/test/poodinis/containertest.d b/test/poodinis/containertest.d index 65b640a..5df30ee 100644 --- a/test/poodinis/containertest.d +++ b/test/poodinis/containertest.d @@ -443,10 +443,24 @@ version(unittest) { assert(firstInstance is secondInstance); } - // Test registering concrete type with option ADD_CONCRETE_TYPE_REGISTRATION + // Test registering type with option DO_NOT_ADD_CONCRETE_TYPE_REGISTRATION unittest { shared(DependencyContainer) container = new DependencyContainer(); - assertThrown!RegistrationException(container.register!(TestClass, TestClass)(RegistrationOptions.ADD_CONCRETE_TYPE_REGISTRATION)); + container.register!(TestInterface, TestClass)(RegistrationOptions.DO_NOT_ADD_CONCRETE_TYPE_REGISTRATION); + + auto firstInstance = container.resolve!TestInterface; + assertThrown!ResolveException(container.resolve!TestClass); + } + + // Test registering type will register by contrete type by default + unittest { + shared(DependencyContainer) container = new DependencyContainer(); + container.register!(TestInterface, TestClass); + + auto firstInstance = container.resolve!TestInterface; + auto secondInstance = container.resolve!TestClass; + + assert(firstInstance is secondInstance); } // Test resolving all registrations to an interface