From e0436438af52295da9cfb54477336b14fb024b44 Mon Sep 17 00:00:00 2001 From: Mike Bierlee Date: Mon, 13 Oct 2014 00:08:39 +0200 Subject: [PATCH] Add additional debug output to more clearly show the chain of dependency injection --- source/poodinis/autowire.d | 8 ++++++-- source/poodinis/container.d | 12 ++++++++++++ source/poodinis/registration.d | 22 ++++++++++++++++++++++ 3 files changed, 40 insertions(+), 2 deletions(-) diff --git a/source/poodinis/autowire.d b/source/poodinis/autowire.d index 2f99ab9..0df4c5b 100644 --- a/source/poodinis/autowire.d +++ b/source/poodinis/autowire.d @@ -21,6 +21,12 @@ class Autowire{}; alias Autowired = Autowire; 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)) { static if(__traits(compiles, __traits( getMember, Type, member )) && __traits(compiles, __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 { auto autowirableType = typeid(typeof(memberReference[0])); 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)); } diff --git a/source/poodinis/container.d b/source/poodinis/container.d index 7ec088e..5fdff5e 100644 --- a/source/poodinis/container.d +++ b/source/poodinis/container.d @@ -11,6 +11,10 @@ import std.string; import std.array; import std.algorithm; +debug { + import std.stdio; +} + public import poodinis.registration; public import poodinis.autowire; @@ -42,6 +46,10 @@ class Container { TypeInfo registeredType = typeid(InterfaceType); TypeInfo_Class instantiatableType = typeid(ConcreteType); + debug { + writeln(format("DEBUG: Register type %s (as %s)", instantiatableType.toString(), registeredType.toString())); + } + Registration newRegistration = new Registration(registeredType, instantiatableType); newRegistration.singleInstance(); registrations[registeredType] = newRegistration; @@ -50,6 +58,10 @@ class Container { public RegistrationType resolve(RegistrationType)() { TypeInfo resolveType = typeid(RegistrationType); + debug { + writeln("DEBUG: Resolving type " ~ resolveType.toString()); + } + Registration* registration = resolveType in registrations; if (!registration) { throw new ResolveException("Type not registered.", resolveType); diff --git a/source/poodinis/registration.d b/source/poodinis/registration.d index 9617352..24f4658 100644 --- a/source/poodinis/registration.d +++ b/source/poodinis/registration.d @@ -7,6 +7,11 @@ module poodinis.registration; +debug { + import std.stdio; + import std.string; +} + class Registration { TypeInfo registeredType = null; TypeInfo_Class instantiatableType = null; @@ -38,6 +43,9 @@ interface CreationScope { class NullScope : CreationScope { public Object getInstance() { + debug { + writeln("DEBUG: No instance created (NullScope)"); + } return null; } } @@ -52,9 +60,17 @@ class SingleInstanceScope : CreationScope { public Object getInstance() { if (instance is null) { + debug { + writeln(format("DEBUG: Creating new instance of type %s (SingleInstanceScope)", instantiatableType.toString())); + } instance = instantiatableType.create(); + } else { + debug { + writeln(format("DEBUG: Existing instance returned of type %s (SingleInstanceScope)", instantiatableType.toString())); + } } + return instance; } } @@ -72,6 +88,9 @@ class NewInstanceScope : CreationScope { } public Object getInstance() { + debug { + writeln(format("DEBUG: Creating new instance of type %s (SingleInstanceScope)", instantiatableType.toString())); + } return instantiatableType.create(); } } @@ -89,6 +108,9 @@ class ExistingInstanceScope : CreationScope { } public Object getInstance() { + debug { + writeln("DEBUG: Existing instance returned (ExistingInstanceScope)"); + } return instance; } }