mirror of
https://github.com/mbierlee/poodinis.git
synced 2024-11-15 04:04:01 +01:00
Rename and deprecate "container" module
This commit is contained in:
parent
b2bb6f15c4
commit
4e8555c7da
|
@ -7,7 +7,7 @@
|
|||
|
||||
module poodinis.autowire;
|
||||
|
||||
public import poodinis.container;
|
||||
public import poodinis.dependency;
|
||||
|
||||
import std.typetuple;
|
||||
|
||||
|
|
|
@ -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;
|
||||
|
|
98
source/poodinis/dependency.d
Normal file
98
source/poodinis/dependency.d
Normal 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;
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
|
Loading…
Reference in a new issue