mirror of
https://github.com/mbierlee/poodinis.git
synced 2024-11-15 04:04:01 +01:00
Use instantiation context to decide whether to autowire or not
This commit is contained in:
parent
9d931f511b
commit
5ebb5805cf
|
@ -92,9 +92,20 @@ class AutowiredRegistration(RegistrationType : Object) : Registration {
|
||||||
super(registeredType, typeid(RegistrationType));
|
super(registeredType, typeid(RegistrationType));
|
||||||
}
|
}
|
||||||
|
|
||||||
public override Object getInstance() {
|
public override Object getInstance(InstantiationContext context = new AutowireInstantiationContext()) {
|
||||||
RegistrationType instance = cast(RegistrationType) super.getInstance();
|
RegistrationType instance = cast(RegistrationType) super.getInstance(context);
|
||||||
container.autowire(instance);
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
return instance;
|
return instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
class AutowireInstantiationContext : InstantiationContext {
|
||||||
|
public bool autowireInstance = true;
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,17 +16,17 @@ class Registration {
|
||||||
TypeInfo registeredType = null;
|
TypeInfo registeredType = null;
|
||||||
TypeInfo_Class instantiatableType = null;
|
TypeInfo_Class instantiatableType = null;
|
||||||
CreationScope registationScope = null;
|
CreationScope registationScope = null;
|
||||||
|
|
||||||
this(TypeInfo registeredType, TypeInfo_Class instantiatableType) {
|
this(TypeInfo registeredType, TypeInfo_Class instantiatableType) {
|
||||||
this.registeredType = registeredType;
|
this.registeredType = registeredType;
|
||||||
this.instantiatableType = instantiatableType;
|
this.instantiatableType = instantiatableType;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Object getInstance() {
|
public Object getInstance(InstantiationContext context = new InstantiationContext()) {
|
||||||
if (registationScope is null) {
|
if (registationScope is null) {
|
||||||
throw new NoScopeDefinedException(registeredType);
|
throw new NoScopeDefinedException(registeredType);
|
||||||
}
|
}
|
||||||
|
|
||||||
return registationScope.getInstance();
|
return registationScope.getInstance();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -53,11 +53,11 @@ class NullScope : CreationScope {
|
||||||
class SingleInstanceScope : CreationScope {
|
class SingleInstanceScope : CreationScope {
|
||||||
TypeInfo_Class instantiatableType = null;
|
TypeInfo_Class instantiatableType = null;
|
||||||
Object instance = null;
|
Object instance = null;
|
||||||
|
|
||||||
this(TypeInfo_Class instantiatableType) {
|
this(TypeInfo_Class instantiatableType) {
|
||||||
this.instantiatableType = instantiatableType;
|
this.instantiatableType = instantiatableType;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Object getInstance() {
|
public Object getInstance() {
|
||||||
if (instance is null) {
|
if (instance is null) {
|
||||||
debug(poodinisVerbose) {
|
debug(poodinisVerbose) {
|
||||||
|
@ -69,8 +69,8 @@ class SingleInstanceScope : CreationScope {
|
||||||
writeln(format("DEBUG: Existing instance returned of type %s (SingleInstanceScope)", instantiatableType.toString()));
|
writeln(format("DEBUG: Existing instance returned of type %s (SingleInstanceScope)", instantiatableType.toString()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
return instance;
|
return instance;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -82,11 +82,11 @@ public Registration singleInstance(Registration registration) {
|
||||||
|
|
||||||
class NewInstanceScope : CreationScope {
|
class NewInstanceScope : CreationScope {
|
||||||
TypeInfo_Class instantiatableType = null;
|
TypeInfo_Class instantiatableType = null;
|
||||||
|
|
||||||
this(TypeInfo_Class instantiatableType) {
|
this(TypeInfo_Class instantiatableType) {
|
||||||
this.instantiatableType = instantiatableType;
|
this.instantiatableType = instantiatableType;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Object getInstance() {
|
public Object getInstance() {
|
||||||
debug(poodinisVerbose) {
|
debug(poodinisVerbose) {
|
||||||
writeln(format("DEBUG: Creating new instance of type %s (SingleInstanceScope)", instantiatableType.toString()));
|
writeln(format("DEBUG: Creating new instance of type %s (SingleInstanceScope)", instantiatableType.toString()));
|
||||||
|
@ -102,11 +102,11 @@ public Registration newInstance(Registration registration) {
|
||||||
|
|
||||||
class ExistingInstanceScope : CreationScope {
|
class ExistingInstanceScope : CreationScope {
|
||||||
Object instance = null;
|
Object instance = null;
|
||||||
|
|
||||||
this(Object instance) {
|
this(Object instance) {
|
||||||
this.instance = instance;
|
this.instance = instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Object getInstance() {
|
public Object getInstance() {
|
||||||
debug(poodinisVerbose) {
|
debug(poodinisVerbose) {
|
||||||
writeln("DEBUG: Existing instance returned (ExistingInstanceScope)");
|
writeln("DEBUG: Existing instance returned (ExistingInstanceScope)");
|
||||||
|
@ -130,3 +130,5 @@ public string toConcreteTypeListString(Registration[] registrations) {
|
||||||
}
|
}
|
||||||
return concreteTypeListString;
|
return concreteTypeListString;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class InstantiationContext {}
|
||||||
|
|
|
@ -179,7 +179,7 @@ version(unittest) {
|
||||||
container.register!ComponentA;
|
container.register!ComponentA;
|
||||||
|
|
||||||
auto registration = new AutowiredRegistration!ComponentB(typeid(ComponentB), container).singleInstance();
|
auto registration = new AutowiredRegistration!ComponentB(typeid(ComponentB), container).singleInstance();
|
||||||
auto instance = cast(ComponentB) registration.getInstance();
|
auto instance = cast(ComponentB) registration.getInstance(new AutowireInstantiationContext());
|
||||||
|
|
||||||
assert(!instance.componentIsNull());
|
assert(!instance.componentIsNull());
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue