From 7a628c32c484e0ed298526e2f67dd5f99a018921 Mon Sep 17 00:00:00 2001 From: Mike Bierlee Date: Tue, 23 Dec 2014 23:03:59 +0100 Subject: [PATCH] Add debug specifier to reduce verbosity of debug output --- dub.json | 11 ++ source/poodinis/autowire.d | 9 +- source/poodinis/dependency.d | 4 +- source/poodinis/registration.d | 264 ++++++++++++++++----------------- 4 files changed, 150 insertions(+), 138 deletions(-) diff --git a/dub.json b/dub.json index ac644a5..394f774 100644 --- a/dub.json +++ b/dub.json @@ -19,6 +19,17 @@ "dflags-dmd": [ "-main" ] + }, + { + "name": "unittestVerbose", + "targetType": "executable", + "debugVersions": ["poodinisVerbose"], + "sourcePaths": [ + "test" + ], + "dflags-dmd": [ + "-main" + ] } ] } \ No newline at end of file diff --git a/source/poodinis/autowire.d b/source/poodinis/autowire.d index 8bf929d..24f84fb 100644 --- a/source/poodinis/autowire.d +++ b/source/poodinis/autowire.d @@ -27,7 +27,7 @@ alias Autowired = Autowire; public void autowire(Type)(DependencyContainer container, Type instance) { // For the love of god, refactor this! - debug { + debug(poodinisVerbose) { auto instanceType = typeid(Type); auto instanceAddress = &instance; writeln(format("DEBUG: Autowiring members of [%s@%s]", instanceType, instanceAddress)); @@ -40,7 +40,8 @@ public void autowire(Type)(DependencyContainer container, Type instance) { if (__traits(getMember, instance, member) is null) { alias memberReference = TypeTuple!(__traits(getMember, instance, member)); alias MemberType = typeof(memberReference)[0]; - debug { + + debug(poodinisVerbose) { string qualifiedInstanceTypeString = typeid(MemberType).toString; } @@ -48,7 +49,7 @@ public void autowire(Type)(DependencyContainer container, Type instance) { static if (is(autowireAttribute == Autowire!T, T) && !is(autowireAttribute.qualifier == UseMemberType)) { alias QualifierType = typeof(autowireAttribute.qualifier); qualifiedInstance = container.resolve!(typeof(memberReference), QualifierType); - debug { + debug(poodinisVerbose) { qualifiedInstanceTypeString = typeid(QualifierType).toString; } } else { @@ -57,7 +58,7 @@ public void autowire(Type)(DependencyContainer container, Type instance) { __traits(getMember, instance, member) = qualifiedInstance; - debug { + debug(poodinisVerbose) { auto qualifiedInstanceAddress = &qualifiedInstance; writeln(format("DEBUG: Autowired instance [%s@%s] to [%s@%s].%s", qualifiedInstanceTypeString, qualifiedInstanceAddress, instanceType, instanceAddress, member)); } diff --git a/source/poodinis/dependency.d b/source/poodinis/dependency.d index 1e91bed..ceb3559 100644 --- a/source/poodinis/dependency.d +++ b/source/poodinis/dependency.d @@ -49,7 +49,7 @@ class DependencyContainer { TypeInfo registeredType = typeid(InterfaceType); TypeInfo_Class concreteType = typeid(ConcreteType); - debug { + debug(poodinisVerbose) { writeln(format("DEBUG: Register type %s (as %s)", concreteType.toString(), registeredType.toString())); } @@ -85,7 +85,7 @@ class DependencyContainer { TypeInfo resolveType = typeid(RegistrationType); TypeInfo qualifierType = typeid(QualifierType); - debug { + debug(poodinisVerbose) { writeln("DEBUG: Resolving type " ~ resolveType.toString() ~ " with qualifier " ~ qualifierType.toString()); } diff --git a/source/poodinis/registration.d b/source/poodinis/registration.d index c36848b..015c5ea 100644 --- a/source/poodinis/registration.d +++ b/source/poodinis/registration.d @@ -1,132 +1,132 @@ -/** - * Poodinis Dependency Injection Framework - * Copyright 2014 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.registration; - -debug { - import std.stdio; - import std.string; -} - -class Registration { - TypeInfo registeredType = null; - TypeInfo_Class instantiatableType = null; - CreationScope registationScope = null; - - this(TypeInfo registeredType, TypeInfo_Class instantiatableType) { - this.registeredType = registeredType; - this.instantiatableType = instantiatableType; - } - - public Object getInstance() { - if (registationScope is null) { - throw new NoScopeDefinedException(registeredType); - } - - return registationScope.getInstance(); - } -} - -class NoScopeDefinedException : Exception { - this(TypeInfo type) { - super("No scope defined for registration of type " ~ type.toString()); - } -} - -interface CreationScope { - public Object getInstance(); -} - -class NullScope : CreationScope { - public Object getInstance() { - debug { - writeln("DEBUG: No instance created (NullScope)"); - } - return null; - } -} - -class SingleInstanceScope : CreationScope { - TypeInfo_Class instantiatableType = null; - Object instance = null; - - this(TypeInfo_Class instantiatableType) { - this.instantiatableType = instantiatableType; - } - - 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; - } -} - -public Registration singleInstance(Registration registration) { - registration.registationScope = new SingleInstanceScope(registration.instantiatableType); - return registration; -} - -class NewInstanceScope : CreationScope { - TypeInfo_Class instantiatableType = null; - - this(TypeInfo_Class instantiatableType) { - this.instantiatableType = instantiatableType; - } - - public Object getInstance() { - debug { - writeln(format("DEBUG: Creating new instance of type %s (SingleInstanceScope)", instantiatableType.toString())); - } - return instantiatableType.create(); - } -} - -public Registration newInstance(Registration registration) { - registration.registationScope = new NewInstanceScope(registration.instantiatableType); - return registration; -} - -class ExistingInstanceScope : CreationScope { - Object instance = null; - - this(Object instance) { - this.instance = instance; - } - - public Object getInstance() { - debug { - writeln("DEBUG: Existing instance returned (ExistingInstanceScope)"); - } - return instance; - } -} - -public Registration existingInstance(Registration registration, Object instance) { - registration.registationScope = new ExistingInstanceScope(instance); - return registration; -} - -public string toConcreteTypeListString(Registration[] registrations) { - auto concreteTypeListString = ""; - foreach (registration ; registrations) { - if (concreteTypeListString.length > 0) { - concreteTypeListString ~= ", "; - } - concreteTypeListString ~= registration.instantiatableType.toString(); - } - return concreteTypeListString; -} +/** + * Poodinis Dependency Injection Framework + * Copyright 2014 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.registration; + +debug { + import std.stdio; + import std.string; +} + +class Registration { + TypeInfo registeredType = null; + TypeInfo_Class instantiatableType = null; + CreationScope registationScope = null; + + this(TypeInfo registeredType, TypeInfo_Class instantiatableType) { + this.registeredType = registeredType; + this.instantiatableType = instantiatableType; + } + + public Object getInstance() { + if (registationScope is null) { + throw new NoScopeDefinedException(registeredType); + } + + return registationScope.getInstance(); + } +} + +class NoScopeDefinedException : Exception { + this(TypeInfo type) { + super("No scope defined for registration of type " ~ type.toString()); + } +} + +interface CreationScope { + public Object getInstance(); +} + +class NullScope : CreationScope { + public Object getInstance() { + debug(poodinisVerbose) { + writeln("DEBUG: No instance created (NullScope)"); + } + return null; + } +} + +class SingleInstanceScope : CreationScope { + TypeInfo_Class instantiatableType = null; + Object instance = null; + + this(TypeInfo_Class instantiatableType) { + this.instantiatableType = instantiatableType; + } + + public Object getInstance() { + if (instance is null) { + debug(poodinisVerbose) { + writeln(format("DEBUG: Creating new instance of type %s (SingleInstanceScope)", instantiatableType.toString())); + } + instance = instantiatableType.create(); + } else { + debug(poodinisVerbose) { + writeln(format("DEBUG: Existing instance returned of type %s (SingleInstanceScope)", instantiatableType.toString())); + } + } + + + return instance; + } +} + +public Registration singleInstance(Registration registration) { + registration.registationScope = new SingleInstanceScope(registration.instantiatableType); + return registration; +} + +class NewInstanceScope : CreationScope { + TypeInfo_Class instantiatableType = null; + + this(TypeInfo_Class instantiatableType) { + this.instantiatableType = instantiatableType; + } + + public Object getInstance() { + debug(poodinisVerbose) { + writeln(format("DEBUG: Creating new instance of type %s (SingleInstanceScope)", instantiatableType.toString())); + } + return instantiatableType.create(); + } +} + +public Registration newInstance(Registration registration) { + registration.registationScope = new NewInstanceScope(registration.instantiatableType); + return registration; +} + +class ExistingInstanceScope : CreationScope { + Object instance = null; + + this(Object instance) { + this.instance = instance; + } + + public Object getInstance() { + debug(poodinisVerbose) { + writeln("DEBUG: Existing instance returned (ExistingInstanceScope)"); + } + return instance; + } +} + +public Registration existingInstance(Registration registration, Object instance) { + registration.registationScope = new ExistingInstanceScope(instance); + return registration; +} + +public string toConcreteTypeListString(Registration[] registrations) { + auto concreteTypeListString = ""; + foreach (registration ; registrations) { + if (concreteTypeListString.length > 0) { + concreteTypeListString ~= ", "; + } + concreteTypeListString ~= registration.instantiatableType.toString(); + } + return concreteTypeListString; +}