Recursively import types everywhere

#20
This commit is contained in:
Mike Bierlee 2017-08-13 18:21:03 +02:00
parent 91fcefa7ed
commit 93d4d3c20f
4 changed files with 23 additions and 18 deletions

View file

@ -22,6 +22,7 @@ import poodinis.registration;
import poodinis.factory;
import poodinis.valueinjection;
import poodinis.polyfill;
import poodinis.imports;
import std.exception;
import std.stdio;
@ -265,7 +266,7 @@ class AutowiredRegistration(RegistrationType : Object) : Registration {
private void delegate() getPreDestructor(RegistrationType instance) {
void delegate() preDestructor = null;
foreach (memberName; __traits(allMembers, RegistrationType)) {
mixin(`import ` ~ moduleName!RegistrationType ~ `;`);
mixin(createImportsString!RegistrationType);
static if (__traits(compiles, __traits(getProtection, __traits(getMember, instance, memberName)))
&& __traits(getProtection, __traits(getMember, instance, memberName)) == "public"
&& isFunction!(mixin(fullyQualifiedName!RegistrationType ~ `.` ~ memberName))

View file

@ -19,6 +19,7 @@ import poodinis.context;
import poodinis.factory;
import poodinis.valueinjection;
import poodinis.polyfill;
import poodinis.imports;
import std.string;
import std.algorithm;
@ -402,19 +403,6 @@ synchronized class DependencyContainer {
}
}
private static string createImportsString(Type)() {
string imports = `import ` ~ moduleName!Type ~ `;`;
static if (__traits(compiles, TemplateArgsOf!Type)) {
foreach(TemplateArgType; TemplateArgsOf!Type) {
static if (!isBuiltinType!TemplateArgType) {
imports ~= createImportsString!TemplateArgType;
}
}
}
return imports;
}
/**
* Clears all dependency registrations managed by this container.
*/

View file

@ -12,6 +12,7 @@
module poodinis.factory;
import poodinis.container;
import poodinis.imports;
import std.typecons;
import std.exception;
@ -122,7 +123,7 @@ class ConstructorInjectingInstanceFactory(InstanceType) : InstanceFactory {
private static string createImportList(Params...)() {
string importList = "";
foreach(param; Params) {
importList ~= "import " ~ moduleName!param ~ ";";
importList ~= createImportsString!param;
}
return importList;
}
@ -148,9 +149,8 @@ class ConstructorInjectingInstanceFactory(InstanceType) : InstanceFactory {
foreach(ctor ; __traits(getOverloads, InstanceType, `__ctor`)) {
static if (parametersAreValid!(Parameters!ctor)) {
isBeingInjected = true;
mixin(`
import ` ~ moduleName!InstanceType ~ `;
` ~ createImportList!(Parameters!ctor) ~ `
mixin(createImportsString!InstanceType
~ createImportList!(Parameters!ctor) ~ `
instance = new ` ~ fullyQualifiedName!InstanceType ~ `(` ~ createArgumentList!(Parameters!ctor) ~ `);
`);
isBeingInjected = false;

16
source/poodinis/imports.d Normal file
View file

@ -0,0 +1,16 @@
module poodinis.imports;
import std.traits;
public static string createImportsString(Type)() {
string imports = `import ` ~ moduleName!Type ~ `;`;
static if (__traits(compiles, TemplateArgsOf!Type)) {
foreach(TemplateArgType; TemplateArgsOf!Type) {
static if (!isBuiltinType!TemplateArgType) {
imports ~= createImportsString!TemplateArgType;
}
}
}
return imports;
}