Add mixin for boilerplate workaround

This commit is contained in:
Mike Bierlee 2014-06-22 17:16:07 +02:00
parent 06024e55db
commit 627d165d6f
3 changed files with 16 additions and 6 deletions

View file

@ -126,14 +126,20 @@ Known issues
* Due to preventive measures of recursion issues in circular dependencies, registrations which are supposed to yield new instances will not autowire classes for which a circular dependency is detected. A new instance will be resolved but the instance's members will not be autowired. * Due to preventive measures of recursion issues in circular dependencies, registrations which are supposed to yield new instances will not autowire classes for which a circular dependency is detected. A new instance will be resolved but the instance's members will not be autowired.
* Resolving a class registered by supertype or interface will only autowire the members inherited from its supertypes and in the case of interfaces none at all. A workaround for this issue is to autowire members in the constructor of a class: * Resolving a class registered by supertype or interface will only autowire the members inherited from its supertypes and in the case of interfaces none at all. A workaround for this issue is to autowire members in the constructor of a class:
```d ```d
import poodinis.autowire;
class ComponentF { class ComponentF {
@Autowire @Autowire
public ComponentA componentA; public ComponentA componentA;
public this() { public this() {
auto container = Container.getInstance(); auto container = Container.getInstance();
container.autowire!ComponentF(this); container.autowire!(typeof(this))(this);
} }
// or use:
// mixin AutowireConstructor;
// which adds the constructor above
} }
``` ```

View file

@ -36,4 +36,11 @@ public void autowire(Type)(Container container, Type instance) {
} }
} }
} }
} }
mixin template AutowireConstructor() {
public this() {
auto __container = Container.getInstance();
__container.autowire!(typeof(this))(this);
}
}

View file

@ -46,10 +46,7 @@ version(unittest) {
@Autowire @Autowire
public ComponentA componentA; public ComponentA componentA;
public this() { mixin AutowireConstructor;
auto container = Container.getInstance();
container.autowire!(typeof(this))(this);
}
} }
// Test autowiring concrete type to existing instance // Test autowiring concrete type to existing instance