From e1f0cca5c5fa6ee875d245100276ff2734829df4 Mon Sep 17 00:00:00 2001 From: Mike Bierlee Date: Mon, 5 Sep 2016 19:29:42 +0200 Subject: [PATCH] Fix injection of dependencies from foreign modules Fixes issue #12 --- CHANGES.md | 4 ++++ README.md | 2 +- source/poodinis/factory.d | 9 +++++++++ test/poodinis/containertest.d | 13 +++++++++++++ test/poodinis/foreigndependencies.d | 10 ++++++++++ 5 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 test/poodinis/foreigndependencies.d diff --git a/CHANGES.md b/CHANGES.md index 1573a95..0488f01 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,5 +1,9 @@ Poodinis Changelog ================== +**Version 7.0.1** +* FIX codegeneration of constructor injection factories for constructors with dependencies from foreign modules, +such as modules from other libraries (Issue #12). + **Version 7.0.0** This version introduces changes which might be incompatible with your current codebase * ADD constructor injection. Injection is done automatically on resolve. See tutorial and examples for more details. diff --git a/README.md b/README.md index 7df508e..bad51bc 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ Poodinis Dependency Injection Framework ======================================= -Version 7.0.0 +Version 7.0.1 Copyright 2014-2016 Mike Bierlee Licensed under the terms of the MIT license - See [LICENSE.txt](LICENSE.txt) diff --git a/source/poodinis/factory.d b/source/poodinis/factory.d index f0cc84d..01375e1 100644 --- a/source/poodinis/factory.d +++ b/source/poodinis/factory.d @@ -107,6 +107,14 @@ class ConstructorInjectingInstanceFactory(InstanceType) : InstanceFactory { return argumentList; } + private static string createImportList(Params...)() { + string importList = ""; + foreach(param; Params) { + importList ~= "import " ~ moduleName!param ~ ";"; + } + return importList; + } + private static bool parametersAreValid(Params...)() { bool isValid = true; foreach(param; Params) { @@ -130,6 +138,7 @@ class ConstructorInjectingInstanceFactory(InstanceType) : InstanceFactory { isBeingInjected = true; mixin(` import ` ~ moduleName!InstanceType ~ `; + ` ~ createImportList!(Parameters!ctor) ~ ` instance = new ` ~ fullyQualifiedName!InstanceType ~ `(` ~ createArgumentList!(Parameters!ctor) ~ `); `); isBeingInjected = false; diff --git a/test/poodinis/containertest.d b/test/poodinis/containertest.d index 244e054..f9147b9 100644 --- a/test/poodinis/containertest.d +++ b/test/poodinis/containertest.d @@ -6,6 +6,7 @@ */ import poodinis; +import poodinis.test.foreignDependencies; import std.exception; import core.thread; @@ -219,6 +220,10 @@ version(unittest) { this(Paper paper) {} } + class Hello { + this(Ola ola) {} + } + // Test register concrete type unittest { auto container = new shared DependencyContainer(); @@ -734,4 +739,12 @@ version(unittest) { assertThrown!InstanceCreationException(container.resolve!Rock); } + + // Test injection of foreign dependency in constructor + unittest { + auto container = new shared DependencyContainer(); + container.register!Ola; + container.register!Hello; + container.resolve!Hello; + } } diff --git a/test/poodinis/foreigndependencies.d b/test/poodinis/foreigndependencies.d new file mode 100644 index 0000000..4f17cb6 --- /dev/null +++ b/test/poodinis/foreigndependencies.d @@ -0,0 +1,10 @@ +/** + * Poodinis Dependency Injection Framework + * Copyright 2014-2016 Mike Bierlee + * This software is licensed under the terms of the MIT license. + * The full terms of the license can be found in the LICENSE file. + */ + +module poodinis.test.foreignDependencies; + +class Ola {}