Skip to content

Commit

Permalink
Merge pull request #8 from Extrawurst/develop
Browse files Browse the repository at this point in the history
fix autowiring in base classes
  • Loading branch information
Mike Bierlee committed Feb 9, 2016
2 parents a8f4558 + f84b025 commit 7e05817
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
6 changes: 6 additions & 0 deletions source/poodinis/autowire.d
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,12 @@ public void autowire(Type)(shared(DependencyContainer) container, Type instance)
printDebugAutowiredInstance(typeid(Type), &instance);
}

// note: recurse into base class if there are more between Type and Object in the hirarchy
static if(BaseClassesTuple!Type.length > 1)
{
autowire!(BaseClassesTuple!Type[0])(container, instance);
}

foreach(idx, name; FieldNameTuple!Type) {
autowireMember!(name, idx, Type)(container, instance);
}
Expand Down
16 changes: 16 additions & 0 deletions test/poodinis/autowiretest.d
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ version(unittest) {

class ComponentX : InterfaceA {}

class ComponentZ : ComponentB {
}

class MonkeyShine {
@Autowire!ComponentX
public InterfaceA component;
Expand Down Expand Up @@ -198,4 +201,17 @@ version(unittest) {

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);
}
}

0 comments on commit 7e05817

Please sign in to comment.