Fix injection of dependencies from foreign modules

Fixes issue #12
This commit is contained in:
Mike Bierlee 2016-09-05 19:29:42 +02:00
parent dc1cf04e96
commit e1f0cca5c5
5 changed files with 37 additions and 1 deletions

View file

@ -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.

View file

@ -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)

View file

@ -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;

View file

@ -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;
}
}

View file

@ -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 {}