Fix autowiring classes with non-symbolic unassignable members (such as aliases)

This commit is contained in:
Mike Bierlee 2014-07-09 23:15:05 +02:00
parent af8e154dc5
commit b38bccc03c
2 changed files with 148 additions and 122 deletions

View file

@ -20,19 +20,21 @@ class Autowire{};
public void autowire(Type)(Container container, Type instance) { public void autowire(Type)(Container container, Type instance) {
foreach (member ; __traits(allMembers, Type)) { foreach (member ; __traits(allMembers, Type)) {
foreach (attribute; mixin(`__traits(getAttributes, Type.` ~ member ~ `)`) ) { static if(__traits(compiles, __traits( getMember, Type, member )) && __traits(compiles, __traits(getAttributes, __traits(getMember, Type, member )))) {
if (is(attribute : Autowire) && __traits(getMember, instance, member) is null){ foreach(attribute; __traits(getAttributes, __traits(getMember, Type, member))) {
alias TypeTuple!(__traits(getMember, instance, member)) memberReference; if (is(attribute : Autowire) && __traits(getMember, instance, member) is null){
auto autowirableInstance = container.resolve!(typeof(memberReference)); alias TypeTuple!(__traits(getMember, instance, member)) memberReference;
debug { auto autowirableInstance = container.resolve!(typeof(memberReference));
auto autowirableType = typeid(typeof(memberReference[0])); debug {
auto autowireableAddress = &autowirableInstance; auto autowirableType = typeid(typeof(memberReference[0]));
auto memberType = typeid(Type); auto autowireableAddress = &autowirableInstance;
auto instanceAddress = &instance; auto memberType = typeid(Type);
writeln(format("DEBUG: Autowire instance [%s@%s] to [%s@%s].%s", autowirableType, autowireableAddress, memberType, instanceAddress, member)); auto instanceAddress = &instance;
writeln(format("DEBUG: Autowire instance [%s@%s] to [%s@%s].%s", autowirableType, autowireableAddress, memberType, instanceAddress, member));
}
__traits(getMember, instance, member) = autowirableInstance;
} }
__traits(getMember, instance, member) = autowirableInstance;
} }
} }
} }

View file

@ -1,111 +1,135 @@
/** /**
* Poodinis Dependency Injection Framework * Poodinis Dependency Injection Framework
* Copyright 2014 Mike Bierlee * Copyright 2014 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.autowire; import poodinis.autowire;
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;
public bool componentIsNull() { public bool componentIsNull() {
return componentA is null; return componentA is null;
} }
} }
interface InterfaceA { interface InterfaceA {
} }
class ComponentC : InterfaceA { class ComponentC : InterfaceA {
} }
class ComponentD { class ComponentD {
public @Autowire InterfaceA componentC = null; public @Autowire InterfaceA componentC = null;
public bool componentIsNull() { public bool componentIsNull() {
return componentC is null; return componentC is null;
} }
} }
class DummyAttribute{}; class DummyAttribute{};
class ComponentE { class ComponentE {
@DummyAttribute @DummyAttribute
public ComponentC componentC; public ComponentC componentC;
} }
class ComponentF { class ComponentF {
@Autowire @Autowire
public ComponentA componentA; public ComponentA componentA;
mixin AutowireConstructor; mixin AutowireConstructor;
} }
// Test autowiring concrete type to existing instance class ComponentDeclarationCocktail {
unittest { alias noomer = int;
auto container = new Container();
container.register!ComponentA; @Autowire
auto componentB = new ComponentB(); public ComponentA componentA;
container.autowire!(ComponentB)(componentB);
assert(!componentB.componentIsNull(), "Autowirable dependency failed to autowire"); public void doesNothing() {
} }
// Test autowiring interface type to existing instance ~this(){
unittest { }
auto container = new Container(); }
container.register!(InterfaceA, ComponentC);
auto componentD = new ComponentD(); // Test autowiring concrete type to existing instance
container.autowire!(ComponentD)(componentD); unittest {
assert(!componentD.componentIsNull(), "Autowirable dependency failed to autowire"); auto container = new Container();
} container.register!ComponentA;
auto componentB = new ComponentB();
// Test autowiring will only happen once container.autowire!(ComponentB)(componentB);
unittest { assert(!componentB.componentIsNull(), "Autowirable dependency failed to autowire");
auto container = new Container(); }
container.register!(InterfaceA, ComponentC).newInstance();
auto componentD = new ComponentD(); // Test autowiring interface type to existing instance
container.autowire!(ComponentD)(componentD); unittest {
auto expectedComponent = componentD.componentC; auto container = new Container();
container.autowire!(ComponentD)(componentD); container.register!(InterfaceA, ComponentC);
auto actualComponent = componentD.componentC; auto componentD = new ComponentD();
assert(expectedComponent is actualComponent, "Autowiring the second time wired a different instance"); container.autowire!(ComponentD)(componentD);
} assert(!componentD.componentIsNull(), "Autowirable dependency failed to autowire");
}
// Test autowiring unregistered type
unittest { // Test autowiring will only happen once
auto container = new Container(); unittest {
auto componentD = new ComponentD(); auto container = new Container();
assertThrown!(ResolveException)(container.autowire!(ComponentD)(componentD), "Autowiring unregistered type should throw ResolveException"); container.register!(InterfaceA, ComponentC).newInstance();
} auto componentD = new ComponentD();
container.autowire!(ComponentD)(componentD);
// Test autowiring member with non-autowire attribute does not autowire auto expectedComponent = componentD.componentC;
unittest { container.autowire!(ComponentD)(componentD);
auto container = new Container(); auto actualComponent = componentD.componentC;
auto componentE = new ComponentE(); assert(expectedComponent is actualComponent, "Autowiring the second time wired a different instance");
container.autowire!ComponentE(componentE); }
assert(componentE.componentC is null, "Autowiring should not occur for members with attributes other than @Autowire");
} // Test autowiring unregistered type
unittest {
// Test autowire in constructor auto container = new Container();
unittest { auto componentD = new ComponentD();
auto container = Container.getInstance(); assertThrown!(ResolveException)(container.autowire!(ComponentD)(componentD), "Autowiring unregistered type should throw ResolveException");
container.register!ComponentA; }
auto componentF = new ComponentF();
auto autowiredComponentA = componentF.componentA; // Test autowiring member with non-autowire attribute does not autowire
container.register!(ComponentF).existingInstance(componentF); unittest {
assert(componentF.componentA !is null, "Constructor did not autowire component F"); auto container = new Container();
auto componentE = new ComponentE();
auto resolvedComponentF = container.resolve!ComponentF; container.autowire!ComponentE(componentE);
assert(resolvedComponentF.componentA is autowiredComponentA, "Resolving instance of ComponentF rewired members"); assert(componentE.componentC is null, "Autowiring should not occur for members with attributes other than @Autowire");
}
container.clearAllRegistrations();
} // Test autowire in constructor
unittest {
auto container = Container.getInstance();
container.register!ComponentA;
auto componentF = new ComponentF();
auto autowiredComponentA = componentF.componentA;
container.register!(ComponentF).existingInstance(componentF);
assert(componentF.componentA !is null, "Constructor did not autowire component F");
auto resolvedComponentF = container.resolve!ComponentF;
assert(resolvedComponentF.componentA is autowiredComponentA, "Resolving instance of ComponentF rewired members");
container.clearAllRegistrations();
}
// Test autowire class with alias declaration
unittest {
auto container = new Container();
container.register!ComponentA;
auto componentDeclarationCocktail = new ComponentDeclarationCocktail();
container.autowire(componentDeclarationCocktail);
assert(componentDeclarationCocktail.componentA !is null, "Autowiring class with non-assignable declarations failed");
}
} }