Add additional debug output to more clearly show the chain of dependency injection

This commit is contained in:
Mike Bierlee 2014-10-13 00:08:39 +02:00
parent 92db3c0405
commit e0436438af
3 changed files with 40 additions and 2 deletions

View file

@ -21,6 +21,12 @@ class Autowire{};
alias Autowired = Autowire; alias Autowired = Autowire;
public void autowire(Type)(Container container, Type instance) { public void autowire(Type)(Container container, Type instance) {
debug {
auto memberType = typeid(Type);
auto instanceAddress = &instance;
writeln(format("DEBUG: Autowiring members of [%s@%s]", memberType, instanceAddress));
}
foreach (member ; __traits(allMembers, Type)) { foreach (member ; __traits(allMembers, Type)) {
static if(__traits(compiles, __traits( getMember, Type, member )) && __traits(compiles, __traits(getAttributes, __traits(getMember, Type, member )))) { static if(__traits(compiles, __traits( getMember, Type, member )) && __traits(compiles, __traits(getAttributes, __traits(getMember, Type, member )))) {
foreach(attribute; __traits(getAttributes, __traits(getMember, Type, member))) { foreach(attribute; __traits(getAttributes, __traits(getMember, Type, member))) {
@ -30,8 +36,6 @@ public void autowire(Type)(Container container, Type instance) {
debug { debug {
auto autowirableType = typeid(typeof(memberReference[0])); auto autowirableType = typeid(typeof(memberReference[0]));
auto autowireableAddress = &autowirableInstance; auto autowireableAddress = &autowirableInstance;
auto memberType = typeid(Type);
auto instanceAddress = &instance;
writeln(format("DEBUG: Autowire instance [%s@%s] to [%s@%s].%s", autowirableType, autowireableAddress, memberType, instanceAddress, member)); writeln(format("DEBUG: Autowire instance [%s@%s] to [%s@%s].%s", autowirableType, autowireableAddress, memberType, instanceAddress, member));
} }

View file

@ -11,6 +11,10 @@ import std.string;
import std.array; import std.array;
import std.algorithm; import std.algorithm;
debug {
import std.stdio;
}
public import poodinis.registration; public import poodinis.registration;
public import poodinis.autowire; public import poodinis.autowire;
@ -42,6 +46,10 @@ class Container {
TypeInfo registeredType = typeid(InterfaceType); TypeInfo registeredType = typeid(InterfaceType);
TypeInfo_Class instantiatableType = typeid(ConcreteType); TypeInfo_Class instantiatableType = typeid(ConcreteType);
debug {
writeln(format("DEBUG: Register type %s (as %s)", instantiatableType.toString(), registeredType.toString()));
}
Registration newRegistration = new Registration(registeredType, instantiatableType); Registration newRegistration = new Registration(registeredType, instantiatableType);
newRegistration.singleInstance(); newRegistration.singleInstance();
registrations[registeredType] = newRegistration; registrations[registeredType] = newRegistration;
@ -50,6 +58,10 @@ class Container {
public RegistrationType resolve(RegistrationType)() { public RegistrationType resolve(RegistrationType)() {
TypeInfo resolveType = typeid(RegistrationType); TypeInfo resolveType = typeid(RegistrationType);
debug {
writeln("DEBUG: Resolving type " ~ resolveType.toString());
}
Registration* registration = resolveType in registrations; Registration* registration = resolveType in registrations;
if (!registration) { if (!registration) {
throw new ResolveException("Type not registered.", resolveType); throw new ResolveException("Type not registered.", resolveType);

View file

@ -7,6 +7,11 @@
module poodinis.registration; module poodinis.registration;
debug {
import std.stdio;
import std.string;
}
class Registration { class Registration {
TypeInfo registeredType = null; TypeInfo registeredType = null;
TypeInfo_Class instantiatableType = null; TypeInfo_Class instantiatableType = null;
@ -38,6 +43,9 @@ interface CreationScope {
class NullScope : CreationScope { class NullScope : CreationScope {
public Object getInstance() { public Object getInstance() {
debug {
writeln("DEBUG: No instance created (NullScope)");
}
return null; return null;
} }
} }
@ -52,9 +60,17 @@ class SingleInstanceScope : CreationScope {
public Object getInstance() { public Object getInstance() {
if (instance is null) { if (instance is null) {
debug {
writeln(format("DEBUG: Creating new instance of type %s (SingleInstanceScope)", instantiatableType.toString()));
}
instance = instantiatableType.create(); instance = instantiatableType.create();
} else {
debug {
writeln(format("DEBUG: Existing instance returned of type %s (SingleInstanceScope)", instantiatableType.toString()));
}
} }
return instance; return instance;
} }
} }
@ -72,6 +88,9 @@ class NewInstanceScope : CreationScope {
} }
public Object getInstance() { public Object getInstance() {
debug {
writeln(format("DEBUG: Creating new instance of type %s (SingleInstanceScope)", instantiatableType.toString()));
}
return instantiatableType.create(); return instantiatableType.create();
} }
} }
@ -89,6 +108,9 @@ class ExistingInstanceScope : CreationScope {
} }
public Object getInstance() { public Object getInstance() {
debug {
writeln("DEBUG: Existing instance returned (ExistingInstanceScope)");
}
return instance; return instance;
} }
} }