mirror of
https://github.com/mbierlee/poodinis.git
synced 2024-11-15 04:04:01 +01:00
Add factory method invocation to instance factory
This commit is contained in:
parent
aa8aef5c06
commit
80916c47fb
|
@ -67,16 +67,19 @@ class Registration {
|
||||||
}
|
}
|
||||||
|
|
||||||
alias CreatesSingleton = Flag!"CreatesSingleton";
|
alias CreatesSingleton = Flag!"CreatesSingleton";
|
||||||
|
alias InstanceFactoryMethod = Object delegate();
|
||||||
|
|
||||||
class InstanceFactory {
|
class InstanceFactory {
|
||||||
private TypeInfo_Class instanceType = null;
|
private TypeInfo_Class instanceType = null;
|
||||||
private Object instance = null;
|
private Object instance = null;
|
||||||
private CreatesSingleton createsSingleton;
|
private CreatesSingleton createsSingleton;
|
||||||
|
private InstanceFactoryMethod factoryMethod;
|
||||||
|
|
||||||
this(TypeInfo_Class instanceType, CreatesSingleton createsSingleton = CreatesSingleton.yes, Object existingInstance = null) {
|
this(TypeInfo_Class instanceType, CreatesSingleton createsSingleton = CreatesSingleton.yes, Object existingInstance = null, InstanceFactoryMethod factoryMethod = null) {
|
||||||
this.instanceType = instanceType;
|
this.instanceType = instanceType;
|
||||||
this.createsSingleton = existingInstance !is null ? CreatesSingleton.yes : createsSingleton;
|
this.createsSingleton = existingInstance !is null ? CreatesSingleton.yes : createsSingleton;
|
||||||
this.instance = existingInstance;
|
this.instance = existingInstance;
|
||||||
|
this.factoryMethod = factoryMethod !is null ? factoryMethod : &this.createInstance;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Object getInstance() {
|
public Object getInstance() {
|
||||||
|
@ -88,14 +91,18 @@ class InstanceFactory {
|
||||||
return instance;
|
return instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
enforce!InstanceCreationException(instanceType, "Instance type is not defined, cannot create instance without knowing its type.");
|
|
||||||
debug(poodinisVerbose) {
|
debug(poodinisVerbose) {
|
||||||
writeln(format("DEBUG: Creating new instance of type %s", instanceType.toString()));
|
writeln(format("DEBUG: Creating new instance of type %s", instanceType.toString()));
|
||||||
}
|
}
|
||||||
|
|
||||||
instance = instanceType.create();
|
instance = factoryMethod();
|
||||||
return instance;
|
return instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private Object createInstance() {
|
||||||
|
enforce!InstanceCreationException(instanceType, "Instance type is not defined, cannot create instance without knowing its type.");
|
||||||
|
return instanceType.create();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -14,7 +14,9 @@ version(unittest) {
|
||||||
|
|
||||||
interface TestInterface {}
|
interface TestInterface {}
|
||||||
|
|
||||||
class TestImplementation : TestInterface {}
|
class TestImplementation : TestInterface {
|
||||||
|
public string someContent = "";
|
||||||
|
}
|
||||||
|
|
||||||
// Test getting instance without scope defined throws exception
|
// Test getting instance without scope defined throws exception
|
||||||
unittest {
|
unittest {
|
||||||
|
@ -103,4 +105,19 @@ version(unittest) {
|
||||||
assert(instance is existingInstance, "Created factory instance is not the existing instance");
|
assert(instance is existingInstance, "Created factory instance is not the existing instance");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Test creating instance using custom factory method
|
||||||
|
unittest {
|
||||||
|
Object factoryMethod() {
|
||||||
|
auto instance = new TestImplementation();
|
||||||
|
instance.someContent = "Ducks!";
|
||||||
|
return instance;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto factory = new InstanceFactory(null, CreatesSingleton.yes, null, &factoryMethod);
|
||||||
|
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.someContent == "Ducks!");
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue