From dc48c879486e5b651c0b0321544d0d102492e573 Mon Sep 17 00:00:00 2001 From: Mike Bierlee Date: Mon, 11 Jun 2018 18:45:27 +0300 Subject: [PATCH] Prevent registration, resolving and constructor injection selection of structs Fixes #25 --- source/poodinis/container.d | 6 +++--- source/poodinis/factory.d | 2 +- test/poodinis/factorytest.d | 11 +++++++++++ test/poodinis/testclasses.d | 11 +++++++++++ 4 files changed, 26 insertions(+), 4 deletions(-) diff --git a/source/poodinis/container.d b/source/poodinis/container.d index 91949dc..37881f7 100644 --- a/source/poodinis/container.d +++ b/source/poodinis/container.d @@ -164,7 +164,7 @@ synchronized class DependencyContainer { * * See_Also: singleInstance, newInstance, existingInstance, RegistrationOption */ - public Registration register(SuperType, ConcreteType : SuperType)(RegistrationOption options = RegistrationOption.none) { + public Registration register(SuperType, ConcreteType : SuperType)(RegistrationOption options = RegistrationOption.none) if (!is(ConcreteType == struct)) { TypeInfo registeredType = typeid(SuperType); TypeInfo_Class concreteType = typeid(ConcreteType); @@ -262,7 +262,7 @@ synchronized class DependencyContainer { * --- * You need to use the resolve method which allows you to specify a qualifier. */ - public RegistrationType resolve(RegistrationType)(ResolveOption resolveOptions = ResolveOption.none) { + public RegistrationType resolve(RegistrationType)(ResolveOption resolveOptions = ResolveOption.none) if (!is(RegistrationType == struct)) { return resolve!(RegistrationType, RegistrationType)(resolveOptions); } @@ -292,7 +292,7 @@ synchronized class DependencyContainer { * container.resolve!(Animal, Dog); * --- */ - public QualifierType resolve(RegistrationType, QualifierType : RegistrationType)(ResolveOption resolveOptions = ResolveOption.none) { + public QualifierType resolve(RegistrationType, QualifierType : RegistrationType)(ResolveOption resolveOptions = ResolveOption.none) if (!is(QualifierType == struct)) { TypeInfo resolveType = typeid(RegistrationType); TypeInfo qualifierType = typeid(QualifierType); diff --git a/source/poodinis/factory.d b/source/poodinis/factory.d index b6bd017..521aa1d 100644 --- a/source/poodinis/factory.d +++ b/source/poodinis/factory.d @@ -131,7 +131,7 @@ class ConstructorInjectingInstanceFactory(InstanceType) : InstanceFactory { private static bool parametersAreValid(Params...)() { bool isValid = true; foreach(param; Params) { - if (isBuiltinType!param) { + if (isBuiltinType!param || is(param == struct)) { isValid = false; break; } diff --git a/test/poodinis/factorytest.d b/test/poodinis/factorytest.d index 419eb46..3be6128 100644 --- a/test/poodinis/factorytest.d +++ b/test/poodinis/factorytest.d @@ -124,6 +124,17 @@ version(unittest) { assert(instance.someOtherClassThen is container.resolve!SomeOtherClassThen); } + // Test injecting constructor of class with struct constructor parameters + unittest { + auto container = new shared DependencyContainer(); + container.register!SomeOtherClassThen; + + auto factory = new ConstructorInjectingInstanceFactory!ClassWithStructConstructor(container); + auto instance = cast(ClassWithStructConstructor) factory.getInstance(); + + assert(instance !is null); + assert(instance.someOtherClassThen is container.resolve!SomeOtherClassThen); + } // Test injecting constructor of class with empty constructor will skip injection unittest { diff --git a/test/poodinis/testclasses.d b/test/poodinis/testclasses.d index 8c7966b..4f37c3f 100644 --- a/test/poodinis/testclasses.d +++ b/test/poodinis/testclasses.d @@ -513,6 +513,17 @@ version(unittest) { } } + class ClassWithStructConstructor { + public SomeOtherClassThen someOtherClassThen; + + this(Thing willNotBePicked) { + } + + this(SomeOtherClassThen someOtherClassThen) { + this.someOtherClassThen = someOtherClassThen; + } + } + class TestType {} class Dependency {}