diff --git a/source/poodinis/autowire.d b/source/poodinis/autowire.d index 1fe1ed9..a579561 100644 --- a/source/poodinis/autowire.d +++ b/source/poodinis/autowire.d @@ -203,19 +203,19 @@ public deprecated void globalAutowire(Type)(Type instance) { class AutowiredRegistration(RegistrationType : Object) : Registration { private shared(DependencyContainer) container; - public this(TypeInfo registeredType, shared(DependencyContainer) container) { - enforce(!(container is null), "Argument 'container' is null. Autowired registrations need to autowire using a container."); - this.container = container; - super(registeredType, typeid(RegistrationType)); + public this(TypeInfo registeredType, shared(DependencyContainer) originatingContainer) { + super(registeredType, typeid(RegistrationType), originatingContainer); } 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); 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!"); if (autowireContext.autowireInstance) { - container.autowire(instance); + originatingContainer.autowire(instance); } return instance; diff --git a/source/poodinis/registration.d b/source/poodinis/registration.d index ba23c2d..26fa684 100644 --- a/source/poodinis/registration.d +++ b/source/poodinis/registration.d @@ -13,6 +13,8 @@ module poodinis.registration; +import poodinis.container; + import std.typecons; import std.exception; @@ -31,6 +33,7 @@ class Registration { private TypeInfo _registeredType = null; private TypeInfo_Class _instanceType = null; private Registration linkedRegistration; + private shared(DependencyContainer) _originatingContainer; public @property registeredType() { return _registeredType; @@ -40,11 +43,16 @@ class Registration { return _instanceType; } + public @property originatingContainer() { + return _originatingContainer; + } + public InstanceFactory instanceFactory = null; - this(TypeInfo registeredType, TypeInfo_Class instanceType) { + this(TypeInfo registeredType, TypeInfo_Class instanceType, shared(DependencyContainer) originatingContainer) { this._registeredType = registeredType; this._instanceType = instanceType; + this._originatingContainer = originatingContainer; } public Object getInstance(InstantiationContext context = new InstantiationContext()) { diff --git a/test/poodinis/registrationtest.d b/test/poodinis/registrationtest.d index 20be117..6c3ef77 100644 --- a/test/poodinis/registrationtest.d +++ b/test/poodinis/registrationtest.d @@ -20,13 +20,13 @@ version(unittest) { // Test getting instance without scope defined throws exception unittest { - Registration registration = new Registration(typeid(TestType), null); - assertThrown!(InstanceCreationException)(registration.getInstance()); + Registration registration = new Registration(typeid(TestType), null, null); + assertThrown!(InstanceCreationException)(registration.getInstance(), null); } // Test set single instance scope using scope setter unittest { - Registration registration = new Registration(null, typeid(TestType)); + Registration registration = new Registration(null, typeid(TestType), null); auto chainedRegistration = registration.singleInstance(); auto instance1 = registration.getInstance(); auto instance2 = registration.getInstance(); @@ -36,7 +36,7 @@ version(unittest) { // Test set new instance scope using scope setter unittest { - Registration registration = new Registration(null, typeid(TestType)); + Registration registration = new Registration(null, typeid(TestType), null); auto chainedRegistration = registration.newInstance(); auto instance1 = registration.getInstance(); auto instance2 = registration.getInstance(); @@ -46,7 +46,7 @@ version(unittest) { // Test set existing instance scope using scope setter unittest { - Registration registration = new Registration(null, null); + Registration registration = new Registration(null, null, null); auto expectedInstance = new TestType(); auto chainedRegistration = registration.existingInstance(expectedInstance); auto actualInstance = registration.getInstance(); @@ -56,8 +56,8 @@ version(unittest) { // Test linking registrations unittest { - Registration firstRegistration = new Registration(typeid(TestInterface), typeid(TestImplementation)).singleInstance(); - Registration secondRegistration = new Registration(typeid(TestImplementation), typeid(TestImplementation)).singleInstance().linkTo(firstRegistration); + Registration firstRegistration = new Registration(typeid(TestInterface), typeid(TestImplementation), null).singleInstance(); + Registration secondRegistration = new Registration(typeid(TestImplementation), typeid(TestImplementation), null).singleInstance().linkTo(firstRegistration); auto firstInstance = firstRegistration.getInstance(); auto secondInstance = secondRegistration.getInstance();