From 81c6faed163fb3467848d6e7124a80888dafe01a Mon Sep 17 00:00:00 2001 From: Mike Bierlee Date: Wed, 17 Aug 2016 23:01:22 +0200 Subject: [PATCH] Pass factory through constructor to prevent nullpointer exceptions --- source/poodinis/autowire.d | 4 ++-- source/poodinis/container.d | 3 +-- source/poodinis/context.d | 4 +--- source/poodinis/registration.d | 8 ++++++-- test/poodinis/autowiretest.d | 2 +- test/poodinis/registrationtest.d | 12 ++++++------ 6 files changed, 17 insertions(+), 16 deletions(-) diff --git a/source/poodinis/autowire.d b/source/poodinis/autowire.d index 7ebd618..3c6223d 100644 --- a/source/poodinis/autowire.d +++ b/source/poodinis/autowire.d @@ -205,8 +205,8 @@ public deprecated void globalAutowire(Type)(Type instance) { class AutowiredRegistration(RegistrationType : Object) : Registration { private shared(DependencyContainer) container; - public this(TypeInfo registeredType, shared(DependencyContainer) originatingContainer) { - super(registeredType, typeid(RegistrationType), originatingContainer); + public this(TypeInfo registeredType, InstanceFactory instanceFactory, shared(DependencyContainer) originatingContainer) { + super(registeredType, typeid(RegistrationType), instanceFactory, originatingContainer); } public override Object getInstance(InstantiationContext context = new AutowireInstantiationContext()) { diff --git a/source/poodinis/container.d b/source/poodinis/container.d index d5b8aca..94fca9f 100644 --- a/source/poodinis/container.d +++ b/source/poodinis/container.d @@ -147,8 +147,7 @@ synchronized class DependencyContainer { return existingRegistration; } - auto newRegistration = new AutowiredRegistration!ConcreteType(registeredType, this); - newRegistration.instanceFactory = new InstanceFactory(); + auto newRegistration = new AutowiredRegistration!ConcreteType(registeredType, new InstanceFactory(), this); newRegistration.singleInstance(); static if (!is(SuperType == ConcreteType)) { diff --git a/source/poodinis/context.d b/source/poodinis/context.d index a953c4a..c384d4c 100644 --- a/source/poodinis/context.d +++ b/source/poodinis/context.d @@ -62,9 +62,7 @@ public void registerContextComponents(ApplicationContextType : ApplicationContex registration = container.register!(ReturnType!factoryMethod); } - auto instanceFactory = new InstanceFactory(); - instanceFactory.factoryParameters = InstanceFactoryParameters(registration.instanceType, createsSingleton, null, factoryMethod); - registration.instanceFactory = instanceFactory; + registration.instanceFactory.factoryParameters = InstanceFactoryParameters(registration.instanceType, createsSingleton, null, factoryMethod); } } } diff --git a/source/poodinis/registration.d b/source/poodinis/registration.d index 5bcab36..6c9b0b6 100644 --- a/source/poodinis/registration.d +++ b/source/poodinis/registration.d @@ -21,6 +21,7 @@ class Registration { private TypeInfo_Class _instanceType = null; private Registration linkedRegistration; private shared(DependencyContainer) _originatingContainer; + private InstanceFactory _instanceFactory; public @property registeredType() { return _registeredType; @@ -34,12 +35,15 @@ class Registration { return _originatingContainer; } - public InstanceFactory instanceFactory = null; + public @property instanceFactory() { + return _instanceFactory; + } - this(TypeInfo registeredType, TypeInfo_Class instanceType, shared(DependencyContainer) originatingContainer) { + 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()) { diff --git a/test/poodinis/autowiretest.d b/test/poodinis/autowiretest.d index b9e643c..dd9720f 100644 --- a/test/poodinis/autowiretest.d +++ b/test/poodinis/autowiretest.d @@ -185,7 +185,7 @@ version(unittest) { auto container = new shared DependencyContainer(); container.register!ComponentA; - auto registration = new AutowiredRegistration!ComponentB(typeid(ComponentB), container).singleInstance(); + auto registration = new AutowiredRegistration!ComponentB(typeid(ComponentB), new InstanceFactory(), container).singleInstance(); auto instance = cast(ComponentB) registration.getInstance(new AutowireInstantiationContext()); assert(instance.componentA !is null); diff --git a/test/poodinis/registrationtest.d b/test/poodinis/registrationtest.d index f21474f..ce9a1dd 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, null); + Registration registration = new Registration(typeid(TestType), null, null, null); assertThrown!(InstanceCreationException)(registration.getInstance(), null); } // Test set single instance scope using scope setter unittest { - Registration registration = new Registration(null, typeid(TestType), null); + Registration registration = new Registration(null, typeid(TestType), new InstanceFactory(), 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), null); + Registration registration = new Registration(null, typeid(TestType), new InstanceFactory(), 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, null); + Registration registration = new Registration(null, null, new InstanceFactory(), 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), null).singleInstance(); - Registration secondRegistration = new Registration(typeid(TestImplementation), typeid(TestImplementation), null).singleInstance().linkTo(firstRegistration); + Registration firstRegistration = new Registration(typeid(TestInterface), typeid(TestImplementation), new InstanceFactory(), null).singleInstance(); + Registration secondRegistration = new Registration(typeid(TestImplementation), typeid(TestImplementation), new InstanceFactory(), null).singleInstance().linkTo(firstRegistration); auto firstInstance = firstRegistration.getInstance(); auto secondInstance = secondRegistration.getInstance();