diff --git a/README.md b/README.md index adba8ee..4b8c819 100644 --- a/README.md +++ b/README.md @@ -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. * 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 +import poodinis.autowire; + class ComponentF { @Autowire public ComponentA componentA; public this() { auto container = Container.getInstance(); - container.autowire!ComponentF(this); + container.autowire!(typeof(this))(this); } + + // or use: + // mixin AutowireConstructor; + // which adds the constructor above } ``` diff --git a/source/poodinis/autowire.d b/source/poodinis/autowire.d index 4a13fa1..cc9d669 100644 --- a/source/poodinis/autowire.d +++ b/source/poodinis/autowire.d @@ -36,4 +36,11 @@ public void autowire(Type)(Container container, Type instance) { } } } -} \ No newline at end of file +} + +mixin template AutowireConstructor() { + public this() { + auto __container = Container.getInstance(); + __container.autowire!(typeof(this))(this); + } +} diff --git a/test/poodinis/autowiretest.d b/test/poodinis/autowiretest.d index 5daa0ab..b8e78b9 100644 --- a/test/poodinis/autowiretest.d +++ b/test/poodinis/autowiretest.d @@ -46,10 +46,7 @@ version(unittest) { @Autowire public ComponentA componentA; - public this() { - auto container = Container.getInstance(); - container.autowire!(typeof(this))(this); - } + mixin AutowireConstructor; } // Test autowiring concrete type to existing instance