mirror of
https://github.com/mbierlee/poodinis.git
synced 2024-11-15 04:04:01 +01:00
Fix autowiring classes with non-symbolic unassignable members (such as aliases)
This commit is contained in:
parent
af8e154dc5
commit
b38bccc03c
|
@ -20,7 +20,8 @@ class Autowire{};
|
|||
|
||||
public void autowire(Type)(Container container, Type instance) {
|
||||
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 )))) {
|
||||
foreach(attribute; __traits(getAttributes, __traits(getMember, Type, member))) {
|
||||
if (is(attribute : Autowire) && __traits(getMember, instance, member) is null){
|
||||
alias TypeTuple!(__traits(getMember, instance, member)) memberReference;
|
||||
auto autowirableInstance = container.resolve!(typeof(memberReference));
|
||||
|
@ -37,6 +38,7 @@ public void autowire(Type)(Container container, Type instance) {
|
|||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
mixin template AutowireConstructor() {
|
||||
public this() {
|
||||
|
|
|
@ -49,6 +49,19 @@ version(unittest) {
|
|||
mixin AutowireConstructor;
|
||||
}
|
||||
|
||||
class ComponentDeclarationCocktail {
|
||||
alias noomer = int;
|
||||
|
||||
@Autowire
|
||||
public ComponentA componentA;
|
||||
|
||||
public void doesNothing() {
|
||||
}
|
||||
|
||||
~this(){
|
||||
}
|
||||
}
|
||||
|
||||
// Test autowiring concrete type to existing instance
|
||||
unittest {
|
||||
auto container = new Container();
|
||||
|
@ -108,4 +121,15 @@ version(unittest) {
|
|||
|
||||
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");
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue