diff --git a/source/poodinis/autowire.d b/source/poodinis/autowire.d index 00f103c..1870c86 100644 --- a/source/poodinis/autowire.d +++ b/source/poodinis/autowire.d @@ -92,9 +92,20 @@ class AutowiredRegistration(RegistrationType : Object) : Registration { super(registeredType, typeid(RegistrationType)); } - public override Object getInstance() { - RegistrationType instance = cast(RegistrationType) super.getInstance(); - container.autowire(instance); + public override Object getInstance(InstantiationContext context = new AutowireInstantiationContext()) { + RegistrationType instance = cast(RegistrationType) super.getInstance(context); + + 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; } + +} + +class AutowireInstantiationContext : InstantiationContext { + public bool autowireInstance = true; } diff --git a/source/poodinis/registration.d b/source/poodinis/registration.d index 015c5ea..0989489 100644 --- a/source/poodinis/registration.d +++ b/source/poodinis/registration.d @@ -16,17 +16,17 @@ class Registration { TypeInfo registeredType = null; TypeInfo_Class instantiatableType = null; CreationScope registationScope = null; - + this(TypeInfo registeredType, TypeInfo_Class instantiatableType) { this.registeredType = registeredType; this.instantiatableType = instantiatableType; } - - public Object getInstance() { + + public Object getInstance(InstantiationContext context = new InstantiationContext()) { if (registationScope is null) { throw new NoScopeDefinedException(registeredType); } - + return registationScope.getInstance(); } } @@ -53,11 +53,11 @@ class NullScope : CreationScope { class SingleInstanceScope : CreationScope { TypeInfo_Class instantiatableType = null; Object instance = null; - + this(TypeInfo_Class instantiatableType) { this.instantiatableType = instantiatableType; } - + public Object getInstance() { if (instance is null) { debug(poodinisVerbose) { @@ -69,8 +69,8 @@ class SingleInstanceScope : CreationScope { writeln(format("DEBUG: Existing instance returned of type %s (SingleInstanceScope)", instantiatableType.toString())); } } - - + + return instance; } } @@ -82,11 +82,11 @@ public Registration singleInstance(Registration registration) { class NewInstanceScope : CreationScope { TypeInfo_Class instantiatableType = null; - + this(TypeInfo_Class instantiatableType) { this.instantiatableType = instantiatableType; } - + public Object getInstance() { debug(poodinisVerbose) { writeln(format("DEBUG: Creating new instance of type %s (SingleInstanceScope)", instantiatableType.toString())); @@ -102,11 +102,11 @@ public Registration newInstance(Registration registration) { class ExistingInstanceScope : CreationScope { Object instance = null; - + this(Object instance) { this.instance = instance; } - + public Object getInstance() { debug(poodinisVerbose) { writeln("DEBUG: Existing instance returned (ExistingInstanceScope)"); @@ -130,3 +130,5 @@ public string toConcreteTypeListString(Registration[] registrations) { } return concreteTypeListString; } + +class InstantiationContext {} diff --git a/test/poodinis/autowiretest.d b/test/poodinis/autowiretest.d index 0aa3cca..b45985b 100644 --- a/test/poodinis/autowiretest.d +++ b/test/poodinis/autowiretest.d @@ -179,7 +179,7 @@ version(unittest) { container.register!ComponentA; 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()); }