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
|
|
|
|
* Copyright: 2014-2016 Mike Bierlee
|
|
|
|
* 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;
|
|
|
|
|
2016-08-08 23:06:05 +02:00
|
|
|
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;
|
2016-08-08 23:06:05 +02:00
|
|
|
private shared(DependencyContainer) _originatingContainer;
|
2016-08-17 23:01:22 +02:00
|
|
|
private InstanceFactory _instanceFactory;
|
2016-12-17 23:09:56 +01:00
|
|
|
private void delegate() _preDestructor;
|
2016-01-06 20:28:25 +01:00
|
|
|
|
|
|
|
public @property registeredType() {
|
|
|
|
return _registeredType;
|
|
|
|
}
|
|
|
|
|
|
|
|
public @property instanceType() {
|
|
|
|
return _instanceType;
|
|
|
|
}
|
|
|
|
|
2016-08-08 23:06:05 +02:00
|
|
|
public @property originatingContainer() {
|
|
|
|
return _originatingContainer;
|
|
|
|
}
|
|
|
|
|
2016-08-17 23:01:22 +02:00
|
|
|
public @property instanceFactory() {
|
|
|
|
return _instanceFactory;
|
|
|
|
}
|
2016-01-06 20:28:25 +01:00
|
|
|
|
2016-12-17 23:09:56 +01:00
|
|
|
public @property preDestructor() {
|
|
|
|
return _preDestructor;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected @property preDestructor(void delegate() preDestructor) {
|
|
|
|
_preDestructor = preDestructor;
|
|
|
|
}
|
|
|
|
|
2016-08-17 23:01:22 +02:00
|
|
|
this(TypeInfo registeredType, TypeInfo_Class instanceType, InstanceFactory instanceFactory, shared(DependencyContainer) originatingContainer) {
|
2016-01-06 20:28:25 +01:00
|
|
|
this._registeredType = registeredType;
|
|
|
|
this._instanceType = instanceType;
|
2016-08-08 23:06:05 +02:00
|
|
|
this._originatingContainer = originatingContainer;
|
2016-08-17 23:01:22 +02:00
|
|
|
this._instanceFactory = instanceFactory;
|
2016-01-06 20:28:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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) {
|
2016-08-17 21:56:52 +02:00
|
|
|
registration.instanceFactory.factoryParameters = InstanceFactoryParameters(registration.instanceType, CreatesSingleton.yes);
|
2016-01-06 20:28:25 +01:00
|
|
|
return registration;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Scopes registrations to return a new instance every time the given registration is resolved.
|
|
|
|
*/
|
|
|
|
public Registration newInstance(Registration registration) {
|
2016-08-17 21:56:52 +02:00
|
|
|
registration.instanceFactory.factoryParameters = InstanceFactoryParameters(registration.instanceType, CreatesSingleton.no);
|
2016-01-06 20:28:25 +01:00
|
|
|
return registration;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Scopes registrations to return the given instance every time the given registration is resolved.
|
|
|
|
*/
|
|
|
|
public Registration existingInstance(Registration registration, Object instance) {
|
2016-08-17 21:56:52 +02:00
|
|
|
registration.instanceFactory.factoryParameters = InstanceFactoryParameters(registration.instanceType, CreatesSingleton.yes, instance);
|
2016-01-06 20:28:25 +01:00
|
|
|
return registration;
|
|
|
|
}
|
|
|
|
|
|
|
|
public string toConcreteTypeListString(Registration[] registrations) {
|
|
|
|
auto concreteTypeListString = "";
|
|
|
|
foreach (registration ; registrations) {
|
|
|
|
if (concreteTypeListString.length > 0) {
|
|
|
|
concreteTypeListString ~= ", ";
|
|
|
|
}
|
|
|
|
concreteTypeListString ~= registration.instanceType.toString();
|
|
|
|
}
|
|
|
|
return concreteTypeListString;
|
|
|
|
}
|
|
|
|
|
|
|
|
class InstantiationContext {}
|