Pass factory through constructor to prevent nullpointer exceptions

This commit is contained in:
Mike Bierlee 2016-08-17 23:01:22 +02:00
parent 941c5b1961
commit 81c6faed16
6 changed files with 17 additions and 16 deletions

View file

@ -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()) {

View file

@ -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)) {

View file

@ -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);
}
}
}

View file

@ -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()) {

View file

@ -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);

View file

@ -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();