From 121d8715721aee931866d66c1c8ef7e90b380c90 Mon Sep 17 00:00:00 2001 From: Mike Bierlee Date: Sat, 31 May 2014 23:37:02 +0200 Subject: [PATCH] Replace type validity check with template parameter specialization --- source/poodinis/container.d | 25 ++----------------------- test/poodinis/containertest.d | 30 ------------------------------ 2 files changed, 2 insertions(+), 53 deletions(-) diff --git a/source/poodinis/container.d b/source/poodinis/container.d index ee2d530..43f96fe 100644 --- a/source/poodinis/container.d +++ b/source/poodinis/container.d @@ -23,42 +23,21 @@ class Container { private static Container instance; private Registration[TypeInfo] registrations; - private bool _typeValidityCheckEnabled = true; - - @property public void typeValidityCheckEnabled(bool enabled) { - _typeValidityCheckEnabled = enabled; - } - - @property public bool typeValidityCheckEnabled() { - return _typeValidityCheckEnabled; - } public Registration register(ConcreteType)() { - return register!(ConcreteType, ConcreteType)(false); + return register!(ConcreteType, ConcreteType)(); } - public Registration register(InterfaceType, ConcreteType)(bool checkTypeValidity = true) { + public Registration register(InterfaceType, ConcreteType : InterfaceType)() { TypeInfo registeredType = typeid(InterfaceType); TypeInfo_Class instantiatableType = typeid(ConcreteType); - if (typeValidityCheckEnabled && checkTypeValidity) { - checkValidity!(InterfaceType)(registeredType, instantiatableType); - } - Registration newRegistration = new Registration(registeredType, instantiatableType); newRegistration.singleInstance(); registrations[registeredType] = newRegistration; return newRegistration; } - private void checkValidity(InterfaceType)(TypeInfo registeredType, TypeInfo_Class instanceType) { - InterfaceType instanceCanBeCastToInterface = cast(InterfaceType) instanceType.create(); - if (!instanceCanBeCastToInterface) { - string errorMessage = format("%s cannot be cast to %s.", instanceType.name, registeredType.toString()); - throw new RegistrationException(errorMessage, registeredType, instanceType); - } - } - public RegistrationType resolve(RegistrationType)() { TypeInfo resolveType = typeid(RegistrationType); Registration* registration = resolveType in registrations; diff --git a/test/poodinis/containertest.d b/test/poodinis/containertest.d index a10e3e5..206ed5b 100644 --- a/test/poodinis/containertest.d +++ b/test/poodinis/containertest.d @@ -61,31 +61,12 @@ version(unittest) { assert(cast(TestInterface) actualInstance, "Resolved class is not the same type as expected"); } - // Test register unrelated types fails - unittest { - auto container = new Container(); - assertThrown!RegistrationException(container.register!(UnrelatedClass, TestClass)(), "Registering unrelated types does not fail"); - } - - // Test register unrelated types with disabled check on registration - unittest { - auto container = new Container(); - assertNotThrown!RegistrationException(container.register!(UnrelatedClass, TestClass)(false), "Registering unrelated types while disabling type validity fails"); - } - // Test resolve non-registered type unittest { auto container = new Container(); assertThrown!ResolveException(container.resolve!(TestClass)(), "Resolving non-registered type does not fail"); } - // Test register unrelated class with disable global type validity disabled - unittest { - auto container = new Container(); - container.typeValidityCheckEnabled = false; - assertNotThrown!RegistrationException(container.register!(UnrelatedClass, TestClass)(), "Registering unrelated types while disabling global type validity fails"); - } - // Test clear registrations unittest { auto container = new Container(); @@ -101,17 +82,6 @@ version(unittest) { assert(instance1 is instance2, "getInstance does not return the same instance"); } - // Test registering concrete type does not do a validity check - unittest { - auto container = new Container(); - assert(container.typeValidityCheckEnabled); - try { - container.register!(FailOnCreationClass)(); - } catch (Exception) { - assert(false, "Registering concrete type executed a validity check"); - } - } - // Test resolve single instance for type unittest { auto container = new Container();