Add ability to mark autowire dependencies as optional

This commit is contained in:
Mike Bierlee 2016-02-15 22:49:57 +01:00
parent 6746fd64a0
commit 7308702dfe
5 changed files with 303 additions and 223 deletions

View file

@ -1,5 +1,9 @@
Poodinis Changelog Poodinis Changelog
================== ==================
**Version NEXT**
* ADD ability to mark autowire dependencies as optional. When you use UDA @OptionalDependency, a type which fails to autowire will remain null
(or an empty array). No ResolveException is thrown.
**Version 6.1.0** **Version 6.1.0**
* ADD setting persistent registration and resolve options * ADD setting persistent registration and resolve options
* DEPRECATE DO_NOT_ADD_CONCRETE_TYPE_REGISTRATION, use doNotAddConcreteTypeRegistration instead * DEPRECATE DO_NOT_ADD_CONCRETE_TYPE_REGISTRATION, use doNotAddConcreteTypeRegistration instead

View file

@ -94,6 +94,17 @@ assert(instance.dependency !is null);
``` ```
If an interface is to be autowired, you must register a concrete class by interface. Any class registered by concrete type can only be injected when a dependency on a concrete type is autowired. If an interface is to be autowired, you must register a concrete class by interface. Any class registered by concrete type can only be injected when a dependency on a concrete type is autowired.
Using the UDA `OptionalDependency` you can mark an autowired member as being optional. When a member is optional, no ResolveException will be thrown when
the type of the member is not registered and `ResolveOption.registerBeforeResolving` is not set on the container. The member will remain null or an empty array in
case of array dependencies.
```d
class ExampleClass {
@Autowire
@OptionalDependency
private AnotherExampleClass dependency;
}
```
Circular dependencies Circular dependencies
--------------------- ---------------------
Poodinis can autowire circular dependencies when they are registered with `singleInstance` or `existingInstance` registration scopes. Circular dependencies in registrations with `newInstance` scopes will not be autowired, as this would cause an endless loop. Poodinis can autowire circular dependencies when they are registered with `singleInstance` or `existingInstance` registration scopes. Circular dependencies in registrations with `newInstance` scopes will not be autowired, as this would cause an endless loop.

View file

