diff --git a/README.md b/README.md index db96b6d..78a3d78 100644 --- a/README.md +++ b/README.md @@ -133,8 +133,7 @@ class ComponentF { public ComponentA componentA; public this() { - auto container = Container.getInstance(); - container.autowire!(typeof(this))(this); + globalAutowire!(typeof(this))(this); } // or use: diff --git a/source/poodinis/autowire.d b/source/poodinis/autowire.d index cc9d669..9a929da 100644 --- a/source/poodinis/autowire.d +++ b/source/poodinis/autowire.d @@ -1,46 +1,49 @@ -/** - * Poodinis Dependency Injection Framework - * Copyright 2014 Mike Bierlee - * This software is licensed under the terms of the MIT license. - * The full terms of the license can be found in the LICENSE file. - */ - -module poodinis.autowire; - -public import poodinis.container; - -import std.typetuple; - -debug { - import std.stdio; - import std.string; -} - -class Autowire{}; - -public void autowire(Type)(Container container, Type instance) { - foreach (member ; __traits(allMembers, Type)) { - foreach (attribute; mixin(`__traits(getAttributes, 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)); - debug { - auto autowirableType = typeid(typeof(memberReference[0])); - auto autowireableAddress = &autowirableInstance; - auto memberType = typeid(Type); - auto instanceAddress = &instance; - writeln(format("DEBUG: Autowire instance [%s@%s] to [%s@%s].%s", autowirableType, autowireableAddress, memberType, instanceAddress, member)); - } - - __traits(getMember, instance, member) = autowirableInstance; - } - } - } -} - -mixin template AutowireConstructor() { - public this() { - auto __container = Container.getInstance(); - __container.autowire!(typeof(this))(this); - } -} +/** + * Poodinis Dependency Injection Framework + * Copyright 2014 Mike Bierlee + * This software is licensed under the terms of the MIT license. + * The full terms of the license can be found in the LICENSE file. + */ + +module poodinis.autowire; + +public import poodinis.container; + +import std.typetuple; + +debug { + import std.stdio; + import std.string; +} + +class Autowire{}; + +public void autowire(Type)(Container container, Type instance) { + foreach (member ; __traits(allMembers, Type)) { + foreach (attribute; mixin(`__traits(getAttributes, 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)); + debug { + auto autowirableType = typeid(typeof(memberReference[0])); + auto autowireableAddress = &autowirableInstance; + auto memberType = typeid(Type); + auto instanceAddress = &instance; + writeln(format("DEBUG: Autowire instance [%s@%s] to [%s@%s].%s", autowirableType, autowireableAddress, memberType, instanceAddress, member)); + } + + __traits(getMember, instance, member) = autowirableInstance; + } + } + } +} + +mixin template AutowireConstructor() { + public this() { + globalAutowire!(typeof(this))(this); + } +} + +public void globalAutowire(Type)(Type instance) { + Container.getInstance().autowire!(typeof(instance))(instance); +} \ No newline at end of file