Rename and deprecate "container" module

This commit is contained in:
Mike Bierlee 2014-10-25 16:43:32 +02:00
parent b2bb6f15c4
commit 4e8555c7da
4 changed files with 105 additions and 100 deletions

View file

@ -7,7 +7,7 @@
module poodinis.autowire;
public import poodinis.container;
public import poodinis.dependency;
import std.typetuple;

View file

@ -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;
pragma(msg, "Module \"container\" has been renamed to \"dependency\". The use of the \"container\" module is deprecated");
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;
}
}
public import poodinis.dependency;

View file

@ -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;
}
}

View file

@ -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;