diff --git a/source/poodinis/autowire.d b/source/poodinis/autowire.d index a5ace16..7eb36b8 100644 --- a/source/poodinis/autowire.d +++ b/source/poodinis/autowire.d @@ -16,11 +16,11 @@ debug { import std.string; } -class Autowire{}; +struct UseMemberType {}; -struct Qualifier(T) { - T qualifier; -} +struct Autowire(QualifierType = UseMemberType) { + QualifierType qualifier; +}; alias Autowired = Autowire; @@ -36,31 +36,22 @@ public void autowire(Type)(DependencyContainer container, Type instance) { foreach (member ; __traits(allMembers, Type)) { static if(__traits(compiles, __traits(getMember, Type, member)) && __traits(compiles, __traits(getAttributes, __traits(getMember, Type, member)))) { foreach(autowireAttribute; __traits(getAttributes, __traits(getMember, Type, member))) { - static if (is(autowireAttribute : Autowire)) { + static if (__traits(isSame, autowireAttribute, Autowire) || is(autowireAttribute == Autowire!T, T)) { if (__traits(getMember, instance, member) is null) { - alias TypeTuple!(__traits(getMember, instance, member)) memberReference; + alias memberReference = TypeTuple!(__traits(getMember, instance, member)); alias MemberType = typeof(memberReference)[0]; debug { string qualifiedInstanceTypeString = typeid(MemberType).toString; } MemberType qualifiedInstance; - auto resolvedThroughQualifier = false; - foreach (qualifierAttribute; __traits(getAttributes, __traits(getMember, Type, member))) { - static if (is(qualifierAttribute == Qualifier!T, T)) { - alias QualifierType = typeof(qualifierAttribute.qualifier); - qualifiedInstance = container.resolve!(typeof(memberReference), QualifierType); - - debug { - qualifiedInstanceTypeString = typeid(QualifierType).toString; - } - - resolvedThroughQualifier = true; - break; + static if (is(autowireAttribute == Autowire!T, T) && !is(autowireAttribute.qualifier == UseMemberType)) { + alias QualifierType = typeof(autowireAttribute.qualifier); + qualifiedInstance = container.resolve!(typeof(memberReference), QualifierType); + debug { + qualifiedInstanceTypeString = typeid(QualifierType).toString; } - } - - if (!resolvedThroughQualifier) { + } else { qualifiedInstance = container.resolve!(typeof(memberReference)); } diff --git a/test/poodinis/autowiretest.d b/test/poodinis/autowiretest.d index 4b1815f..09ba841 100644 --- a/test/poodinis/autowiretest.d +++ b/test/poodinis/autowiretest.d @@ -62,18 +62,15 @@ version(unittest) { class ComponentX : InterfaceA {} class MonkeyShine { - @Autowire - @Qualifier!ComponentX + @Autowire!ComponentX public InterfaceA component; } class BootstrapBootstrap { - @Autowire - @Qualifier!ComponentX + @Autowire!ComponentX public InterfaceA componentX; - @Autowire - @Qualifier!ComponentC + @Autowire!ComponentC public InterfaceA componentC; }