@ -12,6 +12,12 @@ import std.digest.md;
import std.stdio; import std.stdio;
import std.conv; import std.conv;
class SecurityAuditor {
public void submitAudit() {
writeln("Hmmmyes I have received your audit. It is.... adequate.");
}
}
class SuperSecurityDevice { class SuperSecurityDevice {
private int seed; private int seed;
@ -32,6 +38,18 @@ class SecurityManager {
@Autowire @Autowire
@AssignNewInstance @AssignNewInstance
private SuperSecurityDevice levelTwoSecurity; private SuperSecurityDevice levelTwoSecurity;
@Autowire
@OptionalDependency
private SecurityAuditor auditor;
public void doAudit() {
if (auditor !is null) {
auditor.submitAudit();
} else {
writeln("I uh, will skip the audit for now...");
}
}
} }
void main() { void main() {
@ -45,6 +63,10 @@ void main() {
writeln("Password for user two: " ~ manager.levelTwoSecurity.getPassword()); writeln("Password for user two: " ~ manager.levelTwoSecurity.getPassword());
if (manager.levelOneSecurity is manager.levelTwoSecurity) { if (manager.levelOneSecurity is manager.levelTwoSecurity) {
writeln("SECURITY BREACH!!!!!"); writeln("SECURITY BREACH!!!!!"); // Should not be printed since levelTwoSecurity is a new instance.
} else {
writeln("Security okay!");
} }
manager.doAudit(); // Will not cause the SecurityAuditor to print, since we didn't register a SecurityAuditor.
} }

View file

@ -65,6 +65,14 @@ struct Autowire(QualifierType = UseMemberType) {
QualifierType qualifier; QualifierType qualifier;
}; };
/**
* UDA for marking autowired dependencies optional.
* Optional dependencies will not lead to a resolveException when there is no type registered for them.
* The member will remain null.
*/
struct OptionalDependency {};
/** /**
* UDA for annotating class members to be autowired with a new instance regardless of their registration scope. * UDA for annotating class members to be autowired with a new instance regardless of their registration scope.
* *
@ -127,10 +135,15 @@ private void autowireMember(string member, size_t memberIndex, Type)(shared(Depe
alias MemberType = typeof(Type.tupleof[memberIndex]); alias MemberType = typeof(Type.tupleof[memberIndex]);
enum assignNewInstance = hasUDA!(Type.tupleof[memberIndex], AssignNewInstance); enum assignNewInstance = hasUDA!(Type.tupleof[memberIndex], AssignNewInstance);
enum isOptional = hasUDA!(Type.tupleof[memberIndex], OptionalDependency);
static if (isDynamicArray!MemberType) { static if (isDynamicArray!MemberType) {
alias MemberElementType = ElementType!MemberType; alias MemberElementType = ElementType!MemberType;
auto instances = container.resolveAll!MemberElementType; static if (isOptional) {
auto instances = container.resolveAll!MemberElementType([ResolveOption.noResolveException]);
} else {
auto instances = container.resolveAll!MemberElementType;
}
instance.tupleof[memberIndex] = instances; instance.tupleof[memberIndex] = instances;
debug(poodinisVerbose) { debug(poodinisVerbose) {
printDebugAutowiringArray(typeid(MemberElementType), typeid(Type), &instance, member); printDebugAutowiringArray(typeid(MemberElementType), typeid(Type), &instance, member);
@ -143,12 +156,12 @@ private void autowireMember(string member, size_t memberIndex, Type)(shared(Depe
MemberType qualifiedInstance; MemberType qualifiedInstance;
static if (is(autowireAttribute == Autowire!T, T) && !is(autowireAttribute.qualifier == UseMemberType)) { static if (is(autowireAttribute == Autowire!T, T) && !is(autowireAttribute.qualifier == UseMemberType)) {
alias QualifierType = typeof(autowireAttribute.qualifier); alias QualifierType = typeof(autowireAttribute.qualifier);
qualifiedInstance = createOrResolveInstance!(MemberType, QualifierType, assignNewInstance)(container); qualifiedInstance = createOrResolveInstance!(MemberType, QualifierType, assignNewInstance, isOptional)(container);
debug(poodinisVerbose) { debug(poodinisVerbose) {
qualifiedInstanceType = typeid(QualifierType); qualifiedInstanceType = typeid(QualifierType);
} }
} else { } else {
qualifiedInstance = createOrResolveInstance!(MemberType, MemberType, assignNewInstance)(container); qualifiedInstance = createOrResolveInstance!(MemberType, MemberType, assignNewInstance, isOptional)(container);
} }
instance.tupleof[memberIndex] = qualifiedInstance; instance.tupleof[memberIndex] = qualifiedInstance;
@ -164,12 +177,16 @@ private void autowireMember(string member, size_t memberIndex, Type)(shared(Depe
} }
} }
private QualifierType createOrResolveInstance(MemberType, QualifierType, bool createNew)(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(typeid(MemberType), CreatesSingleton.no, null);
return cast(MemberType) instanceFactory.getInstance(); return cast(MemberType) instanceFactory.getInstance();
} else { } else {
return container.resolve!(MemberType, QualifierType); static if (isOptional) {
return container.resolve!(MemberType, QualifierType)([ResolveOption.noResolveException]);
} else {
return container.resolve!(MemberType, QualifierType);
}
} }
} }

View file

@ -1,217 +1,243 @@
/** /**
* Poodinis Dependency Injection Framework * Poodinis Dependency Injection Framework
* Copyright 2014-2016 Mike Bierlee * Copyright 2014-2016 Mike Bierlee
* This software is licensed under the terms of the MIT license. * This software is licensed under the terms of the MIT license.
* The full terms of the license can be found in the LICENSE file. * The full terms of the license can be found in the LICENSE file.
*/ */
import poodinis; import poodinis;
import std.exception; import std.exception;
version(unittest) { version(unittest) {
class ComponentA {} class ComponentA {}
class ComponentB { class ComponentB {
public @Autowire ComponentA componentA; public @Autowire ComponentA componentA;
} }
interface InterfaceA {} interface InterfaceA {}
class ComponentC : InterfaceA {} class ComponentC : InterfaceA {}
class ComponentD { class ComponentD {
public @Autowire InterfaceA componentC = null; public @Autowire InterfaceA componentC = null;
private @Autowire InterfaceA privateComponentC = null; private @Autowire InterfaceA privateComponentC = null;
} }
class DummyAttribute{}; class DummyAttribute{};
class ComponentE { class ComponentE {
@DummyAttribute @DummyAttribute
public ComponentC componentC; public ComponentC componentC;
} }
class ComponentDeclarationCocktail { class ComponentDeclarationCocktail {
alias noomer = int; alias noomer = int;
@Autowire @Autowire
public ComponentA componentA; public ComponentA componentA;
public void doesNothing() { public void doesNothing() {
} }
~this(){ ~this(){
} }
} }
class ComponentX : InterfaceA {} class ComponentX : InterfaceA {}
class ComponentZ : ComponentB { class ComponentZ : ComponentB {
} }
class MonkeyShine { class MonkeyShine {
@Autowire!ComponentX @Autowire!ComponentX
public InterfaceA component; public InterfaceA component;
} }
class BootstrapBootstrap { class BootstrapBootstrap {
@Autowire!ComponentX @Autowire!ComponentX
public InterfaceA componentX; public InterfaceA componentX;
@Autowire!ComponentC @Autowire!ComponentC
public InterfaceA componentC; public InterfaceA componentC;
} }
class LordOfTheComponents { class LordOfTheComponents {
@Autowire @Autowire
public InterfaceA[] components; public InterfaceA[] components;
} }
class ComponentCharlie { class ComponentCharlie {
@Autowire @Autowire
@AssignNewInstance @AssignNewInstance
public ComponentA componentA; public ComponentA componentA;
} }
// Test autowiring concrete type to existing instance class OuttaTime {
unittest { @Autowire
shared(DependencyContainer) container = new DependencyContainer(); @OptionalDependency
container.register!ComponentA; public InterfaceA interfaceA;
auto componentB = new ComponentB();
container.autowire(componentB); @Autowire
assert(componentB !is null, "Autowirable dependency failed to autowire"); @OptionalDependency
} public ComponentA componentA;
// Test autowiring interface type to existing instance @Autowire
unittest { @OptionalDependency
shared(DependencyContainer) container = new DependencyContainer(); public ComponentC[] componentCs;
container.register!(InterfaceA, ComponentC); }
auto componentD = new ComponentD();
container.autowire(componentD); // Test autowiring concrete type to existing instance
assert(componentD.componentC !is null, "Autowirable dependency failed to autowire"); unittest {
} shared(DependencyContainer) container = new DependencyContainer();
container.register!ComponentA;
// Test autowiring private members auto componentB = new ComponentB();
unittest { container.autowire(componentB);
shared(DependencyContainer) container = new DependencyContainer(); assert(componentB !is null, "Autowirable dependency failed to autowire");
container.register!(InterfaceA, ComponentC); }
auto componentD = new ComponentD();
container.autowire(componentD); // Test autowiring interface type to existing instance
assert(componentD.privateComponentC is componentD.componentC, "Autowire private dependency failed"); unittest {
} shared(DependencyContainer) container = new DependencyContainer();
container.register!(InterfaceA, ComponentC);
// Test autowiring will only happen once auto componentD = new ComponentD();
unittest { container.autowire(componentD);
shared(DependencyContainer) container = new DependencyContainer(); assert(componentD.componentC !is null, "Autowirable dependency failed to autowire");
container.register!(InterfaceA, ComponentC).newInstance(); }
auto componentD = new ComponentD();
container.autowire(componentD); // Test autowiring private members
auto expectedComponent = componentD.componentC; unittest {
container.autowire(componentD); shared(DependencyContainer) container = new DependencyContainer();
auto actualComponent = componentD.componentC; container.register!(InterfaceA, ComponentC);
assert(expectedComponent is actualComponent, "Autowiring the second time wired a different instance"); auto componentD = new ComponentD();
} container.autowire(componentD);
assert(componentD.privateComponentC is componentD.componentC, "Autowire private dependency failed");
// Test autowiring unregistered type }
unittest {
shared(DependencyContainer) container = new DependencyContainer(); // Test autowiring will only happen once
auto componentD = new ComponentD(); unittest {
assertThrown!(ResolveException)(container.autowire(componentD), "Autowiring unregistered type should throw ResolveException"); shared(DependencyContainer) container = new DependencyContainer();
} container.register!(InterfaceA, ComponentC).newInstance();
auto componentD = new ComponentD();
// Test autowiring member with non-autowire attribute does not autowire container.autowire(componentD);
unittest { auto expectedComponent = componentD.componentC;
shared(DependencyContainer) container = new DependencyContainer(); container.autowire(componentD);
auto componentE = new ComponentE(); auto actualComponent = componentD.componentC;
container.autowire(componentE); assert(expectedComponent is actualComponent, "Autowiring the second time wired a different instance");
assert(componentE.componentC is null, "Autowiring should not occur for members with attributes other than @Autowire"); }
}
// Test autowiring unregistered type
// Test autowire class with alias declaration unittest {
unittest { shared(DependencyContainer) container = new DependencyContainer();
shared(DependencyContainer) container = new DependencyContainer(); auto componentD = new ComponentD();
container.register!ComponentA; assertThrown!(ResolveException)(container.autowire(componentD), "Autowiring unregistered type should throw ResolveException");
auto componentDeclarationCocktail = new ComponentDeclarationCocktail(); }
container.autowire(componentDeclarationCocktail); // Test autowiring member with non-autowire attribute does not autowire
unittest {
assert(componentDeclarationCocktail.componentA !is null, "Autowiring class with non-assignable declarations failed"); shared(DependencyContainer) container = new DependencyContainer();
} auto componentE = new ComponentE();
container.autowire(componentE);
// Test autowire class with qualifier assert(componentE.componentC is null, "Autowiring should not occur for members with attributes other than @Autowire");
unittest { }
shared(DependencyContainer) container = new DependencyContainer();
container.register!(InterfaceA, ComponentC); // Test autowire class with alias declaration
container.register!(InterfaceA, ComponentX); unittest {
auto componentX = container.resolve!(InterfaceA, ComponentX); shared(DependencyContainer) container = new DependencyContainer();
container.register!ComponentA;
auto monkeyShine = new MonkeyShine(); auto componentDeclarationCocktail = new ComponentDeclarationCocktail();
container.autowire(monkeyShine);
container.autowire(componentDeclarationCocktail);
assert(monkeyShine.component is componentX, "Autowiring class with qualifier failed");
} assert(componentDeclarationCocktail.componentA !is null, "Autowiring class with non-assignable declarations failed");
}
// Test autowire class with multiple qualifiers
unittest { // Test autowire class with qualifier
shared(DependencyContainer) container = new DependencyContainer(); unittest {
container.register!(InterfaceA, ComponentC); shared(DependencyContainer) container = new DependencyContainer();
container.register!(InterfaceA, ComponentX); container.register!(InterfaceA, ComponentC);
auto componentC = container.resolve!(InterfaceA, ComponentC); container.register!(InterfaceA, ComponentX);
auto componentX = container.resolve!(InterfaceA, ComponentX); auto componentX = container.resolve!(InterfaceA, ComponentX);
auto bootstrapBootstrap = new BootstrapBootstrap(); auto monkeyShine = new MonkeyShine();
container.autowire(bootstrapBootstrap); container.autowire(monkeyShine);
assert(bootstrapBootstrap.componentX is componentX, "Autowiring class with multiple qualifiers failed"); assert(monkeyShine.component is componentX, "Autowiring class with qualifier failed");
assert(bootstrapBootstrap.componentC is componentC, "Autowiring class with multiple qualifiers failed"); }
}
// Test autowire class with multiple qualifiers
// Test getting instance from autowired registration will autowire instance unittest {
unittest { shared(DependencyContainer) container = new DependencyContainer();
shared(DependencyContainer) container = new DependencyContainer(); container.register!(InterfaceA, ComponentC);
container.register!ComponentA; container.register!(InterfaceA, ComponentX);
auto componentC = container.resolve!(InterfaceA, ComponentC);
auto registration = new AutowiredRegistration!ComponentB(typeid(ComponentB), container).singleInstance(); auto componentX = container.resolve!(InterfaceA, ComponentX);
auto instance = cast(ComponentB) registration.getInstance(new AutowireInstantiationContext());
auto bootstrapBootstrap = new BootstrapBootstrap();
assert(instance.componentA !is null); container.autowire(bootstrapBootstrap);
}
assert(bootstrapBootstrap.componentX is componentX, "Autowiring class with multiple qualifiers failed");
// Test autowiring a dynamic array with all qualified types assert(bootstrapBootstrap.componentC is componentC, "Autowiring class with multiple qualifiers failed");
unittest { }
shared(DependencyContainer) container = new DependencyContainer();
container.register!(InterfaceA, ComponentC); // Test getting instance from autowired registration will autowire instance
container.register!(InterfaceA, ComponentX); unittest {
shared(DependencyContainer) container = new DependencyContainer();
auto lord = new LordOfTheComponents(); container.register!ComponentA;
container.autowire(lord);
auto registration = new AutowiredRegistration!ComponentB(typeid(ComponentB), container).singleInstance();
assert(lord.components.length == 2, "Dynamic array was not autowired"); auto instance = cast(ComponentB) registration.getInstance(new AutowireInstantiationContext());
}
assert(instance.componentA !is null);
// Test autowiring new instance of singleinstance registration with newInstance UDA }
unittest {
shared(DependencyContainer) container = new DependencyContainer(); // Test autowiring a dynamic array with all qualified types
container.register!ComponentA; unittest {
shared(DependencyContainer) container = new DependencyContainer();
auto regularComponentA = container.resolve!ComponentA; container.register!(InterfaceA, ComponentC);
auto charlie = new ComponentCharlie(); container.register!(InterfaceA, ComponentX);
container.autowire(charlie); auto lord = new LordOfTheComponents();
container.autowire(lord);
assert(charlie.componentA !is regularComponentA, "Autowiring class with AssignNewInstance did not yield a different instance");
} assert(lord.components.length == 2, "Dynamic array was not autowired");
}
// Test autowiring members from base class
unittest { // Test autowiring new instance of singleinstance registration with newInstance UDA
shared(DependencyContainer) container = new DependencyContainer(); unittest {
container.register!ComponentA; shared(DependencyContainer) container = new DependencyContainer();
container.register!ComponentB; container.register!ComponentA;
container.register!ComponentZ;
auto regularComponentA = container.resolve!ComponentA;
auto instance = new ComponentZ(); auto charlie = new ComponentCharlie();
container.autowire(instance);
container.autowire(charlie);
assert(instance.componentA !is null);
} assert(charlie.componentA !is regularComponentA, "Autowiring class with AssignNewInstance did not yield a different instance");
} }
// Test autowiring members from base class
unittest {
shared(DependencyContainer) container = new DependencyContainer();
container.register!ComponentA;
container.register!ComponentB;
container.register!ComponentZ;
auto instance = new ComponentZ();
container.autowire(instance);
assert(instance.componentA !is null);
}
// Test autowiring optional depenencies
unittest {
shared(DependencyContainer) container = new DependencyContainer();
auto instance = new OuttaTime();
container.autowire(instance);
assert(instance.interfaceA is null);
assert(instance.componentA is null);
assert(instance.componentCs is null);
}
}