Pull keeping track of originating containmer up to Registration class

This commit is contained in:
Mike Bierlee 2016-08-08 23:06:05 +02:00
parent 2a4004856f
commit b6ccc9d4a9
3 changed files with 21 additions and 13 deletions

View file

@ -203,19 +203,19 @@ public deprecated void globalAutowire(Type)(Type instance) {
class AutowiredRegistration(RegistrationType : Object) : Registration { class AutowiredRegistration(RegistrationType : Object) : Registration {
private shared(DependencyContainer) container; private shared(DependencyContainer) container;
public this(TypeInfo registeredType, shared(DependencyContainer) container) { public this(TypeInfo registeredType, shared(DependencyContainer) originatingContainer) {
enforce(!(container is null), "Argument 'container' is null. Autowired registrations need to autowire using a container."); super(registeredType, typeid(RegistrationType), originatingContainer);
this.container = container;
super(registeredType, typeid(RegistrationType));
} }
public override Object getInstance(InstantiationContext context = new AutowireInstantiationContext()) { public override Object getInstance(InstantiationContext context = new AutowireInstantiationContext()) {
enforce(!(originatingContainer is null), "The registration's originating container is null. There is no way to resolve autowire dependencies.");
RegistrationType instance = cast(RegistrationType) super.getInstance(context); RegistrationType instance = cast(RegistrationType) super.getInstance(context);
AutowireInstantiationContext autowireContext = cast(AutowireInstantiationContext) context; AutowireInstantiationContext autowireContext = cast(AutowireInstantiationContext) context;
enforce(!(autowireContext is null), "Given instantiation context type could not be cast to an AutowireInstantiationContext. If you relied on using the default assigned context: make sure you're calling getInstance() on an instance of type AutowiredRegistration!"); enforce(!(autowireContext is null), "Given instantiation context type could not be cast to an AutowireInstantiationContext. If you relied on using the default assigned context: make sure you're calling getInstance() on an instance of type AutowiredRegistration!");
if (autowireContext.autowireInstance) { if (autowireContext.autowireInstance) {
container.autowire(instance); originatingContainer.autowire(instance);
} }
return instance; return instance;

View file

@ -13,6 +13,8 @@
module poodinis.registration; module poodinis.registration;
import poodinis.container;
import std.typecons; import std.typecons;
import std.exception; import std.exception;
@ -31,6 +33,7 @@ class Registration {
private TypeInfo _registeredType = null; private TypeInfo _registeredType = null;
private TypeInfo_Class _instanceType = null; private TypeInfo_Class _instanceType = null;
private Registration linkedRegistration; private Registration linkedRegistration;
private shared(DependencyContainer) _originatingContainer;
public @property registeredType() { public @property registeredType() {
return _registeredType; return _registeredType;
@ -40,11 +43,16 @@ class Registration {
return _instanceType; return _instanceType;
} }
public @property originatingContainer() {
return _originatingContainer;
}
public InstanceFactory instanceFactory = null; public InstanceFactory instanceFactory = null;
this(TypeInfo registeredType, TypeInfo_Class instanceType) { this(TypeInfo registeredType, TypeInfo_Class instanceType, shared(DependencyContainer) originatingContainer) {
this._registeredType = registeredType; this._registeredType = registeredType;
this._instanceType = instanceType; this._instanceType = instanceType;
this._originatingContainer = originatingContainer;
} }
public Object getInstance(InstantiationContext context = new InstantiationContext()) { public Object getInstance(InstantiationContext context = new InstantiationContext()) {

View file

@ -20,13 +20,13 @@ version(unittest) {
// Test getting instance without scope defined throws exception // Test getting instance without scope defined throws exception
unittest { unittest {
Registration registration = new Registration(typeid(TestType), null); Registration registration = new Registration(typeid(TestType), null, null);
assertThrown!(InstanceCreationException)(registration.getInstance()); assertThrown!(InstanceCreationException)(registration.getInstance(), null);
} }
// Test set single instance scope using scope setter // Test set single instance scope using scope setter
unittest { unittest {
Registration registration = new Registration(null, typeid(TestType)); Registration registration = new Registration(null, typeid(TestType), null);
auto chainedRegistration = registration.singleInstance(); auto chainedRegistration = registration.singleInstance();
auto instance1 = registration.getInstance(); auto instance1 = registration.getInstance();
auto instance2 = registration.getInstance(); auto instance2 = registration.getInstance();
@ -36,7 +36,7 @@ version(unittest) {
// Test set new instance scope using scope setter // Test set new instance scope using scope setter
unittest { unittest {
Registration registration = new Registration(null, typeid(TestType)); Registration registration = new Registration(null, typeid(TestType), null);
auto chainedRegistration = registration.newInstance(); auto chainedRegistration = registration.newInstance();
auto instance1 = registration.getInstance(); auto instance1 = registration.getInstance();
auto instance2 = registration.getInstance(); auto instance2 = registration.getInstance();
@ -46,7 +46,7 @@ version(unittest) {
// Test set existing instance scope using scope setter // Test set existing instance scope using scope setter
unittest { unittest {
Registration registration = new Registration(null, null); Registration registration = new Registration(null, null, null);
auto expectedInstance = new TestType(); auto expectedInstance = new TestType();
auto chainedRegistration = registration.existingInstance(expectedInstance); auto chainedRegistration = registration.existingInstance(expectedInstance);
auto actualInstance = registration.getInstance(); auto actualInstance = registration.getInstance();
@ -56,8 +56,8 @@ version(unittest) {
// Test linking registrations // Test linking registrations
unittest { unittest {
Registration firstRegistration = new Registration(typeid(TestInterface), typeid(TestImplementation)).singleInstance(); Registration firstRegistration = new Registration(typeid(TestInterface), typeid(TestImplementation), null).singleInstance();
Registration secondRegistration = new Registration(typeid(TestImplementation), typeid(TestImplementation)).singleInstance().linkTo(firstRegistration); Registration secondRegistration = new Registration(typeid(TestImplementation), typeid(TestImplementation), null).singleInstance().linkTo(firstRegistration);
auto firstInstance = firstRegistration.getInstance(); auto firstInstance = firstRegistration.getInstance();
auto secondInstance = secondRegistration.getInstance(); auto secondInstance = secondRegistration.getInstance();