diff --git a/source/poodinis/autowire.d b/source/poodinis/autowire.d index 77b86a2..b8dc35b 100644 --- a/source/poodinis/autowire.d +++ b/source/poodinis/autowire.d @@ -7,7 +7,7 @@ module poodinis.autowire; -public import poodinis.container; +public import poodinis.dependency; import std.typetuple; diff --git a/source/poodinis/container.d b/source/poodinis/container.d index 2225df2..510edd8 100644 --- a/source/poodinis/container.d +++ b/source/poodinis/container.d @@ -1,98 +1,5 @@ -/** - * 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.container; - -import std.string; -import std.array; -import std.algorithm; - -debug { - import std.stdio; -} - -public import poodinis.registration; -public import poodinis.autowire; - -class RegistrationException : Exception { - this(string message, TypeInfo registeredType, TypeInfo_Class instantiatableType) { - super(format("Exception while registering type %s to %s: %s", registeredType.toString(), instantiatableType.name, message)); - } -} - -class ResolveException : Exception { - this(string message, TypeInfo resolveType) { - super(format("Exception while resolving type %s: %s", resolveType.toString(), message)); - } -} - -deprecated("Container has been renamed to DependencyContainer") -alias Container = DependencyContainer; - -class DependencyContainer { - - private static DependencyContainer instance; - - private Registration[TypeInfo] registrations; - - private Registration*[] autowireStack; - - public Registration register(ConcreteType)() { - return register!(ConcreteType, ConcreteType)(); - } - - public Registration register(InterfaceType, ConcreteType : InterfaceType)() { - 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; - return newRegistration; - } - - 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); - } - - RegistrationType instance = cast(RegistrationType) registration.getInstance(); - - if (!autowireStack.canFind(registration)) { - autowireStack ~= registration; - this.autowire!(RegistrationType)(instance); - autowireStack.popBack(); - } - - return instance; - } - - public void clearAllRegistrations() { - registrations.destroy(); - } - - public void removeRegistration(RegistrationType)() { - registrations.remove(typeid(RegistrationType)); - } - - public static DependencyContainer getInstance() { - if (instance is null) { - instance = new DependencyContainer(); - } - return instance; - } -} +module poodinis.container; + +pragma(msg, "Module \"container\" has been renamed to \"dependency\". The use of the \"container\" module is deprecated"); + +public import poodinis.dependency; diff --git a/source/poodinis/dependency.d b/source/poodinis/dependency.d new file mode 100644 index 0000000..7dd0665 --- /dev/null +++ b/source/poodinis/dependency.d @@ -0,0 +1,98 @@ +/** + * 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.dependency; + +import std.string; +import std.array; +import std.algorithm; + +debug { + import std.stdio; +} + +public import poodinis.registration; +public import poodinis.autowire; + +class RegistrationException : Exception { + this(string message, TypeInfo registeredType, TypeInfo_Class instantiatableType) { + super(format("Exception while registering type %s to %s: %s", registeredType.toString(), instantiatableType.name, message)); + } +} + +class ResolveException : Exception { + this(string message, TypeInfo resolveType) { + super(format("Exception while resolving type %s: %s", resolveType.toString(), message)); + } +} + +deprecated("Container has been renamed to DependencyContainer") +alias Container = DependencyContainer; + +class DependencyContainer { + + private static DependencyContainer instance; + + private Registration[TypeInfo] registrations; + + private Registration*[] autowireStack; + + public Registration register(ConcreteType)() { + return register!(ConcreteType, ConcreteType)(); + } + + public Registration register(InterfaceType, ConcreteType : InterfaceType)() { + 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; + return newRegistration; + } + + 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); + } + + RegistrationType instance = cast(RegistrationType) registration.getInstance(); + + if (!autowireStack.canFind(registration)) { + autowireStack ~= registration; + this.autowire!(RegistrationType)(instance); + autowireStack.popBack(); + } + + return instance; + } + + public void clearAllRegistrations() { + registrations.destroy(); + } + + public void removeRegistration(RegistrationType)() { + registrations.remove(typeid(RegistrationType)); + } + + public static DependencyContainer getInstance() { + if (instance is null) { + instance = new DependencyContainer(); + } + return instance; + } +} diff --git a/test/poodinis/containertest.d b/test/poodinis/dependencytest.d similarity index 96% rename from test/poodinis/containertest.d rename to test/poodinis/dependencytest.d index 59a9bff..c253532 100644 --- a/test/poodinis/containertest.d +++ b/test/poodinis/dependencytest.d @@ -5,7 +5,7 @@ * The full terms of the license can be found in the LICENSE file. */ -import poodinis.container; +import poodinis.dependency; import std.exception;