Move factory parameters to parameter object

This commit is contained in:
Mike Bierlee 2016-08-17 21:56:52 +02:00
parent 44e77aff9c
commit 6b3b23a376
5 changed files with 52 additions and 26 deletions

View file

@ -180,7 +180,8 @@ private void autowireMember(string member, size_t memberIndex, Type)(shared(Depe
private QualifierType createOrResolveInstance(MemberType, QualifierType, bool createNew, bool isOptional)(shared(DependencyContainer) container) { private QualifierType createOrResolveInstance(MemberType, QualifierType, bool createNew, bool isOptional)(shared(DependencyContainer) container) {
static if (createNew) { static if (createNew) {
auto instanceFactory = new InstanceFactory(typeid(MemberType), CreatesSingleton.no, null); auto instanceFactory = new InstanceFactory();
instanceFactory.factoryParameters = InstanceFactoryParameters(typeid(MemberType), CreatesSingleton.no);
return cast(MemberType) instanceFactory.getInstance(); return cast(MemberType) instanceFactory.getInstance();
} else { } else {
static if (isOptional) { static if (isOptional) {

View file

@ -62,7 +62,9 @@ public void registerContextComponents(ApplicationContextType : ApplicationContex
registration = container.register!(ReturnType!factoryMethod); registration = container.register!(ReturnType!factoryMethod);
} }
registration.instanceFactory = new InstanceFactory(registration.instanceType, createsSingleton, null, factoryMethod); auto instanceFactory = new InstanceFactory();
instanceFactory.factoryParameters = InstanceFactoryParameters(registration.instanceType, createsSingleton, null, factoryMethod);
registration.instanceFactory = instanceFactory;
} }
} }
} }

View file

@ -28,38 +28,53 @@ class InstanceCreationException : Exception {
} }
} }
class InstanceFactory { struct InstanceFactoryParameters {
private TypeInfo_Class instanceType = null; TypeInfo_Class instanceType;
private Object instance = null; CreatesSingleton createsSingleton = CreatesSingleton.yes;
private CreatesSingleton createsSingleton; Object existingInstance;
private InstanceFactoryMethod factoryMethod; InstanceFactoryMethod factoryMethod;
}
this(TypeInfo_Class instanceType, CreatesSingleton createsSingleton = CreatesSingleton.yes, Object existingInstance = null, InstanceFactoryMethod factoryMethod = null) { class InstanceFactory {
this.instanceType = instanceType; private Object instance = null;
this.createsSingleton = existingInstance !is null ? CreatesSingleton.yes : createsSingleton; private InstanceFactoryParameters _factoryParameters;
this.instance = existingInstance;
this.factoryMethod = factoryMethod !is null ? factoryMethod : &this.createInstance; public @property void factoryParameters(InstanceFactoryParameters factoryParameters) {
if (factoryParameters.factoryMethod is null) {
factoryParameters.factoryMethod = &this.createInstance;
}
if (factoryParameters.existingInstance !is null) {
factoryParameters.createsSingleton = CreatesSingleton.yes;
this.instance = factoryParameters.existingInstance;
}
this.factoryParameters = factoryParameters;
}
public @property InstanceFactoryParameters factoryParameters() {
return _factoryParameters;
} }
public Object getInstance() { public Object getInstance() {
if (createsSingleton && instance !is null) { if (_factoryParameters.createsSingleton && instance !is null) {
debug(poodinisVerbose) { debug(poodinisVerbose) {
writeln(format("DEBUG: Existing instance returned of type %s", instanceType.toString())); writeln(format("DEBUG: Existing instance returned of type %s", _factoryParameters.instanceType.toString()));
} }
return instance; return instance;
} }
debug(poodinisVerbose) { debug(poodinisVerbose) {
writeln(format("DEBUG: Creating new instance of type %s", instanceType.toString())); writeln(format("DEBUG: Creating new instance of type %s", _factoryParameters.instanceType.toString()));
} }
instance = factoryMethod(); instance = _factoryParameters.factoryMethod();
return instance; return instance;
} }
private Object createInstance() { private Object createInstance() {
enforce!InstanceCreationException(instanceType, "Instance type is not defined, cannot create instance without knowing its type."); enforce!InstanceCreationException(_factoryParameters.instanceType, "Instance type is not defined, cannot create instance without knowing its type.");
return instanceType.create(); return _factoryParameters.instanceType.create();
} }
} }

View file

