poodinis/source/poodinis/registration.d
2014-06-01 19:05:02 +02:00

100 lines
2.3 KiB
D

/**
* 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;
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() {
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) {
instance = instantiatableType.create();
}
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() {
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() {
return instance;
}
}
public Registration existingInstance(Registration registration, Object instance) {
registration.registationScope = new ExistingInstanceScope(instance);
return registration;
}