poodinis/source/poodinis/registration.d

115 lines
3.6 KiB
D
Raw Normal View History

2016-01-06 20:28:25 +01:00
/**
* This module contains objects for defining and scoping dependency registrations.
*
* Part of the Poodinis Dependency Injection framework.
*
* Authors:
* Mike Bierlee, m.bierlee@lostmoment.com
2021-01-04 00:04:20 +01:00
* Copyright: 2014-2021 Mike Bierlee
2016-01-06 20:28:25 +01:00
* License:
* 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;
import poodinis.container;
2016-08-17 20:44:36 +02:00
import poodinis.factory;
2016-01-06 20:28:25 +01:00
class Registration {
private TypeInfo _registeredType = null;
private TypeInfo_Class _instanceType = null;
private Registration linkedRegistration;
private shared(DependencyContainer) _originatingContainer;
private InstanceFactory _instanceFactory;
private void delegate() _preDestructor;
public @property registeredType() {
return _registeredType;
}
public @property instanceType() {
return _instanceType;
}
public @property originatingContainer() {
return _originatingContainer;
}
public @property instanceFactory() {
return _instanceFactory;
}
public @property preDestructor() {
return _preDestructor;
}
protected @property preDestructor(void delegate() preDestructor) {
_preDestructor = preDestructor;
}
this(TypeInfo registeredType, TypeInfo_Class instanceType, InstanceFactory instanceFactory, shared(DependencyContainer) originatingContainer) {
this._registeredType = registeredType;
this._instanceType = instanceType;
this._originatingContainer = originatingContainer;
this._instanceFactory = instanceFactory;
}
public Object getInstance(InstantiationContext context = new InstantiationContext()) {
if (linkedRegistration !is null) {
return linkedRegistration.getInstance(context);
}
if (instanceFactory is null) {
throw new InstanceCreationException("No instance factory defined for registration of type " ~ registeredType.toString());
}
return instanceFactory.getInstance();
}
public Registration linkTo(Registration registration) {
this.linkedRegistration = registration;
return this;
}
2016-01-06 20:28:25 +01:00
}
/**
* Scopes registrations to return the same instance every time a given registration is resolved.
*
* Effectively makes the given registration a singleton.
*/
public Registration singleInstance(Registration registration) {
registration.instanceFactory.factoryParameters = InstanceFactoryParameters(registration.instanceType, CreatesSingleton.yes);
return registration;
2016-01-06 20:28:25 +01:00
}
/**
* Scopes registrations to return a new instance every time the given registration is resolved.
*/
public Registration newInstance(Registration registration) {
registration.instanceFactory.factoryParameters = InstanceFactoryParameters(registration.instanceType, CreatesSingleton.no);
return registration;
2016-01-06 20:28:25 +01:00
}
/**
* Scopes registrations to return the given instance every time the given registration is resolved.
*/
public Registration existingInstance(Registration registration, Object instance) {
registration.instanceFactory.factoryParameters = InstanceFactoryParameters(registration.instanceType, CreatesSingleton.yes, instance);
return registration;
2016-01-06 20:28:25 +01:00
}
public string toConcreteTypeListString(Registration[] registrations) {
auto concreteTypeListString = "";
foreach (registration ; registrations) {
if (concreteTypeListString.length > 0) {
concreteTypeListString ~= ", ";
}
concreteTypeListString ~= registration.instanceType.toString();
}
return concreteTypeListString;
2016-01-06 20:28:25 +01:00
}
class InstantiationContext {}