@ -67,7 +67,8 @@ class Registration {
* Effectively makes the given registration a singleton. * Effectively makes the given registration a singleton.
*/ */
public Registration singleInstance(Registration registration) { public Registration singleInstance(Registration registration) {
registration.instanceFactory = new InstanceFactory(registration.instanceType, CreatesSingleton.yes, null); registration.instanceFactory = new InstanceFactory();
registration.instanceFactory.factoryParameters = InstanceFactoryParameters(registration.instanceType, CreatesSingleton.yes);
return registration; return registration;
} }
@ -75,7 +76,8 @@ public Registration singleInstance(Registration registration) {
* Scopes registrations to return a new instance every time the given registration is resolved. * Scopes registrations to return a new instance every time the given registration is resolved.
*/ */
public Registration newInstance(Registration registration) { public Registration newInstance(Registration registration) {
registration.instanceFactory = new InstanceFactory(registration.instanceType, CreatesSingleton.no, null); registration.instanceFactory = new InstanceFactory();
registration.instanceFactory.factoryParameters = InstanceFactoryParameters(registration.instanceType, CreatesSingleton.no);
return registration; return registration;
} }
@ -83,7 +85,8 @@ public Registration newInstance(Registration registration) {
* Scopes registrations to return the given instance every time the given registration is resolved. * Scopes registrations to return the given instance every time the given registration is resolved.
*/ */
public Registration existingInstance(Registration registration, Object instance) { public Registration existingInstance(Registration registration, Object instance) {
registration.instanceFactory = new InstanceFactory(registration.instanceType, CreatesSingleton.yes, instance); registration.instanceFactory = new InstanceFactory();
registration.instanceFactory.factoryParameters = InstanceFactoryParameters(registration.instanceType, CreatesSingleton.yes, instance);
return registration; return registration;
} }

View file

@ -17,7 +17,8 @@ version(unittest) {
// Test instance factory with singletons // Test instance factory with singletons
unittest { unittest {
auto factory = new InstanceFactory(typeid(TestImplementation), CreatesSingleton.yes, null); auto factory = new InstanceFactory();
factory.factoryParameters = InstanceFactoryParameters(typeid(TestImplementation), CreatesSingleton.yes);
auto instanceOne = factory.getInstance(); auto instanceOne = factory.getInstance();
auto instanceTwo = factory.getInstance(); auto instanceTwo = factory.getInstance();
@ -27,7 +28,8 @@ version(unittest) {
// Test instance factory with new instances // Test instance factory with new instances
unittest { unittest {
auto factory = new InstanceFactory(typeid(TestImplementation), CreatesSingleton.no, null); auto factory = new InstanceFactory();
factory.factoryParameters = InstanceFactoryParameters(typeid(TestImplementation), CreatesSingleton.no);
auto instanceOne = factory.getInstance(); auto instanceOne = factory.getInstance();
auto instanceTwo = factory.getInstance(); auto instanceTwo = factory.getInstance();
@ -38,7 +40,8 @@ version(unittest) {
// Test instance factory with existing instances // Test instance factory with existing instances
unittest { unittest {
auto existingInstance = new TestImplementation(); auto existingInstance = new TestImplementation();
auto factory = new InstanceFactory(typeid(TestImplementation), CreatesSingleton.yes, existingInstance); auto factory = new InstanceFactory();
factory.factoryParameters = InstanceFactoryParameters(typeid(TestImplementation), CreatesSingleton.yes, existingInstance);
auto instanceOne = factory.getInstance(); auto instanceOne = factory.getInstance();
auto instanceTwo = factory.getInstance(); auto instanceTwo = factory.getInstance();
@ -49,7 +52,8 @@ version(unittest) {
// Test instance factory with existing instances when setting singleton flag to "no" // Test instance factory with existing instances when setting singleton flag to "no"
unittest { unittest {
auto existingInstance = new TestImplementation(); auto existingInstance = new TestImplementation();
auto factory = new InstanceFactory(typeid(TestImplementation), CreatesSingleton.no, existingInstance); auto factory = new InstanceFactory();
factory.factoryParameters = InstanceFactoryParameters(typeid(TestImplementation), CreatesSingleton.no, existingInstance);
auto instance = factory.getInstance(); auto instance = factory.getInstance();
assert(instance is existingInstance, "Created factory instance is not the existing instance"); assert(instance is existingInstance, "Created factory instance is not the existing instance");
@ -63,7 +67,8 @@ version(unittest) {
return instance; return instance;
} }
auto factory = new InstanceFactory(null, CreatesSingleton.yes, null, &factoryMethod); auto factory = new InstanceFactory();
factory.factoryParameters = InstanceFactoryParameters(null, CreatesSingleton.yes, null, &factoryMethod);
auto instance = cast(TestImplementation) factory.getInstance(); auto instance = cast(TestImplementation) factory.getInstance();
assert(instance !is null, "No instance was created by factory or could not be cast to expected type"); assert(instance !is null, "No instance was created by factory or could not be cast to expected type");